Module: Grape::DSL::InsideRoute
Defined Under Namespace
Modules: PostBeforeFilter Classes: MethodNotYetAvailable
Instance Attribute Summary
Attributes included from Settings
#inheritable_setting, #top_level_setting
Class Method Summary collapse
-
.post_filter_methods(type) ⇒ Module
A module containing method overrides suitable for the position in the filter evaluation sequence denoted by +type+.
Instance Method Summary collapse
-
#body(value = nil) ⇒ Object
Allows you to define the response body as something other than the return value.
- #configuration ⇒ Object
-
#content_type(val = nil) ⇒ Object
Set response content-type.
-
#cookies ⇒ Object
Set or get a cookie.
-
#declared ⇒ Object
A filtering method that will return a hash consisting only of keys that have been declared by a
params
statement against the current/target endpoint or parent namespaces. -
#entity_class_for_obj(object, options) ⇒ Class
Attempt to locate the Entity class for a given object, if not given explicitly.
-
#entity_representation_for(entity_class, object, options) ⇒ Object
The representation of the given object as done through the given entity_class.
-
#error!(message, status = nil, additional_headers = nil) ⇒ Object
End the request and display an error to the end user with the specified message.
-
#file(value = nil) ⇒ Object
Deprecated method to send files to the client.
-
#present(*args) ⇒ Object
Allows you to make use of Grape Entities by setting the response body to the serializable hash of the entity provided in the
:with
option. -
#rack_response(message, status = 200, headers = { Rack::CONTENT_TYPE => content_type }) ⇒ Object
Creates a Rack response based on the provided message, status, and headers.
-
#redirect(url, permanent: false, body: nil, **_options) ⇒ Object
Redirect to a new url.
-
#return_no_content ⇒ Object
Allows you to explicitly return no content.
-
#route ⇒ Object
Returns route information for the current request.
-
#sendfile(value = nil) ⇒ Object
Allows you to send a file to the client via sendfile.
-
#status(status = nil) ⇒ Object
Set or retrieve the HTTP status code.
-
#stream(value = nil) ⇒ Object
Allows you to define the response as a streamable object.
-
#version ⇒ Object
The API version as specified in the URL.
Methods included from Headers
Methods included from Settings
#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
Class Method Details
.post_filter_methods(type) ⇒ Module
Returns A module containing method overrides suitable for the position in the filter evaluation sequence denoted by +type+. This defaults to an empty module if no overrides are defined for the given filter +type+.
22 23 24 25 |
# File 'lib/grape/dsl/inside_route.rb', line 22 def self.post_filter_methods(type) @post_filter_modules ||= { before: PostBeforeFilter } @post_filter_modules[type] end |
Instance Method Details
#body(value = nil) ⇒ Object
Allows you to define the response body as something other than the return value.
273 274 275 276 277 278 279 280 281 282 |
# File 'lib/grape/dsl/inside_route.rb', line 273 def body(value = nil) if value @body = value elsif value == false @body = '' status 204 else instance_variable_defined?(:@body) ? @body : nil end end |
#configuration ⇒ Object
154 155 156 |
# File 'lib/grape/dsl/inside_route.rb', line 154 def configuration [:for].configuration.evaluate end |
#content_type(val = nil) ⇒ Object
Set response content-type
243 244 245 246 247 248 249 |
# File 'lib/grape/dsl/inside_route.rb', line 243 def content_type(val = nil) if val header(Rack::CONTENT_TYPE, val) else header[Rack::CONTENT_TYPE] end end |
#cookies ⇒ Object
Set or get a cookie
259 260 261 |
# File 'lib/grape/dsl/inside_route.rb', line 259 def @cookies ||= Cookies.new end |
#declared ⇒ Object
A filtering method that will return a hash
consisting only of keys that have been declared by a
params
statement against the current/target endpoint or parent
namespaces.
options. :include_parent_namespaces
defaults to true, hence must be set to false if
you want only to return params declared against the current/target endpoint.
145 146 147 |
# File 'lib/grape/dsl/inside_route.rb', line 145 def declared(*) raise MethodNotYetAvailable, '#declared is not available prior to parameter validation.' end |
#entity_class_for_obj(object, options) ⇒ Class
Attempt to locate the Entity class for a given object, if not given
explicitly. This is done by looking for the presence of Klass::Entity,
where Klass is the class of the object
parameter, or one of its
ancestors.
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/grape/dsl/inside_route.rb', line 429 def entity_class_for_obj(object, ) entity_class = .delete(:with) if entity_class.nil? # entity class not explicitly defined, auto-detect from relation#klass or first object in the collection object_class = if object.respond_to?(:klass) object.klass else object.respond_to?(:first) ? object.first.class : object.class end object_class.ancestors.each do |potential| entity_class ||= (namespace_stackable_with_hash(:representations) || {})[potential] end entity_class ||= object_class.const_get(:Entity) if object_class.const_defined?(:Entity) && object_class.const_get(:Entity).respond_to?(:represent) end entity_class end |
#entity_representation_for(entity_class, object, options) ⇒ Object
Returns the representation of the given object as done through the given entity_class.
452 453 454 455 456 |
# File 'lib/grape/dsl/inside_route.rb', line 452 def entity_representation_for(entity_class, object, ) = { env: env } [:version] = env[Grape::Env::API_VERSION] if env.key?(Grape::Env::API_VERSION) entity_class.represent(object, **.merge()) end |
#error!(message, status = nil, additional_headers = nil) ⇒ Object
End the request and display an error to the end user with the specified message.
164 165 166 167 168 |
# File 'lib/grape/dsl/inside_route.rb', line 164 def error!(, status = nil, additional_headers = nil) status = self.status(status || namespace_inheritable(:default_error_status)) headers = additional_headers.present? ? header.merge(additional_headers) : header throw :error, message: , status: status, headers: headers end |
#file(value = nil) ⇒ Object
Deprecated method to send files to the client. Use sendfile
or stream
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/grape/dsl/inside_route.rb', line 299 def file(value = nil) if value.is_a?(String) Grape.deprecator.warn('Use sendfile or stream to send files.') sendfile(value) elsif !value.is_a?(NilClass) Grape.deprecator.warn('Use stream to use a Stream object.') stream(value) else Grape.deprecator.warn('Use sendfile or stream to send files.') sendfile end end |
#present(*args) ⇒ Object
Allows you to make use of Grape Entities by setting
the response body to the serializable hash of the
entity provided in the :with
option. This has the
added benefit of automatically passing along environment
and version information to the serialization, making it
very easy to do conditional exposures. See Entity docs
for more info.
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/grape/dsl/inside_route.rb', line 379 def present(*args) = args.count > 1 ? args. : {} key, object = if args.count == 2 && args.first.is_a?(Symbol) args else [nil, args.first] end entity_class = entity_class_for_obj(object, ) root = .delete(:root) representation = if entity_class entity_representation_for(entity_class, object, ) else object end representation = { root => representation } if root if key representation = (body || {}).merge(key => representation) elsif entity_class.present? && body raise ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge) representation = body.merge(representation) end body representation end |
#rack_response(message, status = 200, headers = { Rack::CONTENT_TYPE => content_type }) ⇒ Object
Creates a Rack response based on the provided message, status, and headers. The content type in the headers is set to the default content type unless provided. The message is HTML-escaped if the content type is 'text/html'.
Returns: A Rack::Response object containing the specified message, status, and headers.
182 183 184 185 186 |
# File 'lib/grape/dsl/inside_route.rb', line 182 def rack_response(, status = 200, headers = { Rack::CONTENT_TYPE => content_type }) Grape.deprecator.warn('The rack_response method has been deprecated, use error! instead.') = Rack::Utils.escape_html() if headers[Rack::CONTENT_TYPE] == 'text/html' Rack::Response.new(Array.wrap(), Rack::Utils.status_code(status), headers) end |
#redirect(url, permanent: false, body: nil, **_options) ⇒ Object
Redirect to a new url.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/grape/dsl/inside_route.rb', line 194 def redirect(url, permanent: false, body: nil, **) = body if permanent status 301 ||= "This resource has been moved permanently to #{url}." elsif env[Grape::Http::Headers::HTTP_VERSION] == 'HTTP/1.1' && request.request_method.to_s.upcase != Grape::Http::Headers::GET status 303 ||= "An alternate resource is located at #{url}." else status 302 ||= "This resource has been moved temporarily to #{url}." end header Grape::Http::Headers::LOCATION, url content_type 'text/plain' body end |
#return_no_content ⇒ Object
Allows you to explicitly return no content.
293 294 295 296 |
# File 'lib/grape/dsl/inside_route.rb', line 293 def return_no_content status 204 body false end |
#route ⇒ Object
Returns route information for the current request.
417 418 419 |
# File 'lib/grape/dsl/inside_route.rb', line 417 def route env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info] end |
#sendfile(value = nil) ⇒ Object
Allows you to send a file to the client via sendfile.
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/grape/dsl/inside_route.rb', line 320 def sendfile(value = nil) if value.is_a?(String) file_body = Grape::ServeStream::FileBody.new(value) @stream = Grape::ServeStream::StreamResponse.new(file_body) elsif !value.is_a?(NilClass) raise ArgumentError, 'Argument must be a file path' else stream end end |
#status(status = nil) ⇒ Object
Set or retrieve the HTTP status code.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/grape/dsl/inside_route.rb', line 214 def status(status = nil) case status when Symbol raise ArgumentError, "Status code :#{status} is invalid." unless Rack::Utils::SYMBOL_TO_STATUS_CODE.key?(status) @status = Rack::Utils.status_code(status) when Integer @status = status when nil return @status if instance_variable_defined?(:@status) && @status case request.request_method.to_s.upcase when Grape::Http::Headers::POST 201 when Grape::Http::Headers::DELETE if instance_variable_defined?(:@body) && @body.present? 200 else 204 end else 200 end else raise ArgumentError, 'Status code must be Integer or Symbol.' end end |
#stream(value = nil) ⇒ Object
Allows you to define the response as a streamable object.
If Content-Length and Transfer-Encoding are blank (among other conditions), Rack assumes this response can be streamed in chunks.
See:
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/grape/dsl/inside_route.rb', line 346 def stream(value = nil) return if value.nil? && @stream.nil? header Rack::CONTENT_LENGTH, nil header Grape::Http::Headers::TRANSFER_ENCODING, nil header Rack::CACHE_CONTROL, 'no-cache' # Skips ETag generation (reading the response up front) if value.is_a?(String) file_body = Grape::ServeStream::FileBody.new(value) @stream = Grape::ServeStream::StreamResponse.new(file_body) elsif value.respond_to?(:each) @stream = Grape::ServeStream::StreamResponse.new(value) elsif !value.is_a?(NilClass) raise ArgumentError, 'Stream object must respond to :each.' else @stream end end |
#version ⇒ Object
The API version as specified in the URL.
150 151 152 |
# File 'lib/grape/dsl/inside_route.rb', line 150 def version env[Grape::Env::API_VERSION] end |