Class: Faraday::Request::UrlEncoded

Inherits:
Middleware show all
Defined in:
lib/faraday/request/url_encoded.rb

Direct Known Subclasses

Multipart

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'.freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

dependency, inherited, #initialize, loaded?, new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware

Constructor Details

This class inherits a constructor from Faraday::Middleware

Class Attribute Details

.mime_typeObject

Returns the value of attribute mime_type.



6
7
8
# File 'lib/faraday/request/url_encoded.rb', line 6

def mime_type
  @mime_type
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/faraday/request/url_encoded.rb', line 10

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

#match_content_type(env) ⇒ Object



18
19
20
21
22
23
# File 'lib/faraday/request/url_encoded.rb', line 18

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

#process_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/faraday/request/url_encoded.rb', line 25

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

#request_type(env) ⇒ Object



30
31
32
33
34
# File 'lib/faraday/request/url_encoded.rb', line 30

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