Support for named parameters in URLMap (replaces #343) #372

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
4 participants

ta commented Mar 23, 2012

Added support for sinatra-like named parameters in URLMap, which can be useful when working with scoped resources.

Note: Moved from master to a topic branch and replaces pull request #343 - a lesson learned

Named groups are not available in Ruby 1.8.x

Owner

ta replied Feb 21, 2012

Fix committed in 5258bc7

Owner

raggi commented May 13, 2012

Might I ask why you wouldn't just use Sinatra for this?

Member

rkh commented May 13, 2012

Or any other Rack router that's out there. This opens up a ton of issues otherwise.

Owner

raggi commented May 13, 2012

Thank you for your patch. I'm afraid that we're not accepting this one on the grounds that we'd like to keep URLMap very simple, and keep our edge cases low. You might consider creating a simple gem that implements this instead, or using Sinatra.

Thanks again!

raggi closed this May 13, 2012

ta commented May 14, 2012

You are most welcome! Sinatra wasn't a good fit for my project at hand.

Member

rkh commented May 14, 2012

There are a ton of other routers out there.

ta commented May 14, 2012

@rkh: Thank you!

ta commented May 22, 2012

@raggi: Just wanted to explain my use case. With my "named parameters" patch to URLMap I can re-use my apps in different scopes like this:

class Articles < Sinatra::Base
  get "/:id/?" do; ...; end
  get "/?" do; ...; end.
end

class Comments < Sinatra::Base
  # Handle scope based on rack.url_params
  get "/:id/?" do; ...; end
  get "/?" do; ...; end
end

map "/" do
  map "/articles" do
    map "/:article_id/comments" do
      run Comments.new
    end
    run Articles.new
  end
  map "/comments" do
    run Comments.new
  end
end

I guess the "normal" way is to write something like this code below (which might not seem too bad, but if I were to add some classes like Tags etc., it would quickly become a mess with lots of code repetition across the apps).

class Articles < Sinatra::Base
  get "/:id/comments/:id/?" do; ...; end
  get "/:id/comments/?" do; ...; end
  get "/:id/?" do; ...; end
  get "/?" do; ...; end.
end

class Comments < Sinatra::Base
  get "/:id/?" do; ...; end
  get "/?" do; ...; end
end

map "/" do
  map "/articles" do
    run Articles.new
  end
  map "/comments" do
    run Comments.new
  end
end

I can't find any other router/middleware/gem (as @rkh suggests) that can handle my use case. If you have some pointers on how (if possible) to transform my patch into some middleware/gem, I would really appreciate it! Thanks.

Owner

raggi commented May 27, 2012

Seems like the kind of thing that Rails is really good at....

Member

rkh commented May 28, 2012

class Comments < Sinatra::Base
  # ...
end

class Articles < Sinatra::Base
  # ...
  get '/:id/comments/*' do
    Comments.call(env)
  end
end

map('/articles') { run Articles }
map('/comments') { run Comments }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment