Class: Grape::API
- Inherits:
-
Object
- Object
- Grape::API
- 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
Constant Summary collapse
- NON_OVERRIDABLE =
Class methods that we want to call on the API rather than on the API object
i[base 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
-
.base_instance ⇒ Object
Returns the value of attribute base_instance.
-
.instances ⇒ Object
Returns the value of attribute instances.
Class Method Summary collapse
-
.configure {|config| ... } ⇒ Object
Configure an API from the outside.
-
.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.
-
.mount_instance(configuration: nil) ⇒ Object
The remountable class can have a configuration hash to provide some dynamic class-level variables.
-
.override_all_methods! ⇒ Object
Redefines all methods so that are forwarded to add_setup and be recorded.
Class Attribute Details
.base_instance ⇒ Object
Returns the value of attribute base_instance.
29 30 31 |
# File 'lib/grape/api.rb', line 29 def base_instance @base_instance end |
.instances ⇒ Object
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.
67 68 69 70 71 72 73 |
# File 'lib/grape/api.rb', line 67 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
46 47 48 49 50 51 |
# File 'lib/grape/api.rb', line 46 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.
79 80 81 82 83 84 85 |
# File 'lib/grape/api.rb', line 79 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
54 55 56 57 58 59 60 |
# File 'lib/grape/api.rb', line 54 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 |