Class: Grape::API
- Inherits:
-
Object
- Object
- Grape::API
- Extended by:
- ActiveSupport::Autoload
- 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 =
A class-level lock to ensure the API is not compiled by multiple threads simultaneously within the same process.
Mutex.new
- Boolean =
Virtus::Attribute::Boolean
Class Attribute Summary collapse
-
.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
-
.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.
-
.recognize_path(path) ⇒ Object
see Grape::Router#recognize_path.
-
.reset! ⇒ Object
Clears all defined routes, endpoints, etc., on this API.
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 ⇒ API
constructor
Builds the routes from the defined endpoints, effectively compiling this API into a usable form.
Constructor Details
#initialize ⇒ API
Builds the routes from the defined endpoints, effectively compiling this API into a usable form.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/grape/api.rb', line 99 def initialize @router = Router.new self.class.endpoints.each do |endpoint| endpoint.mount_in(@router) end @router.compile! @router.freeze end |
Class Attribute Details
.instance ⇒ Object (readonly)
Returns the value of attribute instance.
10 11 12 |
# File 'lib/grape/api.rb', line 10 def instance @instance end |
Instance Attribute Details
#router ⇒ Object (readonly)
Returns the value of attribute router.
95 96 97 |
# File 'lib/grape/api.rb', line 95 def router @router end |
Class Method Details
.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.
38 39 40 41 |
# File 'lib/grape/api.rb', line 38 def call(env) LOCK.synchronize { compile } unless instance call!(env) end |
.call!(env) ⇒ Object
A non-synchronized version of ::call.
44 45 46 |
# File 'lib/grape/api.rb', line 44 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.
49 50 51 52 53 54 55 |
# File 'lib/grape/api.rb', line 49 def cascade(value = nil) if value.nil? inheritable_setting.namespace_inheritable.keys.include?(: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.
30 31 32 |
# File 'lib/grape/api.rb', line 30 def change! @instance = nil end |
.compile ⇒ Object
Parses the API’s definition and compiles it into an instance of Grape::API.
25 26 27 |
# File 'lib/grape/api.rb', line 25 def compile @instance ||= new end |
.recognize_path(path) ⇒ Object
see Grape::Router#recognize_path
58 59 60 61 |
# File 'lib/grape/api.rb', line 58 def recognize_path(path) LOCK.synchronize { compile } unless instance instance.router.recognize_path(path) end |
.reset! ⇒ Object
Clears all defined routes, endpoints, etc., on this API.
17 18 19 20 21 |
# File 'lib/grape/api.rb', line 17 def reset! reset_endpoints! reset_routes! reset_validations! end |
Instance Method Details
#call(env) ⇒ Object
Handle a request. See Rack documentation for what ‘env` is.
111 112 113 114 115 |
# File 'lib/grape/api.rb', line 111 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.
125 126 127 128 129 |
# File 'lib/grape/api.rb', line 125 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 |