hannesg / uri_template

A URI template library for ruby.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optional fragments depending on other fragments

andrewpthorp opened this issue · comments

I would be more than willing to implement this, but I was wondering if it's desired or if it's already possible:

Let's say, you have the following template:

tpl = URITemplate.new(:colon, 'http://some.api.com[/:optional[/:optional2]]')

:optional2: would only be added if :optional1 existed. In other words:

tpl.expand(optional1: 'foo', optional2: 'bar')
# => http://some.api.com/foo/bar

tpl.expand(optional1: 'foo')
# => http://some.api.com/foo

tpl.expand(optional2: 'bar')
# => http://some.api.com

I have seen this pattern implemented in a few APIs (we can debate the design of said APIs another time). Is this possible with uri_template? If not, would there be any interest in me adding it?

If there is interest in me adding it, could we discuss what the pattern would look like for both formats?

Thanks!

Hi

The colon template in uri_template is currently ultra-simple, so this isn't currently possible. I think it would be great if that was possible. My initial idea for the colon style templates was to implement rails/sinatra-style patterns so that people can simply reuse their routing patterns. So I'd prefer the rails syntax for conditionals: http://some.api.com(/:optional(/:optional2)). Maybe subclassing the colon template class for the different flavours (rails, sinatra, your own ... ) would be an option, too.

Anyway, you have push access now.

Cheers'
Hannes

Thanks! I'll work on this asap.

Hi @andrewpthorp

Any progress so far? Can I help?

Hey @hannesg - I was actually on a family vacation all week last week. Going to start on this in the next few days.

I'll let you know where I can help, perhaps we can pair on it!