Sha256: fba7a872e745480e21f3509c3b52dd6519817b74edaff3cca6d6b9d462e26bc1

Contents?: true

Size: 1.39 KB

Versions: 119

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)
      type = Mime::Type.lookup(content_type).ref if content_type
      encoder(type).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

119 entries across 113 versions & 10 rubygems

Version Path
actionpack-7.0.8.7 lib/action_dispatch/testing/request_encoder.rb
actionpack-7.0.8.6 lib/action_dispatch/testing/request_encoder.rb
actionpack-6.1.7.10 lib/action_dispatch/testing/request_encoder.rb
actionpack-6.1.7.9 lib/action_dispatch/testing/request_encoder.rb
actionpack-7.0.8.5 lib/action_dispatch/testing/request_encoder.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.8.4/lib/action_dispatch/testing/request_encoder.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/request_encoder.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/request_encoder.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/testing/request_encoder.rb
actionpack-7.0.8.4 lib/action_dispatch/testing/request_encoder.rb
actionpack-6.1.7.8 lib/action_dispatch/testing/request_encoder.rb
actionpack-7.0.8.1 lib/action_dispatch/testing/request_encoder.rb
actionpack-6.1.7.7 lib/action_dispatch/testing/request_encoder.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.2.3/lib/action_dispatch/testing/request_encoder.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-6.1.6.1/lib/action_dispatch/testing/request_encoder.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionpack-7.0.3.1/lib/action_dispatch/testing/request_encoder.rb
actionpack-7.0.8 lib/action_dispatch/testing/request_encoder.rb
actionpack-7.0.7.2 lib/action_dispatch/testing/request_encoder.rb
actionpack-6.1.7.6 lib/action_dispatch/testing/request_encoder.rb
actionpack-7.0.7.1 lib/action_dispatch/testing/request_encoder.rb