Sha256: 25047a7b170a474d5623452ece487b79d9118ce2a80bc99c34008b2453e181c3

Contents?: true

Size: 1.39 KB

Versions: 87

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module ActionDispatch
  class RequestEncoder # :nodoc:
    class IdentityEncoder
      def content_type; end
      def accept_header; end
      def encode_params(params); params; end
      def response_parser; -> body { body }; end
    end

    @encoders = { identity: IdentityEncoder.new }

    attr_reader :response_parser

    def initialize(mime_name, param_encoder, response_parser)
      @mime = Mime[mime_name]

      unless @mime
        raise ArgumentError, "Can't register a request encoder for " \
          "unregistered MIME Type: #{mime_name}. See `Mime::Type.register`."
      end

      @response_parser = response_parser || -> body { body }
      @param_encoder   = param_encoder   || :"to_#{@mime.symbol}".to_proc
    end

    def content_type
      @mime.to_s
    end

    def accept_header
      @mime.to_s
    end

    def encode_params(params)
      @param_encoder.call(params) if params
    end

    def self.parser(content_type)
      mime = Mime::Type.lookup(content_type)
      encoder(mime ? mime.ref : nil).response_parser
    end

    def self.encoder(name)
      @encoders[name] || @encoders[:identity]
    end

    def self.register_encoder(mime_name, param_encoder: nil, response_parser: nil)
      @encoders[mime_name] = new(mime_name, param_encoder, response_parser)
    end

    register_encoder :json, response_parser: -> body { JSON.parse(body) }
  end
end

Version data entries

87 entries across 87 versions & 4 rubygems

Version Path
actionpack-5.2.8.1 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.8 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.7.1 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.7 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.6.3 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.6.2 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.6.1 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.6 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.4.6 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.5 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.4.5 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.4.4 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.4.3 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.4.2 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.4.1 lib/action_dispatch/testing/request_encoder.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.4 lib/action_dispatch/testing/request_encoder.rb
actionpack-5.2.4.rc1 lib/action_dispatch/testing/request_encoder.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/testing/request_encoder.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/testing/request_encoder.rb