Class: Grape::API

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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 recognize_path].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.base_instanceObject

Returns the value of attribute base_instance.



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

def base_instance
  @base_instance
end

.instancesObject

Returns the value of attribute instances.



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

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.



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

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



38
39
40
41
42
43
# File 'lib/grape/api.rb', line 38

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



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

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.



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

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



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

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: method_override, args: args, block: block)
    end
  end
end