Sha256: 0145f04c2cad981ef04da532753fd2e9015c7cf679514deedf5b611a48c03eb9

Contents?: true

Size: 968 Bytes

Versions: 6

Compression:

Stored size: 968 Bytes

Contents

module Faraday
  class Request::UrlEncoded < Faraday::Middleware
    CONTENT_TYPE = 'Content-Type'.freeze unless defined? CONTENT_TYPE

    class << self
      attr_accessor :mime_type
    end
    self.mime_type = 'application/x-www-form-urlencoded'.freeze

    def call(env)
      match_content_type(env) do |data|
        params = Faraday::Utils::ParamsHash[data]
        env.body = params.to_query(env.params_encoder)
      end
      @app.call env
    end

    def match_content_type(env)
      if process_request?(env)
        env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
        yield env.body unless env.body.respond_to?(:to_str)
      end
    end

    def process_request?(env)
      type = request_type(env)
      env.body and (type.empty? or type == self.class.mime_type)
    end

    def request_type(env)
      type = env.request_headers[CONTENT_TYPE].to_s
      type = type.split(';', 2).first if type.index(';')
      type
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
faraday-0.9.0.rc6 lib/faraday/request/url_encoded.rb
faraday-0.9.0.rc5 lib/faraday/request/url_encoded.rb
faraday-0.9.0.rc4 lib/faraday/request/url_encoded.rb
faraday-0.9.0.rc3 lib/faraday/request/url_encoded.rb
faraday-0.9.0.rc2 lib/faraday/request/url_encoded.rb
faraday-0.9.0.rc1 lib/faraday/request/url_encoded.rb