Skip to content
This repository

Required and optional parameters for route #490

Closed
ryanwi opened this Issue March 21, 2012 · 7 comments

5 participants

Ryan Williams Konstantin Haase Todd A. Jacobs Henrik Wist omnilinguist
Ryan Williams

Per SO question (http://stackoverflow.com/questions/9775591/required-and-optional-parameters-for-sinatra-route) it would be useful to have required and optional parameters in route strings for things like adding ".json" to a resource id to build a JSON response.

This works fine:

get '/widgets.?:format?'

But, this currently does not:

get '/widgets/:id.?:format?'

If you pass 'abc.json' to that it, the :id parameter will be 'abc.json' and the format parameter will be empty.

The compiled regex appears to be:

/^\/widgets\/([^\/?#]+)(?:\.|%2E)?([^\/?#]+)?$/

The "." is not part of the exclusion in the first group, so it doesn't stop there as it would for a "/" or "#".

Ryan Williams

I did get past this by going full regex on the route and excluding the "." from the first regex group. But, this seems like a fairly common use case that would be helpful to support in regular route strings.

get %r{/widgets\/([^\/?#\.]+)(?:\.|%2E)?([^\/?#]+)?}
Konstantin Haase
Owner
rkh commented March 22, 2012

Discussion about possible solutions: https://gist.github.com/2154980

Todd A. Jacobs

+1 This issue is biting me, too. The Sinatra DSL should handle format extensions such as .xml, .json, and so forth in a sensible and consistent way.

Henrik Wist

Reading through the discussion in https://gist.github.com/2154980 and subsequently in #492, shouldn't this be fixed with 762967f?

Konstantin Haase
Owner

Yes, indeed.

Konstantin Haase rkh closed this November 30, 2012
omnilinguist

Does any official Sinatra release include this fix? I see that 1.3.3 is the latest, but this issue is listed as Milestone 1.4.0.

In the meantime, should we apply a similar workaround that the SO poster tried? How did he generate the compiled Sinatra regex from the route?

Konstantin Haase
Owner

@omnilinguist you could also just run against Sinatra master. I'll see that I can prep a release soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.