Module: Grape::ContentTypes

Defined in:
lib/grape/content_types.rb

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

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

.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.



33
34
35
36
37
38
# File 'lib/grape/content_types.rb', line 33

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

  from_settings.invert.transform_keys! { |k| media_type(k) }
end