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 dups inherit them (avoiding ~1 µs/request of with_indifferent_access recomputation).
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
35 36 37 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 35 def content_type content_type_for(env[Grape::Env::API_FORMAT] || config.format) || 'text/html' end |
#content_type_for(format) ⇒ Object
31 32 33 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 31 def content_type_for(format) content_types_indifferent_access[format] end |
#content_types ⇒ Object
23 24 25 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 23 def content_types @content_types ||= Grape::ContentTypes.content_types_for(config.content_types) end |
#initialize(app, **options) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 16 def initialize(app, **) super content_types mime_types content_types_indifferent_access end |
#mime_types ⇒ Object
27 28 29 |
# File 'lib/grape/middleware/precomputed_content_types.rb', line 27 def mime_types @mime_types ||= Grape::ContentTypes.mime_types_for(content_types) end |