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



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.

Examples:

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

GET /body # => "Body"


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

#configurationObject



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

#contextObject



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.

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)
  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_versionObject



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.

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
# 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'
  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, ""


110
111
112
# File 'lib/grape/dsl/inside_route.rb', line 110

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


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.

Examples:

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

GET /file # => "contents of file"

Raises:

  • (ArgumentError)


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.

Parameters:

  • status (Integer) (defaults to: nil)

    The HTTP Status Code to return for this request.



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:

Examples:

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

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


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

#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