faraday 0.9.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +276 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +27 -217
  5. data/Rakefile +7 -0
  6. data/UPGRADING.md +55 -0
  7. data/examples/client_spec.rb +65 -0
  8. data/examples/client_test.rb +79 -0
  9. data/lib/faraday/adapter/em_http.rb +149 -101
  10. data/lib/faraday/adapter/em_http_ssl_patch.rb +25 -19
  11. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  12. data/lib/faraday/adapter/em_synchrony.rb +106 -56
  13. data/lib/faraday/adapter/excon.rb +98 -55
  14. data/lib/faraday/adapter/httpclient.rb +89 -55
  15. data/lib/faraday/adapter/net_http.rb +132 -53
  16. data/lib/faraday/adapter/net_http_persistent.rb +70 -28
  17. data/lib/faraday/adapter/patron.rb +92 -38
  18. data/lib/faraday/adapter/rack.rb +30 -13
  19. data/lib/faraday/adapter/test.rb +136 -52
  20. data/lib/faraday/adapter/typhoeus.rb +7 -115
  21. data/lib/faraday/adapter.rb +89 -20
  22. data/lib/faraday/adapter_registry.rb +28 -0
  23. data/lib/faraday/autoload.rb +47 -36
  24. data/lib/faraday/connection.rb +345 -168
  25. data/lib/faraday/dependency_loader.rb +37 -0
  26. data/lib/faraday/encoders/flat_params_encoder.rb +98 -0
  27. data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
  28. data/lib/faraday/error.rb +108 -29
  29. data/lib/faraday/file_part.rb +128 -0
  30. data/lib/faraday/logging/formatter.rb +105 -0
  31. data/lib/faraday/middleware.rb +12 -28
  32. data/lib/faraday/middleware_registry.rb +129 -0
  33. data/lib/faraday/options/connection_options.rb +22 -0
  34. data/lib/faraday/options/env.rb +181 -0
  35. data/lib/faraday/options/proxy_options.rb +28 -0
  36. data/lib/faraday/options/request_options.rb +22 -0
  37. data/lib/faraday/options/ssl_options.rb +59 -0
  38. data/lib/faraday/options.rb +57 -194
  39. data/lib/faraday/param_part.rb +53 -0
  40. data/lib/faraday/parameters.rb +4 -196
  41. data/lib/faraday/rack_builder.rb +74 -39
  42. data/lib/faraday/request/authorization.rb +42 -31
  43. data/lib/faraday/request/basic_authentication.rb +14 -7
  44. data/lib/faraday/request/instrumentation.rb +45 -27
  45. data/lib/faraday/request/multipart.rb +81 -45
  46. data/lib/faraday/request/retry.rb +211 -126
  47. data/lib/faraday/request/token_authentication.rb +15 -10
  48. data/lib/faraday/request/url_encoded.rb +41 -23
  49. data/lib/faraday/request.rb +84 -30
  50. data/lib/faraday/response/logger.rb +22 -48
  51. data/lib/faraday/response/raise_error.rb +38 -14
  52. data/lib/faraday/response.rb +27 -16
  53. data/lib/faraday/utils/headers.rb +139 -0
  54. data/lib/faraday/utils/params_hash.rb +61 -0
  55. data/lib/faraday/utils.rb +28 -228
  56. data/lib/faraday.rb +102 -204
  57. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  58. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  59. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  60. data/spec/faraday/adapter/excon_spec.rb +49 -0
  61. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  62. data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
  63. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  64. data/spec/faraday/adapter/patron_spec.rb +18 -0
  65. data/spec/faraday/adapter/rack_spec.rb +8 -0
  66. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  67. data/spec/faraday/adapter_registry_spec.rb +28 -0
  68. data/spec/faraday/adapter_spec.rb +55 -0
  69. data/spec/faraday/composite_read_io_spec.rb +80 -0
  70. data/spec/faraday/connection_spec.rb +691 -0
  71. data/spec/faraday/error_spec.rb +45 -0
  72. data/spec/faraday/middleware_spec.rb +26 -0
  73. data/spec/faraday/options/env_spec.rb +70 -0
  74. data/spec/faraday/options/options_spec.rb +297 -0
  75. data/spec/faraday/options/proxy_options_spec.rb +37 -0
  76. data/spec/faraday/options/request_options_spec.rb +19 -0
  77. data/spec/faraday/params_encoders/flat_spec.rb +34 -0
  78. data/spec/faraday/params_encoders/nested_spec.rb +134 -0
  79. data/spec/faraday/rack_builder_spec.rb +196 -0
  80. data/spec/faraday/request/authorization_spec.rb +88 -0
  81. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  82. data/spec/faraday/request/multipart_spec.rb +274 -0
  83. data/spec/faraday/request/retry_spec.rb +242 -0
  84. data/spec/faraday/request/url_encoded_spec.rb +70 -0
  85. data/spec/faraday/request_spec.rb +109 -0
  86. data/spec/faraday/response/logger_spec.rb +220 -0
  87. data/spec/faraday/response/middleware_spec.rb +52 -0
  88. data/spec/faraday/response/raise_error_spec.rb +106 -0
  89. data/spec/faraday/response_spec.rb +75 -0
  90. data/spec/faraday/utils/headers_spec.rb +82 -0
  91. data/spec/faraday/utils_spec.rb +56 -0
  92. data/spec/faraday_spec.rb +37 -0
  93. data/spec/spec_helper.rb +132 -0
  94. data/spec/support/disabling_stub.rb +14 -0
  95. data/spec/support/fake_safe_buffer.rb +15 -0
  96. data/spec/support/helper_methods.rb +133 -0
  97. data/spec/support/shared_examples/adapter.rb +104 -0
  98. data/spec/support/shared_examples/params_encoder.rb +18 -0
  99. data/spec/support/shared_examples/request_method.rb +234 -0
  100. data/spec/support/streaming_response_checker.rb +35 -0
  101. data/spec/support/webmock_rack_app.rb +68 -0
  102. metadata +110 -64
  103. data/lib/faraday/upload_io.rb +0 -67
data/lib/faraday/utils.rb CHANGED
@@ -1,176 +1,12 @@
1
- require 'thread'
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday/utils/headers'
4
+ require 'faraday/utils/params_hash'
2
5
 
3
6
  module Faraday
7
+ # Utils contains various static helper methods.
4
8
  module Utils
5
- extend self
6
-
7
- # Adapted from Rack::Utils::HeaderHash
8
- class Headers < ::Hash
9
- def self.from(value)
10
- new(value)
11
- end
12
-
13
- def initialize(hash = nil)
14
- super()
15
- @names = {}
16
- self.update(hash || {})
17
- end
18
-
19
- # on dup/clone, we need to duplicate @names hash
20
- def initialize_copy(other)
21
- super
22
- @names = other.names.dup
23
- end
24
-
25
- # need to synchronize concurrent writes to the shared KeyMap
26
- keymap_mutex = Mutex.new
27
-
28
- # symbol -> string mapper + cache
29
- KeyMap = Hash.new do |map, key|
30
- value = if key.respond_to?(:to_str)
31
- key
32
- else
33
- key.to_s.split('_'). # :user_agent => %w(user agent)
34
- each { |w| w.capitalize! }. # => %w(User Agent)
35
- join('-') # => "User-Agent"
36
- end
37
- keymap_mutex.synchronize { map[key] = value }
38
- end
39
- KeyMap[:etag] = "ETag"
40
-
41
- def [](k)
42
- k = KeyMap[k]
43
- super(k) || super(@names[k.downcase])
44
- end
45
-
46
- def []=(k, v)
47
- k = KeyMap[k]
48
- k = (@names[k.downcase] ||= k)
49
- # join multiple values with a comma
50
- v = v.to_ary.join(', ') if v.respond_to? :to_ary
51
- super(k, v)
52
- end
53
-
54
- def fetch(k, *args, &block)
55
- k = KeyMap[k]
56
- key = @names.fetch(k.downcase, k)
57
- super(key, *args, &block)
58
- end
59
-
60
- def delete(k)
61
- k = KeyMap[k]
62
- if k = @names[k.downcase]
63
- @names.delete k.downcase
64
- super(k)
65
- end
66
- end
67
-
68
- def include?(k)
69
- @names.include? k.downcase
70
- end
71
-
72
- alias_method :has_key?, :include?
73
- alias_method :member?, :include?
74
- alias_method :key?, :include?
75
-
76
- def merge!(other)
77
- other.each { |k, v| self[k] = v }
78
- self
79
- end
80
- alias_method :update, :merge!
81
-
82
- def merge(other)
83
- hash = dup
84
- hash.merge! other
85
- end
86
-
87
- def replace(other)
88
- clear
89
- @names.clear
90
- self.update other
91
- self
92
- end
93
-
94
- def to_hash() ::Hash.new.update(self) end
95
-
96
- def parse(header_string)
97
- return unless header_string && !header_string.empty?
98
- header_string.split(/\r\n/).
99
- tap { |a| a.shift if a.first.index('HTTP/') == 0 }. # drop the HTTP status line
100
- map { |h| h.split(/:\s+/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines
101
- each { |key, value|
102
- # join multiple values with a comma
103
- if self[key]
104
- self[key] << ', ' << value
105
- else
106
- self[key] = value
107
- end
108
- }
109
- end
110
-
111
- protected
112
-
113
- def names
114
- @names
115
- end
116
- end
117
-
118
- # hash with stringified keys
119
- class ParamsHash < Hash
120
- def [](key)
121
- super(convert_key(key))
122
- end
123
-
124
- def []=(key, value)
125
- super(convert_key(key), value)
126
- end
127
-
128
- def delete(key)
129
- super(convert_key(key))
130
- end
131
-
132
- def include?(key)
133
- super(convert_key(key))
134
- end
135
-
136
- alias_method :has_key?, :include?
137
- alias_method :member?, :include?
138
- alias_method :key?, :include?
139
-
140
- def update(params)
141
- params.each do |key, value|
142
- self[key] = value
143
- end
144
- self
145
- end
146
- alias_method :merge!, :update
147
-
148
- def merge(params)
149
- dup.update(params)
150
- end
151
-
152
- def replace(other)
153
- clear
154
- update(other)
155
- end
156
-
157
- def merge_query(query, encoder = nil)
158
- if query && !query.empty?
159
- update((encoder || Utils.default_params_encoder).decode(query))
160
- end
161
- self
162
- end
163
-
164
- def to_query(encoder = nil)
165
- (encoder || Utils.default_params_encoder).encode(self)
166
- end
167
-
168
- private
169
-
170
- def convert_key(key)
171
- key.to_s
172
- end
173
- end
9
+ module_function
174
10
 
175
11
  def build_query(params)
176
12
  FlatParamsEncoder.encode(params)
@@ -180,17 +16,19 @@ module Faraday
180
16
  NestedParamsEncoder.encode(params)
181
17
  end
182
18
 
183
- ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/
19
+ ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/.freeze
184
20
 
185
- def escape(s)
186
- s.to_s.gsub(ESCAPE_RE) {|match|
21
+ def escape(str)
22
+ str.to_s.gsub(ESCAPE_RE) do |match|
187
23
  '%' + match.unpack('H2' * match.bytesize).join('%').upcase
188
- }.tr(' ', '+')
24
+ end.tr(' ', '+')
189
25
  end
190
26
 
191
- def unescape(s) CGI.unescape s.to_s end
27
+ def unescape(str)
28
+ CGI.unescape str.to_s
29
+ end
192
30
 
193
- DEFAULT_SEP = /[&;] */n
31
+ DEFAULT_SEP = /[&;] */n.freeze
194
32
 
195
33
  # Adapted from Rack
196
34
  def parse_query(query)
@@ -209,55 +47,18 @@ module Faraday
209
47
  attr_writer :default_params_encoder
210
48
  end
211
49
 
212
- # Stolen from Rack
213
- def normalize_params(params, name, v = nil)
214
- name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
215
- k = $1 || ''
216
- after = $' || ''
217
-
218
- return if k.empty?
219
-
220
- if after == ""
221
- if params[k]
222
- params[k] = Array[params[k]] unless params[k].kind_of?(Array)
223
- params[k] << v
224
- else
225
- params[k] = v
226
- end
227
- elsif after == "[]"
228
- params[k] ||= []
229
- raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
230
- params[k] << v
231
- elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
232
- child_key = $1
233
- params[k] ||= []
234
- raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
235
- if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
236
- normalize_params(params[k].last, child_key, v)
237
- else
238
- params[k] << normalize_params({}, child_key, v)
239
- end
240
- else
241
- params[k] ||= {}
242
- raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash)
243
- params[k] = normalize_params(params[k], after, v)
244
- end
245
-
246
- return params
247
- end
248
-
249
50
  # Normalize URI() behavior across Ruby versions
250
51
  #
251
52
  # url - A String or URI.
252
53
  #
253
54
  # Returns a parsed URI.
254
- def URI(url)
55
+ def URI(url) # rubocop:disable Naming/MethodName
255
56
  if url.respond_to?(:host)
256
57
  url
257
58
  elsif url.respond_to?(:to_str)
258
59
  default_uri_parser.call(url)
259
60
  else
260
- raise ArgumentError, "bad argument (expected URI object or URI string)"
61
+ raise ArgumentError, 'bad argument (expected URI object or URI string)'
261
62
  end
262
63
  end
263
64
 
@@ -270,27 +71,28 @@ module Faraday
270
71
 
271
72
  def default_uri_parser=(parser)
272
73
  @default_uri_parser = if parser.respond_to?(:call) || parser.nil?
273
- parser
274
- else
275
- parser.method(:parse)
276
- end
74
+ parser
75
+ else
76
+ parser.method(:parse)
77
+ end
277
78
  end
278
79
 
279
- # Receives a String or URI and returns just the path with the query string sorted.
80
+ # Receives a String or URI and returns just
81
+ # the path with the query string sorted.
280
82
  def normalize_path(url)
281
83
  url = URI(url)
282
84
  (url.path.start_with?('/') ? url.path : '/' + url.path) +
283
- (url.query ? "?#{sort_query_params(url.query)}" : "")
85
+ (url.query ? "?#{sort_query_params(url.query)}" : '')
284
86
  end
285
87
 
286
88
  # Recursive hash update
287
89
  def deep_merge!(target, hash)
288
90
  hash.each do |key, value|
289
- if Hash === value and Hash === target[key]
290
- target[key] = deep_merge(target[key], value)
291
- else
292
- target[key] = value
293
- end
91
+ target[key] = if value.is_a?(Hash) && target[key].is_a?(Hash)
92
+ deep_merge(target[key], value)
93
+ else
94
+ value
95
+ end
294
96
  end
295
97
  target
296
98
  end
@@ -300,8 +102,6 @@ module Faraday
300
102
  deep_merge!(source.dup, hash)
301
103
  end
302
104
 
303
- protected
304
-
305
105
  def sort_query_params(query)
306
106
  query.split('&').sort.join('&')
307
107
  end
data/lib/faraday.rb CHANGED
@@ -1,130 +1,156 @@
1
- require 'thread'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'cgi'
3
4
  require 'set'
4
5
  require 'forwardable'
6
+ require 'faraday/middleware_registry'
7
+ require 'faraday/dependency_loader'
5
8
 
6
- # Public: This is the main namespace for Faraday. You can either use it to
7
- # create Faraday::Connection objects, or access it directly.
9
+ # This is the main namespace for Faraday.
8
10
  #
9
- # Examples
11
+ # It provides methods to create {Connection} objects, and HTTP-related
12
+ # methods to use directly.
10
13
  #
14
+ # @example Helpful class methods for easy usage
11
15
  # Faraday.get "http://faraday.com"
12
16
  #
17
+ # @example Helpful class method `.new` to create {Connection} objects.
13
18
  # conn = Faraday.new "http://faraday.com"
14
19
  # conn.get '/'
15
20
  #
16
21
  module Faraday
17
- VERSION = "0.9.2"
22
+ VERSION = '1.0.0'
23
+ METHODS_WITH_QUERY = %w[get head delete trace].freeze
24
+ METHODS_WITH_BODY = %w[post put patch].freeze
18
25
 
19
26
  class << self
20
- # Public: Gets or sets the root path that Faraday is being loaded from.
21
- # This is the root from where the libraries are auto-loaded from.
27
+ # The root path that Faraday is being loaded from.
28
+ #
29
+ # This is the root from where the libraries are auto-loaded.
30
+ #
31
+ # @return [String]
22
32
  attr_accessor :root_path
23
33
 
24
- # Public: Gets or sets the path that the Faraday libs are loaded from.
34
+ # Gets or sets the path that the Faraday libs are loaded from.
35
+ # @return [String]
25
36
  attr_accessor :lib_path
26
37
 
27
- # Public: Gets or sets the Symbol key identifying a default Adapter to use
28
- # for the default Faraday::Connection.
38
+ # @overload default_adapter
39
+ # Gets the Symbol key identifying a default Adapter to use
40
+ # for the default {Faraday::Connection}. Defaults to `:net_http`.
41
+ # @return [Symbol] the default adapter
42
+ # @overload default_adapter=(adapter)
43
+ # Updates default adapter while resetting {.default_connection}.
44
+ # @return [Symbol] the new default_adapter.
29
45
  attr_reader :default_adapter
30
46
 
31
- # Public: Sets the default Faraday::Connection for simple scripts that
32
- # access the Faraday constant directly.
33
- #
34
- # Faraday.get "https://faraday.com"
47
+ # Documented below, see default_connection
35
48
  attr_writer :default_connection
36
49
 
37
- # Public: Sets the default options used when calling Faraday#new.
38
- attr_writer :default_connection_options
39
-
40
- # Public: Initializes a new Faraday::Connection.
41
- #
42
- # url - The optional String base URL to use as a prefix for all
43
- # requests. Can also be the options Hash.
44
- # options - The optional Hash used to configure this Faraday::Connection.
45
- # Any of these values will be set on every request made, unless
46
- # overridden for a specific request.
47
- # :url - String base URL.
48
- # :params - Hash of URI query unencoded key/value pairs.
49
- # :headers - Hash of unencoded HTTP header key/value pairs.
50
- # :request - Hash of request options.
51
- # :ssl - Hash of SSL options.
52
- # :proxy - Hash of Proxy options.
53
- #
54
- # Examples
55
- #
50
+ # Tells Faraday to ignore the environment proxy (http_proxy).
51
+ # Defaults to `false`.
52
+ # @return [Boolean]
53
+ attr_accessor :ignore_env_proxy
54
+
55
+ # Initializes a new {Connection}.
56
+ #
57
+ # @param url [String,Hash] The optional String base URL to use as a prefix
58
+ # for all requests. Can also be the options Hash. Any of these
59
+ # values will be set on every request made, unless overridden
60
+ # for a specific request.
61
+ # @param options [Hash]
62
+ # @option options [String] :url Base URL
63
+ # @option options [Hash] :params Hash of unencoded URI query params.
64
+ # @option options [Hash] :headers Hash of unencoded HTTP headers.
65
+ # @option options [Hash] :request Hash of request options.
66
+ # @option options [Hash] :ssl Hash of SSL options.
67
+ # @option options [Hash] :proxy Hash of Proxy options.
68
+ # @return [Faraday::Connection]
69
+ #
70
+ # @example With an URL argument
56
71
  # Faraday.new 'http://faraday.com'
57
- #
58
- # # http://faraday.com?page=1
59
- # Faraday.new 'http://faraday.com', :params => {:page => 1}
60
- #
61
- # # same
62
- #
63
- # Faraday.new :url => 'http://faraday.com',
64
- # :params => {:page => 1}
65
- #
66
- # Returns a Faraday::Connection.
67
- def new(url = nil, options = nil)
68
- block = block_given? ? Proc.new : nil
69
- options = options ? default_connection_options.merge(options) : default_connection_options.dup
72
+ # # => Faraday::Connection to http://faraday.com
73
+ #
74
+ # @example With an URL argument and an options hash
75
+ # Faraday.new 'http://faraday.com', params: { page: 1 }
76
+ # # => Faraday::Connection to http://faraday.com?page=1
77
+ #
78
+ # @example With everything in an options hash
79
+ # Faraday.new url: 'http://faraday.com',
80
+ # params: { page: 1 }
81
+ # # => Faraday::Connection to http://faraday.com?page=1
82
+ def new(url = nil, options = {}, &block)
83
+ options = default_connection_options.merge(options)
70
84
  Faraday::Connection.new(url, options, &block)
71
85
  end
72
86
 
87
+ # @private
73
88
  # Internal: Requires internal Faraday libraries.
74
89
  #
75
- # *libs - One or more relative String names to Faraday classes.
76
- #
77
- # Returns nothing.
90
+ # @param libs [Array] one or more relative String names to Faraday classes.
91
+ # @return [void]
78
92
  def require_libs(*libs)
79
93
  libs.each do |lib|
80
94
  require "#{lib_path}/#{lib}"
81
95
  end
82
96
  end
83
97
 
84
- # Public: Updates default adapter while resetting
85
- # #default_connection.
86
- #
87
- # Returns the new default_adapter.
98
+ alias require_lib require_libs
99
+
100
+ # Documented elsewhere, see default_adapter reader
88
101
  def default_adapter=(adapter)
89
102
  @default_connection = nil
90
103
  @default_adapter = adapter
91
104
  end
92
105
 
93
- alias require_lib require_libs
106
+ def respond_to_missing?(symbol, include_private = false)
107
+ default_connection.respond_to?(symbol, include_private) || super
108
+ end
109
+
110
+ private
94
111
 
95
- private
96
112
  # Internal: Proxies method calls on the Faraday constant to
97
- # #default_connection.
113
+ # .default_connection.
98
114
  def method_missing(name, *args, &block)
99
- default_connection.send(name, *args, &block)
115
+ if default_connection.respond_to?(name)
116
+ default_connection.send(name, *args, &block)
117
+ else
118
+ super
119
+ end
100
120
  end
101
121
  end
102
122
 
103
- self.root_path = File.expand_path "..", __FILE__
104
- self.lib_path = File.expand_path "../faraday", __FILE__
123
+ self.ignore_env_proxy = false
124
+ self.root_path = File.expand_path __dir__
125
+ self.lib_path = File.expand_path 'faraday', __dir__
105
126
  self.default_adapter = :net_http
106
127
 
107
- # Gets the default connection used for simple scripts.
108
- #
109
- # Returns a Faraday::Connection, configured with the #default_adapter.
128
+ # @overload default_connection
129
+ # Gets the default connection used for simple scripts.
130
+ # @return [Faraday::Connection] a connection configured with
131
+ # the default_adapter.
132
+ # @overload default_connection=(connection)
133
+ # @param connection [Faraday::Connection]
134
+ # Sets the default {Faraday::Connection} for simple scripts that
135
+ # access the Faraday constant directly, such as
136
+ # <code>Faraday.get "https://faraday.com"</code>.
110
137
  def self.default_connection
111
- @default_connection ||= Connection.new
138
+ @default_connection ||= Connection.new(default_connection_options)
112
139
  end
113
140
 
114
- # Gets the default connection options used when calling Faraday#new.
141
+ # Gets the default connection options used when calling {Faraday#new}.
115
142
  #
116
- # Returns a Faraday::ConnectionOptions.
143
+ # @return [Faraday::ConnectionOptions]
117
144
  def self.default_connection_options
118
145
  @default_connection_options ||= ConnectionOptions.new
119
146
  end
120
147
 
121
- if (!defined?(RUBY_ENGINE) || "ruby" == RUBY_ENGINE) && RUBY_VERSION < '1.9'
122
- begin
123
- require 'system_timer'
124
- Timer = SystemTimer
125
- rescue LoadError
126
- warn "Faraday: you may want to install system_timer for reliable timeouts"
127
- end
148
+ # Sets the default options used when calling {Faraday#new}.
149
+ #
150
+ # @param options [Hash, Faraday::ConnectionOptions]
151
+ def self.default_connection_options=(options)
152
+ @default_connection = nil
153
+ @default_connection_options = ConnectionOptions.from(options)
128
154
  end
129
155
 
130
156
  unless const_defined? :Timer
@@ -132,137 +158,9 @@ module Faraday
132
158
  Timer = Timeout
133
159
  end
134
160
 
135
- # Public: Adds the ability for other modules to register and lookup
136
- # middleware classes.
137
- module MiddlewareRegistry
138
- # Public: Register middleware class(es) on the current module.
139
- #
140
- # mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
141
- # as fully qualified constant, or a Proc that will be lazily
142
- # called to return the former.
143
- #
144
- # Examples
145
- #
146
- # module Faraday
147
- # class Whatever
148
- # # Middleware looked up by :foo returns Faraday::Whatever::Foo.
149
- # register_middleware :foo => Foo
150
- #
151
- # # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
152
- # register_middleware :bar => :Bar
153
- #
154
- # # Middleware looked up by :baz requires 'baz' and returns Faraday::Whatever.const_get(:Baz)
155
- # register_middleware :baz => [:Baz, 'baz']
156
- # end
157
- # end
158
- #
159
- # Returns nothing.
160
- def register_middleware(autoload_path = nil, mapping = nil)
161
- if mapping.nil?
162
- mapping = autoload_path
163
- autoload_path = nil
164
- end
165
- middleware_mutex do
166
- @middleware_autoload_path = autoload_path if autoload_path
167
- (@registered_middleware ||= {}).update(mapping)
168
- end
169
- end
170
-
171
- # Public: Lookup middleware class with a registered Symbol shortcut.
172
- #
173
- # key - The Symbol key for the registered middleware.
174
- #
175
- # Examples
176
- #
177
- # module Faraday
178
- # class Whatever
179
- # register_middleware :foo => Foo
180
- # end
181
- # end
182
- #
183
- # Faraday::Whatever.lookup_middleware(:foo)
184
- # # => Faraday::Whatever::Foo
185
- #
186
- # Returns a middleware Class.
187
- def lookup_middleware(key)
188
- load_middleware(key) ||
189
- raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
190
- end
191
-
192
- def middleware_mutex(&block)
193
- @middleware_mutex ||= begin
194
- require 'monitor'
195
- Monitor.new
196
- end
197
- @middleware_mutex.synchronize(&block)
198
- end
199
-
200
- def fetch_middleware(key)
201
- defined?(@registered_middleware) && @registered_middleware[key]
202
- end
203
-
204
- def load_middleware(key)
205
- value = fetch_middleware(key)
206
- case value
207
- when Module
208
- value
209
- when Symbol, String
210
- middleware_mutex do
211
- @registered_middleware[key] = const_get(value)
212
- end
213
- when Proc
214
- middleware_mutex do
215
- @registered_middleware[key] = value.call
216
- end
217
- when Array
218
- middleware_mutex do
219
- const, path = value
220
- if root = @middleware_autoload_path
221
- path = "#{root}/#{path}"
222
- end
223
- require(path)
224
- @registered_middleware[key] = const
225
- end
226
- load_middleware(key)
227
- end
228
- end
229
- end
230
-
231
- def self.const_missing(name)
232
- if name.to_sym == :Builder
233
- warn "Faraday::Builder is now Faraday::RackBuilder."
234
- const_set name, RackBuilder
235
- else
236
- super
237
- end
238
- end
239
-
240
- require_libs "utils", "options", "connection", "rack_builder", "parameters",
241
- "middleware", "adapter", "request", "response", "upload_io", "error"
242
-
243
- if !ENV["FARADAY_NO_AUTOLOAD"]
244
- require_lib 'autoload'
245
- end
246
- end
161
+ require_libs 'utils', 'options', 'connection', 'rack_builder', 'parameters',
162
+ 'middleware', 'adapter', 'request', 'response', 'error',
163
+ 'file_part', 'param_part'
247
164
 
248
- # not pulling in active-support JUST for this method. And I love this method.
249
- class Object
250
- # The primary purpose of this method is to "tap into" a method chain,
251
- # in order to perform operations on intermediate results within the chain.
252
- #
253
- # Examples
254
- #
255
- # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
256
- # tap { |x| puts "array: #{x.inspect}" }.
257
- # select { |x| x%2 == 0 }.
258
- # tap { |x| puts "evens: #{x.inspect}" }.
259
- # map { |x| x*x }.
260
- # tap { |x| puts "squares: #{x.inspect}" }
261
- #
262
- # Yields self.
263
- # Returns self.
264
- def tap
265
- yield(self)
266
- self
267
- end unless Object.respond_to?(:tap)
165
+ require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
268
166
  end