Parent

Namespace

Class/Module Index [+]

Quicksearch

Grape::API

The API class is the primary entry point for creating Grape APIs.Users should subclass this class in order to build an API.

Constants

Boolean
LOCK

Attributes

instance[R]

Public Class Methods

call(env) click to toggle source
# File lib/grape/api.rb, line 37
def call(env)
  LOCK.synchronize { compile } unless instance
  call!(env)
end
call!(env) click to toggle source
# File lib/grape/api.rb, line 42
def call!(env)
  instance.call(env)
end
cascade(value = nil) click to toggle source
# 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!() click to toggle source
# File lib/grape/api.rb, line 33
def change!
  @instance = nil
end
compile() click to toggle source
# File lib/grape/api.rb, line 29
def compile
  @instance ||= new
end
imbue(key, value) click to toggle source

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
new() click to toggle source
# 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
reset!() click to toggle source
# 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) click to toggle source

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(key, value) click to toggle source

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

Protected Class Methods

inherit_settings(other_stack) click to toggle source
# 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
inherited(subclass) click to toggle source
# File lib/grape/api.rb, line 104
def inherited(subclass)
  subclass.reset!
  subclass.logger = logger.clone
end
nest(*blocks, &block) click to toggle source

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
prepare_routes() click to toggle source
# File lib/grape/api.rb, line 80
def prepare_routes
  routes = []
  endpoints.each do |endpoint|
    routes.concat(endpoint.routes)
  end
  routes
end

Public Instance Methods

call(env) click to toggle source
# 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?() click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.