Module: Grape::DSL::InsideRoute

Includes:
Declared, Entity
Included in:
Endpoint
Defined in:
lib/grape/dsl/inside_route.rb

Constant Summary collapse

MethodNotYetAvailable =

Backward compatibility: alias exception class to previous location

Declared::MethodNotYetAvailable

Instance Method Summary collapse

Methods included from Entity

#entity_class_for_obj, #present

Methods included from Declared

#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.

Examples:

get '/body' do
  body "Body"
  "Not the Body"
end

GET /body # => "Body"


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

#configurationObject



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

#contextObject



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.

Parameters:

  • message (String)

    The message to display.

  • status (Integer) (defaults to: nil)

    The HTTP Status Code. Defaults to default_error_status, 500 if not set.

  • additional_headers (Hash) (defaults to: nil)

    Addtional headers for the response.

  • backtrace (Array<String>) (defaults to: nil)

    The backtrace of the exception that caused the error.

  • original_exception (Exception) (defaults to: nil)

    The original exception that caused the error.



29
30
31
32
33
34
35
# File 'lib/grape/dsl/inside_route.rb', line 29

def error!(message, 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_versionObject



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.

Parameters:

  • url (String)

    The url to be redirect.

  • permanent (Boolean) (defaults to: false)

    default false.

  • body (defaults to: nil)

    default a short message including the 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_message = body
  if permanent
    status 301
    body_message ||= "This resource has been moved permanently to #{url}."
  elsif http_version == 'HTTP/1.1' && !request.get?
    status 303
    body_message ||= "An alternate resource is located at #{url}."
  else
    status 302
    body_message ||= "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 body_message
end

#return_no_contentObject

Allows you to explicitly return no content.

Examples:

delete :id do
  return_no_content
  "not returned"
end

DELETE /12 # => 204 No Content, ""


116
117
118
# File 'lib/grape/dsl/inside_route.rb', line 116

def return_no_content
  body false
end

#routeObject

Returns route information for the current request.

Examples:


desc "Returns the route description."
get '/' do
  route.description
end


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.

Examples:

get '/file' do
  sendfile FileStreamer.new(...)
end

GET /file # => "contents of file"

Raises:

  • (ArgumentError)


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.

Parameters:

  • status (Integer) (defaults to: nil)

    The HTTP Status Code to return for this request.



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:

Examples:

get '/stream' do
  stream FileStreamer.new(...)
end

GET /stream # => "chunked contents of file"


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

#versionObject

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