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
-
#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
-
#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 Entity
#entity_class_for_obj, #present
Methods included from Declared
Instance Method Details
#api_format(format) ⇒ Object
180 181 182 |
# File 'lib/grape/dsl/inside_route.rb', line 180 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.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/grape/dsl/inside_route.rb', line 96 def body(value = nil) if value @body = value elsif value == false @body = '' status 204 else @body end end |
#configuration ⇒ Object
17 18 19 |
# File 'lib/grape/dsl/inside_route.rb', line 17 def configuration config.api.configuration.evaluate end |
#content_type(val = nil) ⇒ Object
Set response content-type
80 81 82 83 84 |
# File 'lib/grape/dsl/inside_route.rb', line 80 def content_type(val = nil) return header(Rack::CONTENT_TYPE, val) if val header[Rack::CONTENT_TYPE] end |
#context ⇒ Object
184 185 186 |
# File 'lib/grape/dsl/inside_route.rb', line 184 def context self 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.
29 30 31 32 33 34 35 |
# File 'lib/grape/dsl/inside_route.rb', line 29 def error!(, status = nil, additional_headers = nil, backtrace = nil, original_exception = nil) resolved_status = self.status(status || inheritable_setting.default_error_status) headers = additional_headers.present? ? header.merge(additional_headers) : header throw :error, Grape::Exceptions::ErrorResponse.new( message:, status: resolved_status, headers:, backtrace:, original_exception: ) end |
#http_version ⇒ Object
176 177 178 |
# File 'lib/grape/dsl/inside_route.rb', line 176 def http_version env.fetch('HTTP_VERSION') { env[Rack::SERVER_PROTOCOL] } end |
#redirect(url, permanent: false, body: nil) ⇒ Object
Redirect to a new url.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/grape/dsl/inside_route.rb', line 42 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' # Render the message Grape generated as the plain text it is. Setting # only the header left it to the API's own formatter, which on a JSON # API returned the sentence wrapped in quotes under a text/plain content # type. A caller-supplied body keeps the API's format: it may be # structured, and the txt formatter would render a Hash through `to_s`. api_format :txt unless body body end |
#return_no_content ⇒ Object
Allows you to explicitly return no content.
116 117 118 |
# File 'lib/grape/dsl/inside_route.rb', line 116 def return_no_content body false end |
#route ⇒ Object
Returns route information for the current request.
172 173 174 |
# File 'lib/grape/dsl/inside_route.rb', line 172 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.
128 129 130 131 132 133 134 135 |
# File 'lib/grape/dsl/inside_route.rb', line 128 def sendfile(value = nil) return stream if value.nil? raise ArgumentError, 'argument must be a file path' unless value.is_a?(String) file_body = Grape::ServeStream::FileBody.new(value) @stream = Grape::ServeStream::StreamResponse.new(file_body) end |
#status(status = nil) ⇒ Object
Set or retrieve the HTTP status code.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/grape/dsl/inside_route.rb', line 68 def status(status = nil) return @status || default_status if status.nil? case status when Symbol, Integer @status = Rack::Utils.status_code(status) 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:
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/grape/dsl/inside_route.rb', line 152 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) return @stream if value.nil? @stream = Grape::ServeStream::StreamResponse.new(stream_body(value)) end |
#version ⇒ Object
The API version as specified in the URL.
13 14 15 |
# File 'lib/grape/dsl/inside_route.rb', line 13 def version env[Grape::Env::API_VERSION] end |