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
174 175 176 |
# File 'lib/grape/dsl/inside_route.rb', line 174 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.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/grape/dsl/inside_route.rb', line 90 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.for.configuration.evaluate end |
#content_type(val = nil) ⇒ Object
Set response content-type
74 75 76 77 78 |
# File 'lib/grape/dsl/inside_route.rb', line 74 def content_type(val = nil) return header(Rack::CONTENT_TYPE, val) if val header[Rack::CONTENT_TYPE] end |
#context ⇒ Object
178 179 180 |
# File 'lib/grape/dsl/inside_route.rb', line 178 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) status = self.status(status || inheritable_setting.namespace_inheritable[:default_error_status]) headers = additional_headers.present? ? header.merge(additional_headers) : header throw :error, Grape::Exceptions::ErrorResponse.new( message:, status:, headers:, backtrace:, original_exception: ) end |
#http_version ⇒ Object
170 171 172 |
# File 'lib/grape/dsl/inside_route.rb', line 170 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 |
# 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' body end |
#return_no_content ⇒ Object
Allows you to explicitly return no content.
110 111 112 |
# File 'lib/grape/dsl/inside_route.rb', line 110 def return_no_content body false end |
#route ⇒ Object
Returns route information for the current request.
166 167 168 |
# File 'lib/grape/dsl/inside_route.rb', line 166 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.
122 123 124 125 126 127 128 129 |
# File 'lib/grape/dsl/inside_route.rb', line 122 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.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/grape/dsl/inside_route.rb', line 62 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:
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/grape/dsl/inside_route.rb', line 146 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 |