Class: Grape::API

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Mountable
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.



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

def base_instance
  @base_instance
end

.instancesObject

Returns the value of attribute instances.



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

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)


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

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



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

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[:description] 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.



75
76
77
78
79
80
81
# File 'lib/grape/api.rb', line 75

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



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

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