Class: Grape::API

Inherits:
Object
  • Object
show all
Extended by:
Middleware::Auth::DSL
Includes:
DSL::Callbacks, DSL::Configuration, DSL::Helpers, DSL::Middleware, DSL::RequestResponse, DSL::Routing, DSL::Validations
Defined in:
lib/grape/api.rb,
lib/grape.rb,
lib/grape/api/helpers.rb,
lib/grape/validations/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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware::Auth::DSL

auth, http_basic, http_digest

Constructor Details

#initializeAPI

Returns a new instance of API.



118
119
120
121
122
123
124
125
# 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

Class Attribute Details

.instanceObject (readonly)

Returns the value of attribute instance.



17
18
19
# File 'lib/grape/api.rb', line 17

def instance
  @instance
end

Class Method Details

.call(env) ⇒ Object



37
38
39
40
# File 'lib/grape/api.rb', line 37

def call(env)
  LOCK.synchronize { compile } unless instance
  call!(env)
end

.call!(env) ⇒ Object



42
43
44
# File 'lib/grape/api.rb', line 42

def call!(env)
  instance.call(env)
end

.cascade(value = nil) ⇒ Object



53
54
55
56
57
58
59
# 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!Object



33
34
35
# File 'lib/grape/api.rb', line 33

def change!
  @instance = nil
end

.compileObject



29
30
31
# File 'lib/grape/api.rb', line 29

def compile
  @instance ||= new
end

.imbue(key, value) ⇒ Object

Add to a configuration value for this namespace.

Parameters:

  • key (Symbol)

    The key of the configuration variable.

  • value (Object)

    The value to which to set the configuration variable.



74
75
76
# File 'lib/grape/api.rb', line 74

def imbue(key, value)
  settings.imbue(key, value)
end

.reset!Object



21
22
23
24
25
26
27
# 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) ⇒ Object

Create a scope without affecting the URL.

Parameters:

  • name (Symbol) (defaults to: nil)

    Purely placebo, just allows to to name the scope to make the code more readable.



49
50
51
# File 'lib/grape/api.rb', line 49

def scope(name = nil, &block)
  nest(block)
end

.set(key, value) ⇒ Object

Set a configuration value for this namespace.

Parameters:

  • key (Symbol)

    The key of the configuration variable.

  • value (Object)

    The value to which to set the configuration variable.



65
66
67
# File 'lib/grape/api.rb', line 65

def set(key, value)
  settings[key.to_sym] = value
end

Instance Method Details

#call(env) ⇒ Object



127
128
129
130
131
# 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?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.

Returns:



141
142
143
144
145
# 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