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[base= base_instance? call change! configuration compile! inherit_settings recognize_path reset! routes top_level_setting= top_level_setting].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.



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

def base_instance
  @base_instance
end

.instancesObject

Returns the value of attribute instances.



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

def instances
  @instances
end

Class Method Details

.configure {|config| ... } ⇒ Object

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.

Yields:

  • (config)


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

def configure
  config = @base_instance.configuration
  return config unless block_given?

  yield config
  self
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



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

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.



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

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

.override_all_methods!Object

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



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

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