Class: Faraday::Adapter

Inherits:
Middleware show all
Extended by:
AutoloadHelper
Defined in:
lib/faraday/adapter.rb,
lib/faraday/adapter/test.rb,
lib/faraday/adapter/patron.rb,
lib/faraday/adapter/net_http.rb,
lib/faraday/adapter/typhoeus.rb,
lib/faraday/adapter/em_synchrony.rb,
lib/faraday/adapter/action_dispatch.rb

Direct Known Subclasses

ActionDispatch, EMSynchrony, NetHttp, Patron, Typhoeus

Defined Under Namespace

Classes: ActionDispatch, EMSynchrony, NetHttp, Patron, Test, Typhoeus

Constant Summary collapse

FORM_TYPE =
'application/x-www-form-urlencoded'.freeze
MULTIPART_TYPE =
'multipart/form-data'.freeze
CONTENT_TYPE =
'Content-Type'.freeze
DEFAULT_BOUNDARY =
"-----------RubyMultipartPost".freeze

Instance Method Summary collapse

Methods included from AutoloadHelper

all_loaded_constants, autoload_all, load_autoloaded_constants, lookup_module, register_lookup_modules

Methods inherited from Middleware

#initialize, loaded?, setup_parallel_manager

Constructor Details

This class inherits a constructor from Faraday::Middleware

Instance Method Details

#call(env) ⇒ Object



25
26
27
# File 'lib/faraday/adapter.rb', line 25

def call(env)
  process_body_for_request(env)
end

#create_multipart(env, params, boundary = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/faraday/adapter.rb', line 69

def create_multipart(env, params, boundary = nil)
  boundary ||= env[:request][:boundary]
  parts      = []
  process_to_params(parts, params) do |key, value|
    Faraday::Parts::Part.new(boundary, key, value)
  end
  parts     << Faraday::Parts::EpiloguePart.new(boundary)
  env[:request_headers]['Content-Length'] = parts.inject(0) {|sum,i| sum + i.length }.to_s
  Faraday::CompositeReadIO.new(*parts.map{|p| p.to_io })
end

#full_path_for(path, query = nil, fragment = nil) ⇒ Object

assume that query and fragment are already encoded properly



97
98
99
100
101
102
103
104
105
106
# File 'lib/faraday/adapter.rb', line 97

def full_path_for(path, query = nil, fragment = nil)
  full_path = path.dup
  if query && !query.empty?
    full_path << "?#{query}"
  end
  if fragment && !fragment.empty?
    full_path << "##{fragment}"
  end
  full_path
end

#has_multipart?(body) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
# File 'lib/faraday/adapter.rb', line 58

def has_multipart?(body)
  body.values.each do |v|
    if v.respond_to?(:content_type)
      return true
    elsif v.respond_to?(:values)
      return true if has_multipart?(v)
    end
  end
  false
end

#process_body_for_request(env, body = env[:body], headers = env[:request_headers]) ⇒ Object

Converts a body hash into encoded form params. This is done as late as possible in the request cycle in case some other middleware wants to act on the request before sending it out.

env - The current request environment Hash. body - A Hash of keys/values. Strings and empty values will be

ignored.  Default: env[:body]

headers - The Hash of request headers. Default: env

Returns nothing. If the body is processed, it is replaced in the environment for you.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/faraday/adapter.rb', line 40

def process_body_for_request(env, body = env[:body], headers = env[:request_headers])
  return if body.nil? || body.empty? || !body.respond_to?(:each_key)
  if has_multipart?(body)
    env[:request]            ||= {}
    env[:request][:boundary] ||= DEFAULT_BOUNDARY
    headers[CONTENT_TYPE]      = MULTIPART_TYPE + ";boundary=#{env[:request][:boundary]}"
    env[:body] = create_multipart(env, body)
  else
    type = headers[CONTENT_TYPE]
    headers[CONTENT_TYPE] = FORM_TYPE if type.nil? || type.empty?
    parts = []
    process_to_params(parts, env[:body]) do |key, value|
      "#{key}=#{escape(value.to_s)}"
    end
    env[:body] = parts * "&"
  end
end

#process_to_params(pieces, params, base = nil, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/faraday/adapter.rb', line 80

def process_to_params(pieces, params, base = nil, &block)
  params.to_a.each do |key, value|
    key_str = base ? "#{base}[#{key}]" : key

    case value
    when Array
      values = value.inject([]) { |a,v| a << [nil, v] }
      process_to_params(pieces, values, key_str, &block)
    when Hash
      process_to_params(pieces, value, key_str, &block)
    else
      pieces << block.call(key_str, value)
    end
  end
end