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
#api_format, #body, #configuration, #content_type, #context, #declared, #entity_class_for_obj, #entity_representation_for, #error!, #http_version, post_filter_methods, #present, #redirect, #return_no_content, #route, #sendfile, #status, #stream, #version
#header
#global_setting, #namespace_setting, #route_setting
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.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/grape/endpoint.rb', line 49
def initialize(new_settings, options = {}, &block)
require_option(options, :path)
require_option(options, :method)
self.inheritable_setting = new_settings.point_in_time_copy
inheritable_setting.route[:declared_params] = inheritable_setting.namespace_stackable[:declared_params].flatten
inheritable_setting.route[:saved_validations] = inheritable_setting.namespace_stackable[:validations]
inheritable_setting.namespace_stackable[:representations] = [] unless inheritable_setting.namespace_stackable[:representations]
inheritable_setting.namespace_inheritable[:default_error_status] = 500 unless inheritable_setting.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
@status = nil
@stream = nil
@body = nil
@source = block
end
|
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
14
15
16
|
# File 'lib/grape/endpoint.rb', line 14
def env
@env
end
|
#options ⇒ Object
Returns the value of attribute options.
14
15
16
|
# File 'lib/grape/endpoint.rb', line 14
def options
@options
end
|
#request ⇒ Object
Returns the value of attribute request.
14
15
16
|
# File 'lib/grape/endpoint.rb', line 14
def request
@request
end
|
#source ⇒ Object
Returns the value of attribute source.
14
15
16
|
# File 'lib/grape/endpoint.rb', line 14
def source
@source
end
|
Class Method Details
.before_each(new_setup = false, &block) ⇒ Object
20
21
22
23
24
25
26
27
28
29
|
# 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
@before_each << block
else
@before_each = [new_setup]
end
end
|
.run_before_each(endpoint) ⇒ Object
31
32
33
34
|
# File 'lib/grape/endpoint.rb', line 31
def run_before_each(endpoint)
superclass.run_before_each(endpoint) unless self == Endpoint
before_each.each { |blk| blk.try(:call, endpoint) }
end
|
Instance Method Details
#call(env) ⇒ Object
171
172
173
174
|
# File 'lib/grape/endpoint.rb', line 171
def call(env)
lazy_initialize!
dup.call!(env)
end
|
#call!(env) ⇒ Object
176
177
178
179
180
181
182
|
# File 'lib/grape/endpoint.rb', line 176
def call!(env)
env[Grape::Env::API_ENDPOINT] = self
@env = env
singleton_class.include(@helpers) if @helpers
@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.
186
187
188
|
# File 'lib/grape/endpoint.rb', line 186
def endpoints
@endpoints ||= options[:app].try(:endpoints)
end
|
#equals?(endpoint) ⇒ Boolean
190
191
192
|
# File 'lib/grape/endpoint.rb', line 190
def equals?(endpoint)
(options == endpoint.options) && (inheritable_setting.to_hash == endpoint.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.
82
83
84
85
86
87
88
89
|
# File 'lib/grape/endpoint.rb', line 82
def inherit_settings(namespace_stackable)
parent_validations = namespace_stackable[:validations]
inheritable_setting.route[:saved_validations].concat(parent_validations) if parent_validations.any?
parent_declared_params = namespace_stackable[:declared_params]
inheritable_setting.route[:declared_params].concat(parent_declared_params.flatten) if parent_declared_params.any?
endpoints&.each { |e| e.inherit_settings(namespace_stackable) }
end
|
#inspect ⇒ Object
The purpose of this override is solely for stripping internals when an error occurs while calling an endpoint through an api. See github.com/ruby-grape/grape/issues/2398 Otherwise, it calls super.
197
198
199
200
201
|
# File 'lib/grape/endpoint.rb', line 197
def inspect
return super unless env
"#{self.class} in '#{route.origin}' endpoint"
end
|
#map_routes ⇒ Object
157
158
159
|
# File 'lib/grape/endpoint.rb', line 157
def map_routes
options[:method].map { |method| options[:path].map { |path| yield method, path } }
end
|
#mount_in(router) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/grape/endpoint.rb', line 105
def mount_in(router)
return endpoints.each { |e| e.mount_in(router) } if endpoints
reset_routes!
routes.each do |route|
router.append(route.apply(self))
next unless !inheritable_setting.namespace_inheritable[:do_not_route_head] && route.request_method == Rack::GET
route.dup.then do |head_route|
head_route.convert_to_head_request!
router.append(head_route.apply(self))
end
end
end
|
#namespace ⇒ Object
167
168
169
|
# File 'lib/grape/endpoint.rb', line 167
def namespace
@namespace ||= Namespace.joined_space_path(inheritable_setting.namespace_stackable[:namespace])
end
|
#prepare_default_path_settings ⇒ Object
161
162
163
164
165
|
# File 'lib/grape/endpoint.rb', line 161
def prepare_default_path_settings
namespace_stackable_hash = inheritable_setting.namespace_stackable.to_hash
namespace_inheritable_hash = inheritable_setting.namespace_inheritable.to_hash
namespace_stackable_hash.merge!(namespace_inheritable_hash)
end
|
#prepare_default_route_attributes ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/grape/endpoint.rb', line 138
def prepare_default_route_attributes
{
namespace: namespace,
version: prepare_version,
requirements: prepare_routes_requirements,
prefix: inheritable_setting.namespace_inheritable[:root_prefix],
anchor: options[:route_options].fetch(:anchor, true),
settings: inheritable_setting.route.except(:declared_params, :saved_validations),
forward_match: options[:forward_match]
}
end
|
#prepare_routes_requirements ⇒ Object
131
132
133
134
135
136
|
# File 'lib/grape/endpoint.rb', line 131
def prepare_routes_requirements
{}.merge!(*inheritable_setting.namespace_stackable[:namespace].map(&:requirements)).tap do |requirements|
endpoint_requirements = options.dig(:route_options, :requirements)
requirements.merge!(endpoint_requirements) if endpoint_requirements
end
end
|
#prepare_version ⇒ Object
150
151
152
153
154
155
|
# File 'lib/grape/endpoint.rb', line 150
def prepare_version
version = inheritable_setting.namespace_inheritable[:version]
return if version.blank?
version.length == 1 ? version.first : version
end
|
#require_option(options, key) ⇒ Object
91
92
93
|
# File 'lib/grape/endpoint.rb', line 91
def require_option(options, key)
raise Grape::Exceptions::MissingOption.new(key) unless options.key?(key)
end
|
#reset_routes! ⇒ Object
99
100
101
102
103
|
# File 'lib/grape/endpoint.rb', line 99
def reset_routes!
endpoints&.each(&:reset_routes!)
@namespace = nil
@routes = nil
end
|
#routes ⇒ Object
95
96
97
|
# File 'lib/grape/endpoint.rb', line 95
def routes
@routes ||= endpoints&.collect(&:routes)&.flatten || to_routes
end
|
#to_routes ⇒ Object
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/grape/endpoint.rb', line 120
def to_routes
default_route_options = prepare_default_route_attributes
map_routes do |method, raw_path|
prepared_path = Path.new(raw_path, namespace, prepare_default_path_settings)
params = options[:route_options].present? ? options[:route_options].merge(default_route_options) : default_route_options
route = Grape::Router::Route.new(method, prepared_path.origin, prepared_path.suffix, params)
route.apply(self)
end.flatten
end
|