Class: Grape::Middleware::Formatter

Inherits:
Base
  • Object
show all
Includes:
PrecomputedContentTypes
Defined in:
lib/grape/middleware/formatter.rb

Defined Under Namespace

Classes: Options

Constant Summary collapse

ALL_MEDIA_TYPES =
'*/*'
BODY_CARRYING_METHODS =

The request methods that can carry a body worth parsing. See #read_body_input?, which tests the env against this before anything asks for a Rack::Request. QUERY is here because its content is the query (RFC 10008, Section 2), not an optional payload.

[Rack::POST, Rack::PUT, Rack::PATCH, Rack::DELETE, Grape::QUERY].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#app, #config, #env, #options

Instance Method Summary collapse

Methods included from PrecomputedContentTypes

#content_type, #content_type_for, #content_types, #mime_types

Methods inherited from Base

#call, #call!, #context, #query_params, #rack_request, #response

Methods included from DSL::Headers

#header

Constructor Details

#initialize(app, **options) ⇒ Formatter

The formatter is the only middleware that maps an incoming media type back to a format, so it warms mime_types itself rather than making every content-type-aware middleware build a table none of them read.



32
33
34
35
36
37
38
39
# File 'lib/grape/middleware/formatter.rb', line 32

def initialize(app, **options)
  super
  @default_format = config.default_format
  @format = config.format
  @formatters = config.formatters
  @parsers = config.parsers
  mime_types
end

Instance Attribute Details

#default_formatObject (readonly)

Read off ivars rather than delegated into config on every request: negotiate_content_type asks for format and default_format per request and fetch_formatter for formatters, and each delegator cost a Forwardable frame plus a Data reader for a value that was frozen when the middleware was built.



27
28
29
# File 'lib/grape/middleware/formatter.rb', line 27

def default_format
  @default_format
end

#formatObject (readonly)

Read off ivars rather than delegated into config on every request: negotiate_content_type asks for format and default_format per request and fetch_formatter for formatters, and each delegator cost a Forwardable frame plus a Data reader for a value that was frozen when the middleware was built.



27
28
29
# File 'lib/grape/middleware/formatter.rb', line 27

def format
  @format
end

#formattersObject (readonly)

Read off ivars rather than delegated into config on every request: negotiate_content_type asks for format and default_format per request and fetch_formatter for formatters, and each delegator cost a Forwardable frame plus a Data reader for a value that was frozen when the middleware was built.



27
28
29
# File 'lib/grape/middleware/formatter.rb', line 27

def formatters
  @formatters
end

#parsersObject (readonly)

Read off ivars rather than delegated into config on every request: negotiate_content_type asks for format and default_format per request and fetch_formatter for formatters, and each delegator cost a Forwardable frame plus a Data reader for a value that was frozen when the middleware was built.



27
28
29
# File 'lib/grape/middleware/formatter.rb', line 27

def parsers
  @parsers
end

Instance Method Details

#afterObject



46
47
48
49
50
51
52
53
54
# File 'lib/grape/middleware/formatter.rb', line 46

def after
  return unless @app_response

  status, headers, bodies = @app_response

  return [status, headers, []] if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status)

  build_formatted_response(status, headers, bodies)
end

#beforeObject



41
42
43
44
# File 'lib/grape/middleware/formatter.rb', line 41

def before
  negotiate_content_type
  read_body_input
end