Module: JSONAPI::MimeTypes

Defined in:
lib/jsonapi/mime_types.rb

Class Method Summary collapse

Class Method Details

.installObject


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/jsonapi/mime_types.rb', line 5

def self.install
  Mime::Type.register JSONAPI::MEDIA_TYPE, :api_json

  # :nocov:
  if Rails::VERSION::MAJOR >= 5
    parsers = ActionDispatch::Request.parameter_parsers.merge(
      Mime::Type.lookup(JSONAPI::MEDIA_TYPE).symbol => parser
    )
    ActionDispatch::Request.parameter_parsers = parsers
  else
    ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MEDIA_TYPE)] = parser
  end
  # :nocov:
end

.parserObject


20
21
22
23
24
25
26
# File 'lib/jsonapi/mime_types.rb', line 20

def self.parser
  lambda do |body|
    data = JSON.parse(body)
    data = {:_json => data} unless data.is_a?(Hash)
    data.with_indifferent_access
  end
end