Class: Grape::API

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/grape/api.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

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 recognize_path routes].freeze
Helpers =
Grape::DSL::Helpers::BaseHelper

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

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



57
58
59
60
61
62
63
64
65
# File 'lib/grape/api.rb', line 57

def configure
  config = @base_instance.configuration
  if block_given?
    yield config
    self
  else
    config
  end
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



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

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

.mount_instance(configuration: nil) ⇒ 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.



71
72
73
74
75
76
77
# File 'lib/grape/api.rb', line 71

def mount_instance(configuration: nil)
  Class.new(@base_parent).tap do |instance|
    instance.configuration = Grape::Util::EndpointConfiguration.new(configuration || {})
    instance.base = self
    replay_setup_on(instance)
  end
end

.override_all_methods!Object

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



44
45
46
47
48
49
50
# File 'lib/grape/api.rb', line 44

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