Module: Sinatra::REST::Routes
- Defined in:
- lib/sinatra/rest/routes.rb
Overview
Constant Summary collapse
- INFINITY =
1/0.0
Instance Method Summary collapse
-
#allow(proc) ⇒ Object
Allow access to the route based on the result of the given proc, whose argument is a
credentials
object. -
#options(path, opts = {}, &bk) ⇒ Object
Allow the definition of OPTIONS routes.
Instance Method Details
#allow(proc) ⇒ Object
Allow access to the route based on the result of the given proc, whose argument is a credentials
object. You MUST declare a helper function named credentials
that will return an object (of your choice) containing the client’s credentials, that will be passed as an argument to the given Proc. Halts the response process with a 403 status code if the given proc returns false (you may then process the error with a error 403 {}
block). e.g.
helpers do
def credentials; [params["user"], params["password"]]; end
end
get '/', :allow => Proc.new{ |credentials| credentials.first == "someone" && credentials.last == "password" } do
"allowed"
end
26 27 28 29 30 31 32 33 |
# File 'lib/sinatra/rest/routes.rb', line 26 def allow(proc) raise ArgumentError, "You must provide a Proc that returns true or false when given the result of a call to the 'credentials' helper" unless proc.kind_of?(Proc) condition { unless proc.call(credentials) halt 403, "You cannot access this resource" end } end |
#options(path, opts = {}, &bk) ⇒ Object
Allow the definition of OPTIONS routes
13 |
# File 'lib/sinatra/rest/routes.rb', line 13 def (path, opts={}, &bk); route 'OPTIONS', path, opts, &bk end |