hannesg / uri_template

A URI template library for ruby.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Partial expand

andreypopp opened this issue · comments

It would be nice to have partial expand feature which works by binding supplied values to variables and produces another template:

URLTemplate.new("{a}/{b}").partial_expand(a: 1)
=> <URLTemplate @pattern="1/{b}">

Yep, I agree. I left it out at first because I didn't see the immediate value, but now I some pointful usecases.

Okay, I've implemented something in the 0.6.0 release. Please test and report if something doesn't work like you expect it.

This is cool, though I expect

URITemplate.new('{+path}').expand_partial(path: 'x').to_s == 'x{+path}'

and

URITemplate.new('{?x,y}').expand_partial(path: 'x').to_s == '?x=1{&y}'

and so on...

Hm, how should this case be handled:

URITemplate.new('{?x,y}').expand_partial(y: 'z')

{?x}&y=z is not guaranteed to issue a leading questionmark.

Do you think components inside {?...} should be expanded in order they are defined? I didn't find anything about it in RFC6570. I think that:

URITemplate.new('{?x,y}').expand_partial(y: 'z') # => URITemplate('?y=z{&x}')

would be reasonable.

I guess you're right. Just because the example code uses the order of the variables doesn't mean it's mandatory.

Hi, I've written some more code and some more specs. I've also grant you push access in case you want to add some more specs or change the behavior.

Hi @andreypopp

Have you tested the changed behavior? Can you give me some feedback?

Hi, I notice the behavior of URITemplate has changed between 0.6.0 and 0.7.0

URITemplate.new('/foo/{x}/{y}').expand_partial(x: 123).to_s
# 0.6.0 => "/foo/123/{y}"
# 0.7.0 => "/foo/123{x}/{y}"

is there an API to get the old behaviour? I'm using URITemplate for Yaks and this is blocking me from upgrading.