Sha256: b894b9bcbae483345e6538399077ea1c04e98c855aea261fdac8977300921ccb

Contents?: true

Size: 1.04 KB

Versions: 34

Compression:

Stored size: 1.04 KB

Contents

require 'json'

module JSONAPI
  MEDIA_TYPE = 'application/vnd.api+json'

  module MimeTypes
    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

    def self.parser
      lambda do |body|
        begin
          data = JSON.parse(body)
          if data.is_a?(Hash)
            data.with_indifferent_access
          else
            fail JSONAPI::Exceptions::InvalidRequestFormat.new
          end
        rescue JSON::ParserError => e
          { _parser_exception: JSONAPI::Exceptions::BadRequest.new(e.to_s)  }
        rescue => e
          { _parser_exception: e }
        end
      end
    end
  end

  MimeTypes.install
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
jsonapi-resources-0.10.7 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.6 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.5 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.4 lib/jsonapi/mime_types.rb
jsonapi-resources-0.9.12 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.3 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.2 lib/jsonapi/mime_types.rb
jsonapi-resources-0.9.11 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.1 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.0 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.0.beta9 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.0.beta8 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.0.beta7 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.0.beta6 lib/jsonapi/mime_types.rb
jsonapi-resources-0.9.10 lib/jsonapi/mime_types.rb
jsonapi-resources-0.9.9 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.0.beta5 lib/jsonapi/mime_types.rb
jsonapi-resources-0.9.8 lib/jsonapi/mime_types.rb
jsonapi-resources-0.10.0.beta4 lib/jsonapi/mime_types.rb
jsonapi-resources-0.9.7 lib/jsonapi/mime_types.rb