module Grape::DSL::InsideRoute

def api_format(format)

def api_format(format)
  env[Grape::Env::API_FORMAT] = format
end

def body(value = nil)

GET /body # => "Body"

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

return value.
Allows you to define the response body as something other than the
def body(value = nil)
  if value
    @body = value
  elsif value == false
    @body = ''
    status 204
  else
    @body
  end
end

def configuration

def configuration
  config.for.configuration.evaluate
end

def content_type(val = nil)

Set response content-type
def content_type(val = nil)
  return header(Rack::CONTENT_TYPE, val) if val
  header[Rack::CONTENT_TYPE]
end

def context

def context
  self
end

def default_status

The default HTTP status when none has been set explicitly.
def default_status
  return 201 if request.post?
  return 204 if request.delete? && @body.blank?
  200
end

def error!(message, status = nil, additional_headers = nil, backtrace = nil, original_exception = nil)

Parameters:
  • original_exception (Exception) -- The original exception that caused the error.
  • backtrace (Array) -- The backtrace of the exception that caused the error.
  • additional_headers (Hash) -- Addtional headers for the response.
  • status (Integer) -- The HTTP Status Code. Defaults to default_error_status, 500 if not set.
  • message (String) -- The message to display.
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

def http_version

def http_version
  env.fetch('HTTP_VERSION') { env[Rack::SERVER_PROTOCOL] }
end

def redirect(url, permanent: false, body: nil)

Parameters:
  • body () -- default a short message including the URL.
  • permanent (Boolean) -- default false.
  • url (String) -- The url to be redirect.
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

def return_no_content

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

end
"not returned"
return_no_content
delete :id do
@example

Allows you to explicitly return no content.
def return_no_content
  body false
end

def route

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

@example

Returns route information for the current request.
def route
  env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info]
end

def sendfile(value = nil)

GET /file # => "contents of file"

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

Allows you to send a file to the client via sendfile.
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

def status(status = nil)

Parameters:
  • status (Integer) -- The HTTP Status Code to return for this request.
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

def stream(value = nil)

* https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/etag.rb
* https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/chunked.rb
See:

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

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

Rack assumes this response can be streamed in chunks.
If Content-Length and Transfer-Encoding are blank (among other conditions),

Allows you to define the response as a streamable object.
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

def stream_body(value)

Wraps a stream +value+ into a body that responds to +:each+.
def stream_body(value)
  return Grape::ServeStream::FileBody.new(value) if value.is_a?(String)
  raise ArgumentError, 'Stream object must respond to :each.' unless value.respond_to?(:each)
  value
end

def version

The API version as specified in the URL.
def version
  env[Grape::Env::API_VERSION]
end