Class: Grape::API::Instance

Inherits:
Object
  • Object
show all
Includes:
DSL::API
Defined in:
lib/grape/api/instance.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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstance

Builds the routes from the defined endpoints, effectively compiling this API into a usable form.



118
119
120
121
122
123
124
125
126
127
# File 'lib/grape/api/instance.rb', line 118

def initialize
  @router = Router.new
  add_head_not_allowed_methods_and_options_methods
  self.class.endpoints.each do |endpoint|
    endpoint.mount_in(@router)
  end

  @router.compile!
  @router.freeze
end

Class Attribute Details

.baseObject

Returns the value of attribute base.



12
13
14
# File 'lib/grape/api/instance.rb', line 12

def base
  @base
end

.configurationObject

Returns the value of attribute configuration.



13
14
15
# File 'lib/grape/api/instance.rb', line 13

def configuration
  @configuration
end

.instanceObject (readonly)

Returns the value of attribute instance.



11
12
13
# File 'lib/grape/api/instance.rb', line 11

def instance
  @instance
end

Instance Attribute Details

#routerObject (readonly)

Returns the value of attribute router.



114
115
116
# File 'lib/grape/api/instance.rb', line 114

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.



50
51
52
53
# File 'lib/grape/api/instance.rb', line 50

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

.call!(env) ⇒ Object

A non-synchronized version of ::call.



56
57
58
# File 'lib/grape/api/instance.rb', line 56

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.



61
62
63
64
65
66
67
# File 'lib/grape/api/instance.rb', line 61

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.



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

def change!
  @instance = nil
end

.compileObject

Parses the API’s definition and compiles it into an instance of Grape::API.



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

def compile
  @instance ||= new
end

.recognize_path(path) ⇒ Object

see Grape::Router#recognize_path



70
71
72
73
# File 'lib/grape/api/instance.rb', line 70

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.



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

def reset!
  reset_endpoints!
  reset_routes!
  reset_validations!
end

.to_sObject



20
21
22
# File 'lib/grape/api/instance.rb', line 20

def to_s
  (base && base.to_s) || super
end

Instance Method Details

#call(env) ⇒ Object

Handle a request. See Rack documentation for what ‘env` is.



130
131
132
133
134
# File 'lib/grape/api/instance.rb', line 130

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.

Returns:



144
145
146
147
148
# File 'lib/grape/api/instance.rb', line 144

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