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.
-
#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, headers = nil) ⇒ Object
End the request and display an error to the end user with the specified message.
-
#file(value = nil) ⇒ Object
Allows you to define the response as a file-like object.
-
#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.
-
#redirect(url, options = {}) ⇒ Object
Redirect to a new url.
-
#route ⇒ Object
Returns route information for the current request.
-
#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_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
.
21 22 23 24 |
# File 'lib/grape/dsl/inside_route.rb', line 21 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.
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/grape/dsl/inside_route.rb', line 174 def body(value = nil) if value @body = value elsif value == false @body = '' status 204 else @body end end |
#content_type(val = nil) ⇒ Object
Set response content-type
144 145 146 147 148 149 150 |
# File 'lib/grape/dsl/inside_route.rb', line 144 def content_type(val = nil) if val header(Grape::Http::Headers::CONTENT_TYPE, val) else header[Grape::Http::Headers::CONTENT_TYPE] end end |
#cookies ⇒ Object
Set or get a cookie
160 161 162 |
# File 'lib/grape/dsl/inside_route.rb', line 160 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.
72 73 74 |
# File 'lib/grape/dsl/inside_route.rb', line 72 def declared(*) fail 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.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/grape/dsl/inside_route.rb', line 290 def entity_class_for_obj(object, ) entity_class = .delete(:with) if entity_class.nil? # entity class not explicitely 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.
313 314 315 316 317 |
# File 'lib/grape/dsl/inside_route.rb', line 313 def entity_representation_for(entity_class, object, ) = { env: env } [:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION] entity_class.represent(object, .merge()) end |
#error!(message, status = nil, headers = nil) ⇒ Object
End the request and display an error to the end user with the specified message.
86 87 88 89 |
# File 'lib/grape/dsl/inside_route.rb', line 86 def error!(, status = nil, headers = nil) self.status(status || namespace_inheritable(:default_error_status)) throw :error, message: , status: self.status, headers: headers end |
#file(value = nil) ⇒ Object
Allows you to define the response as a file-like object.
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/grape/dsl/inside_route.rb', line 193 def file(value = nil) if value.is_a?(String) file_body = Grape::ServeFile::FileBody.new(value) @file = Grape::ServeFile::FileResponse.new(file_body) elsif !value.is_a?(NilClass) warn '[DEPRECATION] Argument as FileStreamer-like object is deprecated. Use path to file instead.' @file = Grape::ServeFile::FileResponse.new(value) else @file 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.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/grape/dsl/inside_route.rb', line 242 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 fail ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge) representation = @body.merge(representation) end body representation end |
#redirect(url, options = {}) ⇒ Object
Redirect to a new url.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/grape/dsl/inside_route.rb', line 97 def redirect(url, = {}) permanent = .fetch(:permanent, false) = .fetch(:body, nil) if permanent status 301 ||= "This resource has been moved permanently to #{url}." else if 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 end header 'Location', url content_type 'text/plain' body end |
#route ⇒ Object
Returns route information for the current request.
278 279 280 |
# File 'lib/grape/dsl/inside_route.rb', line 278 def route env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info] end |
#status(status = nil) ⇒ Object
Set or retrieve the HTTP status code.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/grape/dsl/inside_route.rb', line 120 def status(status = nil) case status when Symbol if Rack::Utils::SYMBOL_TO_STATUS_CODE.keys.include?(status) @status = Rack::Utils.status_code(status) else fail ArgumentError, "Status code :#{status} is invalid." end when Fixnum @status = status when nil return @status if @status case request.request_method.to_s.upcase when Grape::Http::Headers::POST 201 else 200 end else fail ArgumentError, 'Status code must be Fixnum 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:
220 221 222 223 224 225 |
# File 'lib/grape/dsl/inside_route.rb', line 220 def stream(value = nil) header 'Content-Length', nil header 'Transfer-Encoding', nil header 'Cache-Control', 'no-cache' # Skips ETag generation (reading the response up front) file(value) end |
#version ⇒ Object
The API version as specified in the URL.
77 78 79 |
# File 'lib/grape/dsl/inside_route.rb', line 77 def version env[Grape::Env::API_VERSION] end |