The API class is the primary entry point for creating Grape APIs.Users should subclass this class in order to build an API.
# File lib/grape/api.rb, line 37 def call(env) LOCK.synchronize { compile } unless instance call!(env) end
# File lib/grape/api.rb, line 42 def call!(env) instance.call(env) end
# 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
Add to a configuration value for this namespace.
@param key [Symbol] The key of the configuration variable. @param value [Object] The value to which to set the configuration variable.
# File lib/grape/api.rb, line 74 def imbue(key, value) settings.imbue(key, value) end
# File lib/grape/api.rb, line 118 def initialize @route_set = Rack::Mount::RouteSet.new add_head_not_allowed_methods_and_options_methods self.class.endpoints.each do |endpoint| endpoint.mount_in(@route_set) end @route_set.freeze end
# 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
Create a scope without affecting the URL.
@param name [Symbol] Purely placebo, just allows to to name the scope to make the code more readable.
# File lib/grape/api.rb, line 49 def scope(name = nil, &block) nest(block) end
Set a configuration value for this namespace.
@param key [Symbol] The key of the configuration variable. @param value [Object] The value to which to set the configuration variable.
# File lib/grape/api.rb, line 65 def set(key, value) settings[key.to_sym] = value end
# File lib/grape/api.rb, line 109 def inherit_settings(other_stack) settings.prepend other_stack endpoints.each do |e| e.settings.prepend(other_stack) e.options[:app].inherit_settings(other_stack) if e.options[:app].respond_to?(:inherit_settings, true) end end
# File lib/grape/api.rb, line 104 def inherited(subclass) subclass.reset! subclass.logger = logger.clone end
Execute first the provided block, then each of the block passed in. Allows for simple ‘before’ setups of settings stack pushes.
# File lib/grape/api.rb, line 91 def nest(*blocks, &block) blocks.reject! { |b| b.nil? } if blocks.any? settings.push # create a new context to eval the follow instance_eval(&block) if block_given? blocks.each { |b| instance_eval(&b) } settings.pop # when finished, we pop the context reset_validations! else instance_eval(&block) end end
# 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
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.
# 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
Generated with the Darkfish Rdoc Generator 2.