Class: Grape::API

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/api.rb,
lib/grape/api/helpers.rb,
lib/grape/api/instance.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 Classes: Boolean, Instance

Constant Summary collapse

NON_OVERRIDABLE =

Class methods that we want to call on the API rather than on the API object

%i[call call! configuration compile! inherited].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.base_instanceObject

Returns the value of attribute base_instance.



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

def base_instance
  @base_instance
end

.instancesObject

Returns the value of attribute instances.



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

def instances
  @instances
end

Class Method Details

.callObject

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. NOTE: This will only be called on an API directly mounted on RACK



77
78
79
# File 'lib/grape/api.rb', line 77

def call(...)
  instance_for_rack.call(...)
end

.compile!Object



127
128
129
# File 'lib/grape/api.rb', line 127

def compile!
  instance_for_rack.compile! # See API::Instance.compile!
end

.configureObject

Configure an API from the outside. If a block is given, it’ll pass a configuration hash to the block which you can use to configure your API. If no block is given, returns the configuration hash. The configuration set here is accessible from inside an API with ‘configuration` as normal.



62
63
64
65
66
67
68
69
70
# File 'lib/grape/api.rb', line 62

def configure
  config = @base_instance.configuration
  if block_given?
    yield config
    self
  else
    config
  end
end

.const_missing(*args) ⇒ Object

Alleviates problems with autoloading by tring to search for the constant



82
83
84
85
86
87
88
# File 'lib/grape/api.rb', line 82

def const_missing(*args)
  if base_instance.const_defined?(*args)
    base_instance.const_get(*args)
  else
    super
  end
end

.inherited(api) ⇒ Object

When inherited, will create a list of all instances (times the API was mounted) It will listen to the setup required to mount that endpoint, and replicate it on any new instance



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

def inherited(api)
  super

  api.initial_setup(self == Grape::API ? Grape::API::Instance : @base_instance)
  api.override_all_methods!
end

.initial_setup(base_instance_parent) ⇒ Object

Initialize the instance variables on the remountable class, and the base_instance an instance that will be used to create the set up but will not be mounted



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

def initial_setup(base_instance_parent)
  @instances = []
  @setup = Set.new
  @base_parent = base_instance_parent
  @base_instance = mount_instance
end

.method_missing(method, *args, &block) ⇒ Object



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

def method_missing(method, *args, &block)
  # If there's a missing method, it may be defined on the base_instance instead.
  if respond_to_missing?(method)
    base_instance.send(method, *args, &block)
  else
    super
  end
end

.mount_instance(**opts) ⇒ Object

The remountable class can have a configuration hash to provide some dynamic class-level variables. For instance, a description could be done using: ‘desc configuration` if it may vary depending on where the endpoint is mounted. Use with care, if you find yourself using configuration too much, you may actually want to provide a new API rather than remount it.



94
95
96
97
98
99
100
# File 'lib/grape/api.rb', line 94

def mount_instance(**opts)
  instance = Class.new(@base_parent)
  instance.configuration = Grape::Util::EndpointConfiguration.new(opts[:configuration] || {})
  instance.base = self
  replay_setup_on(instance)
  instance
end

.newObject

Rather than initializing an object of type Grape::API, create an object of type Instance



26
27
28
# File 'lib/grape/api.rb', line 26

def new(...)
  base_instance.new(...)
end

.override_all_methods!Object

Redefines all methods so that are forwarded to add_setup and be recorded



49
50
51
52
53
54
55
# File 'lib/grape/api.rb', line 49

def override_all_methods!
  (base_instance.methods - Class.methods - NON_OVERRIDABLE).each do |method_override|
    define_singleton_method(method_override) do |*args, &block|
      add_setup(method_override, *args, &block)
    end
  end
end

.replay_setup_on(instance) ⇒ Object

Replays the set up to produce an API as defined in this class, can be called on classes that inherit from Grape::API



104
105
106
107
108
# File 'lib/grape/api.rb', line 104

def replay_setup_on(instance)
  @setup.each do |setup_step|
    replay_step_on(instance, setup_step)
  end
end

.respond_to?(method, include_private = false) ⇒ Boolean

Returns:



110
111
112
# File 'lib/grape/api.rb', line 110

def respond_to?(method, include_private = false)
  super || base_instance.respond_to?(method, include_private)
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:



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

def respond_to_missing?(method, include_private = false)
  base_instance.respond_to?(method, include_private)
end