Class: Grape::API::Instance

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, DSL::Callbacks, DSL::Desc, DSL::Helpers, DSL::Logger, DSL::Middleware, DSL::RequestResponse, DSL::Routing, DSL::Settings, DSL::Validations, Middleware::Auth::DSL
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

Boolean =
Grape::API::Boolean
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

Attributes included from DSL::Settings

#inheritable_setting

Attributes included from DSL::Routing

#endpoints

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Settings

global_setting, namespace_setting, route_setting, top_level_setting

Methods included from DSL::Desc

desc

Methods included from DSL::Validations

contract, params

Methods included from DSL::Logger

logger

Methods included from DSL::Middleware

middleware, use

Methods included from DSL::RequestResponse

content_type, content_types, default_error_formatter, default_error_status, default_format, error_formatter, format, formatter, parser, represent, rescue_from

Methods included from DSL::Routing

build_with, cascade, do_not_document!, do_not_route_head!, do_not_route_options!, given, lint!, mount, mounted, namespace, prefix, route, route_param, routes, scope, version, versions

Methods included from DSL::Helpers

helpers

Methods included from Middleware::Auth::DSL

auth, http_basic, http_digest

Constructor Details

#initializeInstance

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



102
103
104
105
106
107
108
109
110
111
# File 'lib/grape/api/instance.rb', line 102

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

.configurationObject

Returns the value of attribute configuration.



24
25
26
# File 'lib/grape/api/instance.rb', line 24

def configuration
  @configuration
end

Instance Attribute Details

#routerObject (readonly)

Returns the value of attribute router.



98
99
100
# File 'lib/grape/api/instance.rb', line 98

def router
  @router
end

Class Method Details

.base=(grape_api) ⇒ Object



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

def base=(grape_api)
  @base = grape_api
  grape_api.instances << self
end

.base_instance?Boolean

Returns:



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

def base_instance?
  self == @base.base_instance
end

.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.



52
53
54
55
# File 'lib/grape/api/instance.rb', line 52

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

.change!Object

Wipe the compiled API so we can recompile after changes were made.



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

def change!
  @instance = nil
end

.compile!Object



57
58
59
60
61
# File 'lib/grape/api/instance.rb', line 57

def compile!
  return if @instance

  LOCK.synchronize { @instance ||= new }
end

.recognize_path(path) ⇒ Object

see Grape::Router#recognize_path



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

def recognize_path(path)
  compile!
  @instance.router.recognize_path(path)
end

.reset!Object

Clears all defined routes, endpoints, etc., on this API.



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

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.



114
115
116
117
118
119
120
121
122
# File 'lib/grape/api/instance.rb', line 114

def call(env)
  status, headers, response = @router.call(env)
  unless cascade?
    headers = Grape::Util::Header.new.merge(headers)
    headers.delete('X-Cascade')
  end

  [status, headers, response]
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:



132
133
134
135
136
137
138
# File 'lib/grape/api/instance.rb', line 132

def cascade?
  namespace_inheritable = self.class.inheritable_setting.namespace_inheritable
  return namespace_inheritable[:cascade] if namespace_inheritable.key?(:cascade)
  return namespace_inheritable[:version_options][:cascade] if namespace_inheritable[:version_options]&.key?(:cascade)

  true
end