class Grape::API
The API
class is the primary entry point for creating Grape
APIs. Users should subclass this class in order to build an API
.
Constants
Attributes
Public Class Methods
Source
# File lib/grape/api.rb, line 68 def configure config = @base_instance.configuration if block_given? yield config self else config end end
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.
Source
# 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
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
Source
# 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
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
Source
# 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
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.
Source
# 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
Redefines all methods so that are forwarded to add_setup
and be recorded
Private Class Methods
Source
# File lib/grape/api.rb, line 109 def add_setup(step) @setup << step last_response = nil @instances.each do |instance| last_response = replay_step_on(instance, **step) end refresh_mount_step if step[:method] != :mount last_response end
Adds a new stage to the set up require to get a Grape::API
up and running
Source
# File lib/grape/api.rb, line 151 def any_lazy?(args) args.any? { |argument| argument.try(:lazy?) } end
Source
# File lib/grape/api.rb, line 155 def evaluate_arguments(configuration, *args) args.map do |argument| if argument.try(:lazy?) argument.evaluate_from(configuration) elsif argument.is_a?(Hash) argument.transform_values { |value| evaluate_arguments(configuration, value).first } elsif argument.is_a?(Array) evaluate_arguments(configuration, *argument) else argument end end end
Source
# File lib/grape/api.rb, line 100 def instance_for_rack if never_mounted? base_instance else mounted_instances.first end end
Source
# File lib/grape/api.rb, line 173 def mounted_instances instances - [base_instance] end
Source
# File lib/grape/api.rb, line 169 def never_mounted? mounted_instances.empty? end
Source
# File lib/grape/api.rb, line 121 def refresh_mount_step @setup.each do |setup_step| next if setup_step[:method] != :mount refresh_mount_step = setup_step.merge(method: :refresh_mounted_api) @setup << refresh_mount_step @instances.each do |instance| replay_step_on(instance, **refresh_mount_step) end end end
Updating all previously mounted classes in the case that new methods have been executed.
Source
# File lib/grape/api.rb, line 94 def replay_setup_on(instance) @setup.each do |setup_step| replay_step_on(instance, **setup_step) end end
Replays the set up to produce an API
as defined in this class, can be called on classes that inherit from Grape::API
Source
# File lib/grape/api.rb, line 133 def replay_step_on(instance, method:, args:, block:) return if skip_immediate_run?(instance, args) eval_args = evaluate_arguments(instance.configuration, *args) response = instance.send(method, *eval_args, &block) if skip_immediate_run?(instance, [response]) response else evaluate_arguments(instance.configuration, response).first end end
Source
# File lib/grape/api.rb, line 146 def skip_immediate_run?(instance, args) instance.base_instance? && (any_lazy?(args) || args.any? { |arg| arg.is_a?(Hash) && any_lazy?(arg.values) }) end
Skips steps that contain arguments to be lazily executed (on re-mount time)