Class: Grape::Endpoint
Overview
An Endpoint is the proxy scope in which all routing blocks are executed. In other words, any methods on the instance level of this class may be called from inside a ‘get`, `post`, etc.
Instance Attribute Summary collapse
#inheritable_setting, #top_level_setting
Class Method Summary
collapse
Instance Method Summary
collapse
#body, #configuration, #content_type, #cookies, #declared, #entity_class_for_obj, #entity_representation_for, #error!, #file, post_filter_methods, #present, #redirect, #return_no_content, #route, #status, #stream, #version
#header
#api_class_setting, #get_or_set, #global_setting, #namespace_end, #namespace_inheritable, #namespace_inheritable_to_nil, #namespace_reverse_stackable, #namespace_reverse_stackable_with_hash, #namespace_setting, #namespace_stackable, #namespace_stackable_with_hash, #namespace_start, #route_end, #route_setting, #unset, #unset_api_class_setting, #unset_global_setting, #unset_namespace_inheritable, #unset_namespace_setting, #unset_namespace_stackable, #unset_route_setting, #within_namespace
Constructor Details
#initialize(new_settings, options = {}) { ... } ⇒ Endpoint
Note:
This happens at the time of API definition, so in this context the
Create a new endpoint. endpoint does not know if it will be mounted under a different endpoint.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/grape/endpoint.rb', line 77
def initialize(new_settings, options = {}, &block)
require_option(options, :path)
require_option(options, :method)
self.inheritable_setting = new_settings.point_in_time_copy
route_setting(:saved_declared_params, namespace_stackable(:declared_params))
route_setting(:saved_validations, namespace_stackable(:validations))
namespace_stackable(:representations, []) unless namespace_stackable(:representations)
namespace_inheritable(:default_error_status, 500) unless namespace_inheritable(:default_error_status)
@options = options
@options[:path] = Array(options[:path])
@options[:path] << '/' if options[:path].empty?
@options[:method] = Array(options[:method])
@options[:route_options] ||= {}
@lazy_initialize_lock = Mutex.new
@lazy_initialized = nil
@block = nil
@status = nil
@file = nil
@body = nil
@proc = nil
return unless block_given?
@source = block
@block = self.class.generate_api_method(method_name, &block)
end
|
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
12
13
14
|
# File 'lib/grape/endpoint.rb', line 12
def block
@block
end
|
#env ⇒ Object
Returns the value of attribute env.
13
14
15
|
# File 'lib/grape/endpoint.rb', line 13
def env
@env
end
|
Returns the value of attribute headers.
13
14
15
|
# File 'lib/grape/endpoint.rb', line 13
def
@headers
end
|
#options ⇒ Object
Returns the value of attribute options.
12
13
14
|
# File 'lib/grape/endpoint.rb', line 12
def options
@options
end
|
#params ⇒ Object
Returns the value of attribute params.
13
14
15
|
# File 'lib/grape/endpoint.rb', line 13
def params
@params
end
|
#request ⇒ Object
Returns the value of attribute request.
13
14
15
|
# File 'lib/grape/endpoint.rb', line 13
def request
@request
end
|
#source ⇒ Object
Returns the value of attribute source.
12
13
14
|
# File 'lib/grape/endpoint.rb', line 12
def source
@source
end
|
Class Method Details
.before_each(new_setup = false, &block) ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/grape/endpoint.rb', line 20
def before_each(new_setup = false, &block)
@before_each ||= []
if new_setup == false
return @before_each unless block_given?
@before_each << block
else
@before_each = [new_setup]
end
end
|
.generate_api_method(method_name, &block) ⇒ Proc
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
Create an UnboundMethod that is appropriate for executing an endpoint route.
The unbound method allows explicit calls to return
without raising a LocalJumpError
. The method will be removed, but a Proc
reference to it will be returned. The returned Proc
expects a single argument: the instance of Endpoint
to bind to the method during the call.
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/grape/endpoint.rb', line 48
def generate_api_method(method_name, &block)
if method_defined?(method_name)
raise NameError.new("method #{method_name.inspect} already exists and cannot be used as an unbound method name")
end
define_method(method_name, &block)
method = instance_method(method_name)
remove_method(method_name)
proc do |endpoint_instance|
ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: endpoint_instance) do
method.bind(endpoint_instance).call
end
end
end
|
.new(*args, &block) ⇒ Object
16
17
18
|
# File 'lib/grape/endpoint.rb', line 16
def new(*args, &block)
self == Endpoint ? Class.new(Endpoint).new(*args, &block) : super
end
|
.run_before_each(endpoint) ⇒ Object
30
31
32
33
|
# File 'lib/grape/endpoint.rb', line 30
def run_before_each(endpoint)
superclass.run_before_each(endpoint) unless self == Endpoint
before_each.each { |blk| blk.call(endpoint) if blk.respond_to?(:call) }
end
|
Instance Method Details
#call(env) ⇒ Object
221
222
223
224
|
# File 'lib/grape/endpoint.rb', line 221
def call(env)
lazy_initialize!
dup.call!(env)
end
|
#call!(env) ⇒ Object
226
227
228
229
230
|
# File 'lib/grape/endpoint.rb', line 226
def call!(env)
env[Grape::Env::API_ENDPOINT] = self
@env = env
@app.call(env)
end
|
#endpoints ⇒ Object
Return the collection of endpoints within this endpoint. This is the case when an Grape::API mounts another Grape::API.
234
235
236
|
# File 'lib/grape/endpoint.rb', line 234
def endpoints
options[:app].endpoints if options[:app] && options[:app].respond_to?(:endpoints)
end
|
#equals?(e) ⇒ Boolean
238
239
240
|
# File 'lib/grape/endpoint.rb', line 238
def equals?(e)
(options == e.options) && (inheritable_setting.to_hash == e.inheritable_setting.to_hash)
end
|
#inherit_settings(namespace_stackable) ⇒ Object
Update our settings from a given set of stackable parameters. Used when the endpoint’s API is mounted under another one.
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/grape/endpoint.rb', line 114
def inherit_settings(namespace_stackable)
inheritable_setting.route[:saved_validations] += namespace_stackable[:validations]
parent_declared_params = namespace_stackable[:declared_params]
if parent_declared_params
inheritable_setting.route[:declared_params] ||= []
inheritable_setting.route[:declared_params].concat(parent_declared_params.flatten)
end
endpoints && endpoints.each { |e| e.inherit_settings(namespace_stackable) }
end
|
#map_routes ⇒ Object
208
209
210
|
# File 'lib/grape/endpoint.rb', line 208
def map_routes
options[:method].map { |method| options[:path].map { |path| yield method, path } }
end
|
#merge_route_options(**default) ⇒ Object
204
205
206
|
# File 'lib/grape/endpoint.rb', line 204
def merge_route_options(**default)
options[:route_options].clone.merge!(**default)
end
|
#method_name ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/grape/endpoint.rb', line 130
def method_name
[options[:method],
Namespace.joined_space(namespace_stackable(:namespace)),
(namespace_stackable(:mount_path) || []).join('/'),
options[:path].join('/')]
.join(' ')
end
|
#mount_in(router) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/grape/endpoint.rb', line 148
def mount_in(router)
if endpoints
endpoints.each { |e| e.mount_in(router) }
else
reset_routes!
routes.each do |route|
methods = [route.request_method]
if !namespace_inheritable(:do_not_route_head) && route.request_method == Grape::Http::Headers::GET
methods << Grape::Http::Headers::HEAD
end
methods.each do |method|
unless route.request_method == method
route = Grape::Router::Route.new(method, route.origin, **route.attributes.to_h)
end
router.append(route.apply(self))
end
end
end
end
|
#namespace ⇒ Object
217
218
219
|
# File 'lib/grape/endpoint.rb', line 217
def namespace
@namespace ||= Namespace.joined_space_path(namespace_stackable(:namespace))
end
|
#prepare_default_route_attributes ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/grape/endpoint.rb', line 186
def prepare_default_route_attributes
{
namespace: namespace,
version: prepare_version,
requirements: prepare_routes_requirements,
prefix: namespace_inheritable(:root_prefix),
anchor: options[:route_options].fetch(:anchor, true),
settings: inheritable_setting.route.except(:saved_declared_params, :saved_validations),
forward_match: options[:forward_match]
}
end
|
#prepare_path(path) ⇒ Object
212
213
214
215
|
# File 'lib/grape/endpoint.rb', line 212
def prepare_path(path)
path_settings = inheritable_setting.to_hash[:namespace_stackable].merge(inheritable_setting.to_hash[:namespace_inheritable])
Path.prepare(path, namespace, path_settings)
end
|
#prepare_routes_requirements ⇒ Object
178
179
180
181
182
183
184
|
# File 'lib/grape/endpoint.rb', line 178
def prepare_routes_requirements
endpoint_requirements = options[:route_options][:requirements] || {}
all_requirements = (namespace_stackable(:namespace).map(&:requirements) << endpoint_requirements)
all_requirements.reduce({}) do |base_requirements, single_requirements|
base_requirements.merge!(single_requirements)
end
end
|
#prepare_version ⇒ Object
198
199
200
201
202
|
# File 'lib/grape/endpoint.rb', line 198
def prepare_version
version = namespace_inheritable(:version) || []
return if version.empty?
version.length == 1 ? version.first.to_s : version
end
|
#require_option(options, key) ⇒ Object
126
127
128
|
# File 'lib/grape/endpoint.rb', line 126
def require_option(options, key)
raise Grape::Exceptions::MissingOption.new(key) unless options.key?(key)
end
|
#reset_routes! ⇒ Object
142
143
144
145
146
|
# File 'lib/grape/endpoint.rb', line 142
def reset_routes!
endpoints.each(&:reset_routes!) if endpoints
@namespace = nil
@routes = nil
end
|
#routes ⇒ Object
138
139
140
|
# File 'lib/grape/endpoint.rb', line 138
def routes
@routes ||= endpoints ? endpoints.collect(&:routes).flatten : to_routes
end
|
#to_routes ⇒ Object
168
169
170
171
172
173
174
175
176
|
# File 'lib/grape/endpoint.rb', line 168
def to_routes
route_options = prepare_default_route_attributes
map_routes do |method, path|
path = prepare_path(path)
params = merge_route_options(**route_options.merge(suffix: path.suffix))
route = Router::Route.new(method, path.path, **params)
route.apply(self)
end.flatten
end
|