Class: Grape::API
- Inherits:
-
Object
- Object
- Grape::API
- Extended by:
- Middleware::Auth::DSL
- Includes:
- DSL::Callbacks, DSL::Configuration, DSL::Helpers, DSL::Middleware, DSL::RequestResponse, DSL::Routing, DSL::Validations
- Defined in:
- lib/grape/api.rb,
lib/grape.rb,
lib/grape/api/helpers.rb,
lib/grape/validations/coerce.rb
Overview
The API class is the primary entry point for creating Grape APIs.Users should subclass this class in order to build an API.
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- LOCK =
Mutex.new
- Boolean =
rubocop:disable ConstantName
Virtus::Attribute::Boolean
Class Attribute Summary collapse
-
.instance ⇒ Object
readonly
Returns the value of attribute instance.
Class Method Summary collapse
- .call(env) ⇒ Object
- .call!(env) ⇒ Object
- .cascade(value = nil) ⇒ Object
- .change! ⇒ Object
- .compile ⇒ Object
-
.imbue(key, value) ⇒ Object
Add to a configuration value for this namespace.
- .reset! ⇒ Object
-
.scope(name = nil, &block) ⇒ Object
Create a scope without affecting the URL.
-
.set(key, value) ⇒ Object
Set a configuration value for this namespace.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#cascade? ⇒ Boolean
Some requests may return a HTTP 404 error if grape cannot find a matching route.
-
#initialize ⇒ API
constructor
A new instance of API.
Methods included from Middleware::Auth::DSL
Constructor Details
#initialize ⇒ API
Returns a new instance of API.
118 119 120 121 122 123 124 125 |
# File 'lib/grape/api.rb', line 118 def initialize @route_set = Rack::Mount::RouteSet.new self.class.endpoints.each do |endpoint| endpoint.mount_in(@route_set) end @route_set.freeze end |
Class Attribute Details
.instance ⇒ Object (readonly)
Returns the value of attribute instance.
17 18 19 |
# File 'lib/grape/api.rb', line 17 def instance @instance end |
Class Method Details
.call(env) ⇒ Object
37 38 39 40 |
# File 'lib/grape/api.rb', line 37 def call(env) LOCK.synchronize { compile } unless instance call!(env) end |
.call!(env) ⇒ Object
42 43 44 |
# File 'lib/grape/api.rb', line 42 def call!(env) instance.call(env) end |
.cascade(value = nil) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/grape/api.rb', line 53 def cascade(value = nil) if value.nil? settings.key?(:cascade) ? !!settings[:cascade] : true else set(:cascade, value) end end |
.change! ⇒ Object
33 34 35 |
# File 'lib/grape/api.rb', line 33 def change! @instance = nil end |
.compile ⇒ Object
29 30 31 |
# File 'lib/grape/api.rb', line 29 def compile @instance ||= new end |
.imbue(key, value) ⇒ Object
Add to a configuration value for this namespace.
74 75 76 |
# File 'lib/grape/api.rb', line 74 def imbue(key, value) settings.imbue(key, value) end |
.reset! ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/grape/api.rb', line 21 def reset! @settings = Grape::Util::HashStack.new @route_set = Rack::Mount::RouteSet.new @endpoints = [] @routes = nil reset_validations! end |
.scope(name = nil, &block) ⇒ Object
Create a scope without affecting the URL.
49 50 51 |
# File 'lib/grape/api.rb', line 49 def scope(name = nil, &block) nest(block) end |
.set(key, value) ⇒ Object
Set a configuration value for this namespace.
65 66 67 |
# File 'lib/grape/api.rb', line 65 def set(key, value) settings[key.to_sym] = value end |
Instance Method Details
#call(env) ⇒ Object
127 128 129 130 131 |
# File 'lib/grape/api.rb', line 127 def call(env) status, headers, body = @route_set.call(env) headers.delete('X-Cascade') unless cascade? [status, headers, body] end |
#cascade? ⇒ Boolean
Some requests may return a HTTP 404 error if grape cannot find a matching route. In this case, Rack::Mount adds a X-Cascade header to the response and sets it to 'pass', indicating to grape's parents they should keep looking for a matching route on other resources.
In some applications (e.g. mounting grape on rails), one might need to trap errors from reaching upstream. This is effectivelly done by unsetting X-Cascade. Default :cascade is true.
141 142 143 144 145 |
# File 'lib/grape/api.rb', line 141 def cascade? return !!self.class.settings[:cascade] if self.class.settings.key?(:cascade) return !!self.class.settings[:version_options][:cascade] if self.class.settings[:version_options] && self.class.settings[:version_options].key?(:cascade) true end |