hannesg / uri_template

A URI template library for ruby.
169 stars 85 forks source link

Optional fragments depending on other fragments #9

Open andrewpthorp opened 11 years ago

andrewpthorp commented 11 years ago

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!

hannesg commented 11 years ago

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

andrewpthorp commented 11 years ago

Thanks! I'll work on this asap.

hannesg commented 11 years ago

Cool!

hannesg commented 11 years ago

Hi @andrewpthorp

Any progress so far? Can I help?

andrewpthorp commented 11 years ago

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!