http_router and multiple regex on a single parameter

20 views
Skip to first unread message

Adam Watson

unread,
Nov 15, 2011, 9:33:04 PM11/15/11
to Goliath.io
I'm having problems getting multiple routes with differently formatted
variable segments to work... is it possible to provide multiple
similar routes with different regex constraints for the variable
segment?

In other words, the following route assignments do not work since only
the first route seems to be registered:

class RackRoutes < Goliath::API

get '/items/:item_type/count', :item_type => /^[a-zA-Z][\w\-]+$/
do
... do some awesome stuff with an alpha-based param...
end

get '/items/:item_id/count', :item_id => /\d+/ do
...do some stuff with a numeric param....
end

end

In the above code, if I try to hit the second route using a numeric
parameter segment, I always get a 404.

I know I could perform the regex inside the body of the mapping, but I
really want a 404 to be issued if neither of the regex patterns are
matched using the map provided.


Thanks!
Adam

Joshua Hull

unread,
Nov 15, 2011, 10:30:00 PM11/15/11
to golia...@googlegroups.com, Goliath.io
I'll take a look at this!

Adam Watson

unread,
Nov 16, 2011, 2:50:56 PM11/16/11
to Goliath.io
Awesome, thanks Josh.

In the meantime, I worked around it using the following pattern:

https://gist.github.com/1371133

Cheers,
Adam

On Nov 15, 7:30 pm, Joshua Hull <joshbu...@gmail.com> wrote:
> I'll take a look at this!
>
Reply all
Reply to author
Forward
0 new messages