Class: Grape::API
- Includes:
- DSL::API
- Defined in:
- lib/grape/api.rb,
lib/grape.rb,
lib/grape/api/helpers.rb,
lib/grape/validations/validators/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
- .reset! ⇒ Object
-
.scope(name = nil, &block) ⇒ Object
Create a scope without affecting the URL.
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 DSL::Configuration
desc_container, stacked_hash_to_hash
Constructor Details
#initialize ⇒ API
Returns a new instance of API.
87 88 89 90 91 92 93 94 95 |
# File 'lib/grape/api.rb', line 87 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.
9 10 11 |
# File 'lib/grape/api.rb', line 9 def instance @instance end |
Class Method Details
.call(env) ⇒ Object
27 28 29 30 |
# File 'lib/grape/api.rb', line 27 def call(env) LOCK.synchronize { compile } unless instance call!(env) end |
.call!(env) ⇒ Object
32 33 34 |
# File 'lib/grape/api.rb', line 32 def call!(env) instance.call(env) end |
.cascade(value = nil) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/grape/api.rb', line 45 def cascade(value = nil) if value.nil? inheritable_setting.namespace_inheritable.keys.include?(:cascade) ? !!namespace_inheritable(:cascade) : true else namespace_inheritable(:cascade, value) end end |
.change! ⇒ Object
23 24 25 |
# File 'lib/grape/api.rb', line 23 def change! @instance = nil end |
.compile ⇒ Object
19 20 21 |
# File 'lib/grape/api.rb', line 19 def compile @instance ||= new end |
.reset! ⇒ Object
12 13 14 15 16 17 |
# File 'lib/grape/api.rb', line 12 def reset! @route_set = Rack::Mount::RouteSet.new @endpoints = [] @routes = nil reset_validations! end |
.scope(name = nil, &block) ⇒ Object
Create a scope without affecting the URL.
39 40 41 42 43 |
# File 'lib/grape/api.rb', line 39 def scope(name = nil, &block) within_namespace do nest(block) end end |
Instance Method Details
#call(env) ⇒ Object
97 98 99 100 101 |
# File 'lib/grape/api.rb', line 97 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.
111 112 113 114 115 |
# File 'lib/grape/api.rb', line 111 def cascade? return !!self.class.namespace_inheritable(:cascade) if self.class.inheritable_setting.namespace_inheritable.keys.include?(:cascade) return !!self.class.namespace_inheritable(:version_options)[:cascade] if self.class.namespace_inheritable(:version_options) && self.class.namespace_inheritable(:version_options).key?(:cascade) true end |