Sinatra 中的动词不可知匹配

发布于 2024-12-19 20:57:02 字数 235 浏览 3 评论 0原文

我们可以写

get '/foo' do
  ...
end

post '/foo' do
  ...
end

这很好。但是我可以在一个路由中组合多个 HTTP 动词吗?

We can write

get '/foo' do
  ...
end

and

post '/foo' do
  ...
end

which is fine. But can I combine multiple HTTP verbs in one route?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

憧憬巴黎街头的黎明 2024-12-26 20:57:03

这可以通过多路由扩展 是 sinatra-contrib 的一部分:

require 'sinatra'
require "sinatra/multi_route"
route :get, :post, '/foo' do
  # "GET" or "POST"
  p request.env["REQUEST_METHOD"]
end

# Or for module-style applications
class MyApp < Sinatra::Base
  register Sinatra::MultiRoute
  route :get, :post, '/foo' do
    # ...
  end
end

但是,请注意,您可以自己简单地完成此操作,而无需扩展 via:

foo = lambda do
  # Your route here
end
get  '/foo', &foo
post '/foo', &foo

或者更优雅地作为元方法:

def self.get_or_post(url,&block)
  get(url,&block)
  post(url,&block)
end

get_or_post '/foo' do
  # ...
end

您可能还对 关于该功能的讨论

This is possible via the multi-route extension that is part of sinatra-contrib:

require 'sinatra'
require "sinatra/multi_route"
route :get, :post, '/foo' do
  # "GET" or "POST"
  p request.env["REQUEST_METHOD"]
end

# Or for module-style applications
class MyApp < Sinatra::Base
  register Sinatra::MultiRoute
  route :get, :post, '/foo' do
    # ...
  end
end

However, note that you can do this simply yourself without the extension via:

foo = lambda do
  # Your route here
end
get  '/foo', &foo
post '/foo', &foo

Or more elegantly as a meta-method:

def self.get_or_post(url,&block)
  get(url,&block)
  post(url,&block)
end

get_or_post '/foo' do
  # ...
end

You might also be interested in this discussion on the feature.

帥小哥 2024-12-26 20:57:03

FWIW,我只是手动执行此操作,没有辅助方法或扩展:

%i(get post).each do |method|
   send method, '/foo' do 
       ...
   end
 end

尽管如果您经常执行此操作,那么将其抽象出来当然是有意义的。

FWIW, I just do it manually, with no helper methods or extensions:

%i(get post).each do |method|
   send method, '/foo' do 
       ...
   end
 end

Although if you're doing it a lot it of course makes sense to abstract that out.

焚却相思 2024-12-26 20:57:03

Phrogz 有一个很好的答案,但如果 lambda 或包含 sinatra-contrib 不适合您,那么此元方法将达到与 sinatra-contrib 相同的结果,以达到您的目的:

# Provides a way to handle multiple HTTP verbs with a single block
#
# @example
#   route :get, :post, '/something' do
#       # Handle your route here
#   end
def self.route(*methods, path, &block)
    methods.each do |method|
        method.to_sym
        self.send method, path, &block
    end
end

如果您对能够发送有点谨慎任意方法self,那么您可以通过在数组中设置允许方法的白名单来保护自己,然后检查该符号在数组中是否存在。

# Provides a way to handle multiple HTTP verbs with a single block
#
# @example
#   route :get, :post, '/something' do
#       # Handle your route here
#   end
def self.route(*methods, path, &block)
    allowed_methods = [:get, :post, :delete, :patch, :put, :head, :options]
    methods.each do |method|
        method.to_sym
        self.send(method, path, &block) if allowed_methods.include? method
    end
end

Phrogz has a great answer, but if either lambdas or including sinatra-contrib isn't for you, then this meta method will achieve the same result as sinatra-contrib for your purposes:

# Provides a way to handle multiple HTTP verbs with a single block
#
# @example
#   route :get, :post, '/something' do
#       # Handle your route here
#   end
def self.route(*methods, path, &block)
    methods.each do |method|
        method.to_sym
        self.send method, path, &block
    end
end

If you're a little wary of being able to send arbitrary methods to self, then you can protect yourself by setting up a whitelist of allowed methods in an array, and then checking for the symbol's presence in the array.

# Provides a way to handle multiple HTTP verbs with a single block
#
# @example
#   route :get, :post, '/something' do
#       # Handle your route here
#   end
def self.route(*methods, path, &block)
    allowed_methods = [:get, :post, :delete, :patch, :put, :head, :options]
    methods.each do |method|
        method.to_sym
        self.send(method, path, &block) if allowed_methods.include? method
    end
end
疯到世界奔溃 2024-12-26 20:57:03

这是一个服务不可用的服务器,我设法在单行上获得它:)

require 'sinatra';set port: ARGV[0]||80;%w.get post put patch options delete..map{|v|send(v,'*'){503}}

我实际上用它来测试一些客户端代码在面对 503 时的行为。

Here's a service-unavailable server that I managed to get on single line :)

require 'sinatra';set port: ARGV[0]||80;%w.get post put patch options delete..map{|v|send(v,'*'){503}}

I actually used this to test the behavior of some client code in the face of 503s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文