Module: Roda::RodaPlugins::NotAllowed
- Defined in:
- lib/roda/plugins/not_allowed.rb
Overview
The not_allowed plugin makes Roda attempt to automatically support the 405 Method Not Allowed response status. The plugin changes the r.get
and r.post
verb methods to automatically return a 405 status if they are called with any arguments, and the arguments match but the request method does not match. So this code:
r.get '' do
"a"
end
will return a 200 response for GET /
and a 405 response for POST /
.
This plugin also changes the r.is
method so that if you use a verb method inside r.is
, it returns a 405 status if none of the verb methods match. So this code:
r.is '' do
r.get do
"a"
end
r.post do
"b"
end
end
will return a 200 response for GET /
and POST /
, but a 405 response for PUT /
.
Note that this plugin will probably not do what you want for code such as:
r.get '' do
"a"
end
r.post '' do
"b"
end
Since for a POST /
request, when r.get
method matches the path but not the request method, it will return an immediate 405 response. You must DRY up this code for it work correctly, like this:
r.is '' do
r.get do
"a"
end
r.post do
"b"
end
end
In all cases where it uses a 405 response, it also sets the Allow
header in the response to contain the request methods supported.
To make this affect the verb methods added by the all_verbs plugin, load this plugin first.
Defined Under Namespace
Modules: RequestClassMethods, RequestMethods
Class Method Summary collapse
-
.configure(app) ⇒ Object
Redefine the
r.get
andr.post
methods when loading the plugin.
Class Method Details
.configure(app) ⇒ Object
Redefine the r.get
and r.post
methods when loading the plugin.
67 68 69 70 71 72 |
# File 'lib/roda/plugins/not_allowed.rb', line 67 def self.configure(app) app.request_module do app::RodaRequest.def_verb_method(self, :get) app::RodaRequest.def_verb_method(self, :post) end end |