Module: Grape::ContentTypes
- Defined in:
- lib/grape/content_types.rb
Defined Under Namespace
Classes: LookupCache, MimeTypesCache
Constant Summary collapse
- DEFAULTS =
Content types are listed in order of preference.
{ xml: 'application/xml', serializable_hash: 'application/json', json: 'application/json', binary: 'application/octet-stream', txt: 'text/plain' }.freeze
- MIME_TYPES =
Grape::ContentTypes::DEFAULTS.except(:serializable_hash).invert.freeze
Class Method Summary collapse
- .content_types_for(from_settings) ⇒ Object
-
.lookup_for(from_settings) ⇒ Object
Every format under both spellings in one plain Hash, so a lookup is a single
Hash#[]. -
.media_type(content_type) ⇒ Object
The media type of a content-type header: the part before any
;parameters, with surrounding whitespace removed (e.g.'text/html'for'text/html; charset=utf-8'). - .mime_types_for(from_settings) ⇒ Object
Class Method Details
.content_types_for(from_settings) ⇒ Object
18 19 20 |
# File 'lib/grape/content_types.rb', line 18 def content_types_for(from_settings) from_settings.presence || DEFAULTS end |
.lookup_for(from_settings) ⇒ Object
Every format under both spellings in one plain Hash, so a lookup is a
single Hash#[]. HashWithIndifferentAccess converted the key on every
read instead, and this is read two or three times per request — to
negotiate the format, and again to set the response content type.
Keys arrive as Symbols: the content_type DSL symbolizes what it is
given and the defaults are Symbols. The key is stored as it came too,
so a middleware constructed directly with String keys still answers to
either spelling, as the indifferent hash did.
37 38 39 |
# File 'lib/grape/content_types.rb', line 37 def lookup_for(from_settings) LookupCache[from_settings] end |
.media_type(content_type) ⇒ Object
The media type of a content-type header: the part before any ;
parameters, with surrounding whitespace removed
(e.g. 'text/html' for 'text/html; charset=utf-8'). Returns nil for a
nil content type. Skips the split (and its allocation) when there are no
parameters, which is the common case.
46 47 48 49 50 51 |
# File 'lib/grape/content_types.rb', line 46 def media_type(content_type) return if content_type.nil? base = content_type.include?(';') ? content_type.split(';', 2).first : content_type base.strip end |
.mime_types_for(from_settings) ⇒ Object
22 23 24 25 26 |
# File 'lib/grape/content_types.rb', line 22 def mime_types_for(from_settings) return MIME_TYPES if from_settings == Grape::ContentTypes::DEFAULTS MimeTypesCache[from_settings] end |