Class: Grape::Middleware::Formatter
- 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
-
#default_format ⇒ Object
readonly
Read off ivars rather than delegated into
configon every request:negotiate_content_typeasks forformatanddefault_formatper request andfetch_formatterforformatters, and each delegator cost a Forwardable frame plus a Data reader for a value that was frozen when the middleware was built. -
#format ⇒ Object
readonly
Read off ivars rather than delegated into
configon every request:negotiate_content_typeasks forformatanddefault_formatper request andfetch_formatterforformatters, and each delegator cost a Forwardable frame plus a Data reader for a value that was frozen when the middleware was built. -
#formatters ⇒ Object
readonly
Read off ivars rather than delegated into
configon every request:negotiate_content_typeasks forformatanddefault_formatper request andfetch_formatterforformatters, and each delegator cost a Forwardable frame plus a Data reader for a value that was frozen when the middleware was built. -
#parsers ⇒ Object
readonly
Read off ivars rather than delegated into
configon every request:negotiate_content_typeasks forformatanddefault_formatper request andfetch_formatterforformatters, and each delegator cost a Forwardable frame plus a Data reader for a value that was frozen when the middleware was built.
Attributes inherited from Base
Instance Method Summary collapse
- #after ⇒ Object
- #before ⇒ Object
-
#initialize(app, **options) ⇒ Formatter
constructor
The formatter is the only middleware that maps an incoming media type back to a format, so it warms
mime_typesitself rather than making every content-type-aware middleware build a table none of them read.
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
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, **) super @default_format = config.default_format @format = config.format @formatters = config.formatters @parsers = config.parsers mime_types end |
Instance Attribute Details
#default_format ⇒ Object (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 |
#format ⇒ Object (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 |
#formatters ⇒ Object (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 |
#parsers ⇒ Object (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
#after ⇒ Object
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 |
#before ⇒ Object
41 42 43 44 |
# File 'lib/grape/middleware/formatter.rb', line 41 def before negotiate_content_type read_body_input end |