Module: MediaTypes::Deserialization

Extended by:
ActiveSupport::Concern
Defined in:
lib/media_types/deserialization.rb,
lib/media_types/deserialization/error.rb,
lib/media_types/deserialization/version.rb,
lib/media_types/deserialization/content_format_error.rb,
lib/media_types/deserialization/content_type_not_recognised.rb,
lib/media_types/deserialization/content_does_not_match_content_type.rb

Defined Under Namespace

Classes: ContentDoesNotMatchContentType, ContentFormatError, ContentTypeNotRecognised, Error

Constant Summary collapse

PARAMETERS_KEY =
'api.media_type_deserializer.request.parameters'
DEFAULT_JSON_DESERIALIZER =
lambda do |raw_post|
  require 'oj'
  data = Oj.load(raw_post, Oj.default_options) || {}
  data.is_a?(::Hash) ? data : { _json: data }
end
DEFAULT_LOOKUP_TABLE =
{
  'application/json' => :json,
  'text/xml' => :xml,
  'text/html' => :html
}
VERSION =
'0.1.3'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(&block) ⇒ Object



37
38
39
# File 'lib/media_types/deserialization.rb', line 37

def self.configure(&block)
  block_given? ? instance_exec(self, &block) : self
end

Instance Method Details

#media_type_paramsObject

Returns both GET and POST parameters in a single hash.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/media_types/deserialization.rb', line 42

def media_type_params
  # If this has been parsed before, e.g. in a middleware, return directly
  previous_params = request.get_header(PARAMETERS_KEY)
  return previous_params if previous_params

  deserialize_content.tap do |params|
    params.merge!(request.path_parameters)
    request.set_header(PARAMETERS_KEY, params)
  end
rescue StandardError => ex
  if defined?(::Oj::Error) && ex.is_a?(::Oj::Error)
    raise ContentFormatError, 'Body is not valid JSON: ' + ex.message
  end

  raise ex
end