Today someone on #sinatra asked if it was possible to easily define the same handler for multiple HTTP verbs at the same time.
Sinatra itself doesn’t include that, but it’s very easy to create a helper method that does it for you:
# Define a handler for multiple http verbs at once
def any(url, verbs = %w(get post put delete), &block)
verbs.each do |verb|
send(verb, url, &block)
end
end
any '/url' do
# ...
"return value"
end
any '/other_url', %w(get post) do
# ...
end
(Hat-tip to Harry Vangberg for naming suggestions)
It’s not quite perfect. It doesn’t pass any extra args to the routes, like :agent, and if you don’t put the definition on the same level where you define your routes, it might not work (you have to make sure Sinatra::Application gets the send).
But it probably works for most cases.
Tweet
