Module: Grape::DSL::InsideRoute
Constant Summary collapse
- MethodNotYetAvailable =
Backward compatibility: alias exception class to previous location
Declared::MethodNotYetAvailable
Instance Method Summary collapse
- #api_format(format) ⇒ Object
-
#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.
- #context ⇒ Object
-
#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, backtrace = nil, original_exception = nil) ⇒ Object
End the request and display an error to the end user with the specified message.
- #http_version ⇒ Object
-
#present(*args, **options) ⇒ Object
Allows you to make use of Grape Entities by setting the response body to the serializable hash of the entity provided in the
:withoption. -
#redirect(url, permanent: false, body: nil) ⇒ 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 Declared
Instance Method Details
#api_format(format) ⇒ Object
284 285 286 |
# File 'lib/grape/dsl/inside_route.rb', line 284 def api_format(format) env[Grape::Env::API_FORMAT] = format end |
#body(value = nil) ⇒ Object
Allows you to define the response body as something other than the return value.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/grape/dsl/inside_route.rb', line 110 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
16 17 18 |
# File 'lib/grape/dsl/inside_route.rb', line 16 def configuration [:for].configuration.evaluate end |
#content_type(val = nil) ⇒ Object
Set response content-type
92 93 94 95 96 97 98 |
# File 'lib/grape/dsl/inside_route.rb', line 92 def content_type(val = nil) if val header(Rack::CONTENT_TYPE, val) else header[Rack::CONTENT_TYPE] end end |
#context ⇒ Object
288 289 290 |
# File 'lib/grape/dsl/inside_route.rb', line 288 def context self 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.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/grape/dsl/inside_route.rb', line 251 def entity_class_for_obj(object, ) entity_class = .delete(:with) return entity_class if entity_class # 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 representations = inheritable_setting.namespace_stackable_with_hash(:representations) if representations potential = object_class.ancestors.detect { |potential| representations.key?(potential) } entity_class = representations[potential] if potential end entity_class = object_class.const_get(:Entity) if !entity_class && object_class.const_defined?(:Entity) && object_class.const_get(:Entity).respond_to?(:represent) 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.
274 275 276 277 278 |
# File 'lib/grape/dsl/inside_route.rb', line 274 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, **, **) end |
#error!(message, status = nil, additional_headers = nil, backtrace = nil, original_exception = nil) ⇒ Object
End the request and display an error to the end user with the specified message.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/grape/dsl/inside_route.rb', line 28 def error!(, status = nil, additional_headers = nil, backtrace = nil, original_exception = nil) status = self.status(status || inheritable_setting.namespace_inheritable[:default_error_status]) headers = additional_headers.present? ? header.merge(additional_headers) : header throw :error, message: , status: status, headers: headers, backtrace: backtrace, original_exception: original_exception end |
#http_version ⇒ Object
280 281 282 |
# File 'lib/grape/dsl/inside_route.rb', line 280 def http_version env.fetch('HTTP_VERSION') { env[Rack::SERVER_PROTOCOL] } end |
#present(*args, **options) ⇒ 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.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/grape/dsl/inside_route.rb', line 202 def present(*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 |
#redirect(url, permanent: false, body: nil) ⇒ Object
Redirect to a new url.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/grape/dsl/inside_route.rb', line 44 def redirect(url, permanent: false, body: nil) = body if permanent status 301 ||= "This resource has been moved permanently to #{url}." elsif http_version == 'HTTP/1.1' && !request.get? status 303 ||= "An alternate resource is located at #{url}." else status 302 ||= "This resource has been moved temporarily to #{url}." end header 'Location', url content_type 'text/plain' body end |
#return_no_content ⇒ Object
Allows you to explicitly return no content.
130 131 132 133 |
# File 'lib/grape/dsl/inside_route.rb', line 130 def return_no_content status 204 body false end |
#route ⇒ Object
Returns route information for the current request.
239 240 241 |
# File 'lib/grape/dsl/inside_route.rb', line 239 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.
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/grape/dsl/inside_route.rb', line 143 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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/grape/dsl/inside_route.rb', line 64 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 if request.post? 201 elsif request.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:
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/grape/dsl/inside_route.rb', line 169 def stream(value = nil) return if value.nil? && @stream.nil? header Rack::CONTENT_LENGTH, nil header '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.
12 13 14 |
# File 'lib/grape/dsl/inside_route.rb', line 12 def version env[Grape::Env::API_VERSION] end |