Module: Grape::Middleware::PrecomputedContentTypes
- Included in:
- Error, Formatter, Versioner::Base
- Defined in:
- lib/grape/middleware/precomputed_content_types.rb
Overview
Include in a middleware subclass that needs content-type negotiation.
Provides content_types / mime_types / content_type_for /
content_type resolved from config.content_types and
config.format — so the consuming middleware's Options Data class
must declare both fields. Warms those caches on the parent instance
at initialization so per-request +dup+s inherit them rather than
rebuilding them.
mime_types is not warmed here: Formatter is the only middleware that
reads it, and it warms it itself. The tables behind mime_types and
content_type_for are shared process-wide per content-type registry
(see Grape::ContentTypes), so the ivars below memoize a lookup, not a
copy.
Opt-in: plain Grape::Middleware::Base subclasses that don't need
content-type-aware helpers don't pay for them.
Instance Method Summary collapse
- #content_type ⇒ Object
- #content_type_for(format) ⇒ Object
- #content_types ⇒ Object
- #initialize(app, **options) ⇒ Object
- #mime_types ⇒ Object
Instance Method Details
#content_type ⇒ Object
40 41 42 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 40 def content_type content_type_for(env[Grape::Env::API_FORMAT] || config.format) || 'text/html' end |
#content_type_for(format) ⇒ Object
36 37 38 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 36 def content_type_for(format) content_types_lookup[format] end |
#content_types ⇒ Object
28 29 30 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 28 def content_types @content_types ||= Grape::ContentTypes.content_types_for(config.content_types) end |
#initialize(app, **options) ⇒ Object
22 23 24 25 26 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 22 def initialize(app, **) super content_types content_types_lookup end |
#mime_types ⇒ Object
32 33 34 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 32 def mime_types @mime_types ||= Grape::ContentTypes.mime_types_for(content_types) end |