Class: Faraday::Request::UrlEncoded

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

Direct Known Subclasses

Multipart

Constant Summary

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

#lookup_middleware, #register_middleware

Constructor Details

This class inherits a constructor from Faraday::Middleware

Class Attribute Details

+ (Object) mime_type

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

- (Object) call(env)



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

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

- (Object) match_content_type(env)



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

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

- (Boolean) process_request?(env)

Returns:

  • (Boolean)


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

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

- (Object) request_type(env)



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

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