Class: Grape::API::Instance
- Inherits:
-
Object
- Object
- Grape::API::Instance
- Includes:
- DSL::API
- Defined in:
- lib/grape/api/instance.rb,
lib/grape/api.rb
Overview
The API Instance class, is the engine behind Grape::API. Each class that inherits from this will represent a different API instance
Constant Summary collapse
- Boolean =
Grape::API::Boolean
- LOCK =
A class-level lock to ensure the API is not compiled by multiple threads simultaneously within the same process.
Mutex.new
Class Attribute Summary collapse
-
.base ⇒ Object
Returns the value of attribute base.
-
.configuration ⇒ Object
Returns the value of attribute configuration.
-
.instance ⇒ Object
readonly
Returns the value of attribute instance.
Instance Attribute Summary collapse
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Class Method Summary collapse
- .base_instance? ⇒ Boolean
-
.call(env) ⇒ Object
This is the interface point between Rack and Grape; it accepts a request from Rack and ultimately returns an array of three values: the status, the headers, and the body.
-
.call!(env) ⇒ Object
A non-synchronized version of ::call.
-
.cascade(value = nil) ⇒ Object
Some requests may return a HTTP 404 error if grape cannot find a matching route.
-
.change! ⇒ Object
Wipe the compiled API so we can recompile after changes were made.
-
.compile ⇒ Object
Parses the API’s definition and compiles it into an instance of Grape::API.
- .compile! ⇒ Object
- .given(conditional_option, &block) ⇒ Object
- .mounted(&block) ⇒ Object
-
.recognize_path(path) ⇒ Object
see Grape::Router#recognize_path.
-
.reset! ⇒ Object
Clears all defined routes, endpoints, etc., on this API.
- .to_s ⇒ Object
Instance Method Summary collapse
-
#call(env) ⇒ Object
Handle a request.
-
#cascade? ⇒ Boolean
Some requests may return a HTTP 404 error if grape cannot find a matching route.
-
#initialize ⇒ Instance
constructor
Builds the routes from the defined endpoints, effectively compiling this API into a usable form.
Constructor Details
#initialize ⇒ Instance
Builds the routes from the defined endpoints, effectively compiling this API into a usable form.
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/grape/api/instance.rb', line 152 def initialize @router = Router.new self.class.endpoints.each do |endpoint| endpoint.mount_in(@router) end @router.compile! @router.freeze end |
Class Attribute Details
.base ⇒ Object
Returns the value of attribute base.
13 14 15 |
# File 'lib/grape/api/instance.rb', line 13 def base @base end |
.configuration ⇒ Object
Returns the value of attribute configuration.
14 15 16 |
# File 'lib/grape/api/instance.rb', line 14 def configuration @configuration end |
.instance ⇒ Object (readonly)
Returns the value of attribute instance.
13 14 15 |
# File 'lib/grape/api/instance.rb', line 13 def instance @instance end |
Instance Attribute Details
#router ⇒ Object (readonly)
Returns the value of attribute router.
148 149 150 |
# File 'lib/grape/api/instance.rb', line 148 def router @router end |
Class Method Details
.base_instance? ⇒ Boolean
33 34 35 |
# File 'lib/grape/api/instance.rb', line 33 def base_instance? self == base.base_instance end |
.call(env) ⇒ Object
This is the interface point between Rack and Grape; it accepts a request from Rack and ultimately returns an array of three values: the status, the headers, and the body. See [the rack specification] (www.rubydoc.info/github/rack/rack/master/file/SPEC) for more.
63 64 65 66 |
# File 'lib/grape/api/instance.rb', line 63 def call(env) compile! call!(env) end |
.call!(env) ⇒ Object
A non-synchronized version of ::call.
69 70 71 |
# File 'lib/grape/api/instance.rb', line 69 def call!(env) instance.call(env) end |
.cascade(value = nil) ⇒ Object
Some requests may return a HTTP 404 error if grape cannot find a matching route. In this case, Grape::Router 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.
74 75 76 77 78 79 80 |
# File 'lib/grape/api/instance.rb', line 74 def cascade(value = nil) if value.nil? inheritable_setting.namespace_inheritable.key?(:cascade) ? !namespace_inheritable(:cascade).nil? : true else namespace_inheritable(:cascade, value) end end |
.change! ⇒ Object
Wipe the compiled API so we can recompile after changes were made.
55 56 57 |
# File 'lib/grape/api/instance.rb', line 55 def change! @instance = nil end |
.compile ⇒ Object
Parses the API’s definition and compiles it into an instance of Grape::API.
50 51 52 |
# File 'lib/grape/api/instance.rb', line 50 def compile @instance ||= new end |
.compile! ⇒ Object
82 83 84 85 86 |
# File 'lib/grape/api/instance.rb', line 82 def compile! return if instance LOCK.synchronize { compile unless instance } end |
.given(conditional_option, &block) ⇒ Object
16 17 18 |
# File 'lib/grape/api/instance.rb', line 16 def given(conditional_option, &block) evaluate_as_instance_with_configuration(block, lazy: true) if conditional_option && block end |
.mounted(&block) ⇒ Object
20 21 22 |
# File 'lib/grape/api/instance.rb', line 20 def mounted(&block) evaluate_as_instance_with_configuration(block, lazy: true) end |
.recognize_path(path) ⇒ Object
see Grape::Router#recognize_path
89 90 91 92 |
# File 'lib/grape/api/instance.rb', line 89 def recognize_path(path) compile! instance.router.recognize_path(path) end |
.reset! ⇒ Object
Clears all defined routes, endpoints, etc., on this API.
42 43 44 45 46 |
# File 'lib/grape/api/instance.rb', line 42 def reset! reset_endpoints! reset_routes! reset_validations! end |
.to_s ⇒ Object
29 30 31 |
# File 'lib/grape/api/instance.rb', line 29 def to_s base&.to_s || super end |
Instance Method Details
#call(env) ⇒ Object
Handle a request. See Rack documentation for what ‘env` is.
164 165 166 167 168 |
# File 'lib/grape/api/instance.rb', line 164 def call(env) result = @router.call(env) result[1].delete(Grape::Http::Headers::X_CASCADE) unless cascade? result end |
#cascade? ⇒ Boolean
Some requests may return a HTTP 404 error if grape cannot find a matching route. In this case, Grape::Router 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.
178 179 180 181 182 183 |
# File 'lib/grape/api/instance.rb', line 178 def cascade? return self.class.namespace_inheritable(:cascade) if self.class.inheritable_setting.namespace_inheritable.key?(:cascade) return self.class.namespace_inheritable(:version_options)[:cascade] if self.class.namespace_inheritable(:version_options)&.key?(:cascade) true end |