faraday 0.15.4 → 1.0.1

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 (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +276 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +18 -344
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +65 -0
  7. data/examples/client_test.rb +79 -0
  8. data/lib/faraday/adapter/em_http.rb +144 -101
  9. data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
  10. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  11. data/lib/faraday/adapter/em_synchrony.rb +104 -60
  12. data/lib/faraday/adapter/excon.rb +98 -56
  13. data/lib/faraday/adapter/httpclient.rb +83 -59
  14. data/lib/faraday/adapter/net_http.rb +130 -63
  15. data/lib/faraday/adapter/net_http_persistent.rb +51 -28
  16. data/lib/faraday/adapter/patron.rb +80 -43
  17. data/lib/faraday/adapter/rack.rb +30 -13
  18. data/lib/faraday/adapter/test.rb +86 -53
  19. data/lib/faraday/adapter/typhoeus.rb +4 -1
  20. data/lib/faraday/adapter.rb +82 -22
  21. data/lib/faraday/adapter_registry.rb +30 -0
  22. data/lib/faraday/autoload.rb +47 -36
  23. data/lib/faraday/connection.rb +312 -182
  24. data/lib/faraday/dependency_loader.rb +37 -0
  25. data/lib/faraday/encoders/flat_params_encoder.rb +98 -0
  26. data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
  27. data/lib/faraday/error.rb +103 -37
  28. data/lib/faraday/file_part.rb +128 -0
  29. data/lib/faraday/logging/formatter.rb +105 -0
  30. data/lib/faraday/middleware.rb +12 -28
  31. data/lib/faraday/middleware_registry.rb +129 -0
  32. data/lib/faraday/options/connection_options.rb +22 -0
  33. data/lib/faraday/options/env.rb +181 -0
  34. data/lib/faraday/options/proxy_options.rb +28 -0
  35. data/lib/faraday/options/request_options.rb +22 -0
  36. data/lib/faraday/options/ssl_options.rb +59 -0
  37. data/lib/faraday/options.rb +35 -186
  38. data/lib/faraday/param_part.rb +53 -0
  39. data/lib/faraday/parameters.rb +4 -197
  40. data/lib/faraday/rack_builder.rb +67 -56
  41. data/lib/faraday/request/authorization.rb +44 -30
  42. data/lib/faraday/request/basic_authentication.rb +14 -7
  43. data/lib/faraday/request/instrumentation.rb +45 -27
  44. data/lib/faraday/request/multipart.rb +79 -48
  45. data/lib/faraday/request/retry.rb +198 -169
  46. data/lib/faraday/request/token_authentication.rb +15 -10
  47. data/lib/faraday/request/url_encoded.rb +43 -23
  48. data/lib/faraday/request.rb +68 -36
  49. data/lib/faraday/response/logger.rb +22 -69
  50. data/lib/faraday/response/raise_error.rb +38 -14
  51. data/lib/faraday/response.rb +27 -17
  52. data/lib/faraday/utils/headers.rb +139 -0
  53. data/lib/faraday/utils/params_hash.rb +61 -0
  54. data/lib/faraday/utils.rb +36 -245
  55. data/lib/faraday.rb +94 -176
  56. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  57. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  58. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  59. data/spec/faraday/adapter/excon_spec.rb +49 -0
  60. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  61. data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
  62. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  63. data/spec/faraday/adapter/patron_spec.rb +18 -0
  64. data/spec/faraday/adapter/rack_spec.rb +8 -0
  65. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  66. data/spec/faraday/adapter_registry_spec.rb +28 -0
  67. data/spec/faraday/adapter_spec.rb +55 -0
  68. data/spec/faraday/composite_read_io_spec.rb +80 -0
  69. data/spec/faraday/connection_spec.rb +691 -0
  70. data/spec/faraday/error_spec.rb +45 -0
  71. data/spec/faraday/middleware_spec.rb +26 -0
  72. data/spec/faraday/options/env_spec.rb +70 -0
  73. data/spec/faraday/options/options_spec.rb +297 -0
  74. data/spec/faraday/options/proxy_options_spec.rb +37 -0
  75. data/spec/faraday/options/request_options_spec.rb +19 -0
  76. data/spec/faraday/params_encoders/flat_spec.rb +34 -0
  77. data/spec/faraday/params_encoders/nested_spec.rb +134 -0
  78. data/spec/faraday/rack_builder_spec.rb +196 -0
  79. data/spec/faraday/request/authorization_spec.rb +88 -0
  80. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  81. data/spec/faraday/request/multipart_spec.rb +274 -0
  82. data/spec/faraday/request/retry_spec.rb +242 -0
  83. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  84. data/spec/faraday/request_spec.rb +109 -0
  85. data/spec/faraday/response/logger_spec.rb +220 -0
  86. data/spec/faraday/response/middleware_spec.rb +68 -0
  87. data/spec/faraday/response/raise_error_spec.rb +106 -0
  88. data/spec/faraday/response_spec.rb +75 -0
  89. data/spec/faraday/utils/headers_spec.rb +82 -0
  90. data/spec/faraday/utils_spec.rb +56 -0
  91. data/spec/faraday_spec.rb +37 -0
  92. data/spec/spec_helper.rb +132 -0
  93. data/spec/support/disabling_stub.rb +14 -0
  94. data/spec/support/fake_safe_buffer.rb +15 -0
  95. data/spec/support/helper_methods.rb +133 -0
  96. data/spec/support/shared_examples/adapter.rb +104 -0
  97. data/spec/support/shared_examples/params_encoder.rb +18 -0
  98. data/spec/support/shared_examples/request_method.rb +234 -0
  99. data/spec/support/streaming_response_checker.rb +35 -0
  100. data/spec/support/webmock_rack_app.rb +68 -0
  101. metadata +78 -9
  102. data/lib/faraday/upload_io.rb +0 -67
data/lib/faraday/utils.rb CHANGED
@@ -1,193 +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 self.allocate
14
- new_self = super
15
- new_self.initialize_names
16
- new_self
17
- end
18
-
19
- def initialize(hash = nil)
20
- super()
21
- @names = {}
22
- self.update(hash || {})
23
- end
24
-
25
- def initialize_names
26
- @names = {}
27
- end
28
-
29
- # on dup/clone, we need to duplicate @names hash
30
- def initialize_copy(other)
31
- super
32
- @names = other.names.dup
33
- end
34
-
35
- # need to synchronize concurrent writes to the shared KeyMap
36
- keymap_mutex = Mutex.new
37
-
38
- # symbol -> string mapper + cache
39
- KeyMap = Hash.new do |map, key|
40
- value = if key.respond_to?(:to_str)
41
- key
42
- else
43
- key.to_s.split('_'). # :user_agent => %w(user agent)
44
- each { |w| w.capitalize! }. # => %w(User Agent)
45
- join('-') # => "User-Agent"
46
- end
47
- keymap_mutex.synchronize { map[key] = value }
48
- end
49
- KeyMap[:etag] = "ETag"
50
-
51
- def [](k)
52
- k = KeyMap[k]
53
- super(k) || super(@names[k.downcase])
54
- end
55
-
56
- def []=(k, v)
57
- k = KeyMap[k]
58
- k = (@names[k.downcase] ||= k)
59
- # join multiple values with a comma
60
- v = v.to_ary.join(', ') if v.respond_to? :to_ary
61
- super(k, v)
62
- end
63
-
64
- def fetch(k, *args, &block)
65
- k = KeyMap[k]
66
- key = @names.fetch(k.downcase, k)
67
- super(key, *args, &block)
68
- end
69
-
70
- def delete(k)
71
- k = KeyMap[k]
72
- if k = @names[k.downcase]
73
- @names.delete k.downcase
74
- super(k)
75
- end
76
- end
77
-
78
- def include?(k)
79
- @names.include? k.downcase
80
- end
81
-
82
- alias_method :has_key?, :include?
83
- alias_method :member?, :include?
84
- alias_method :key?, :include?
85
-
86
- def merge!(other)
87
- other.each { |k, v| self[k] = v }
88
- self
89
- end
90
- alias_method :update, :merge!
91
-
92
- def merge(other)
93
- hash = dup
94
- hash.merge! other
95
- end
96
-
97
- def replace(other)
98
- clear
99
- @names.clear
100
- self.update other
101
- self
102
- end
103
-
104
- def to_hash() ::Hash.new.update(self) end
105
-
106
- def parse(header_string)
107
- return unless header_string && !header_string.empty?
108
-
109
- headers = header_string.split(/\r\n/)
110
-
111
- # Find the last set of response headers.
112
- start_index = headers.rindex { |x| x.match(/^HTTP\//) } || 0
113
- last_response = headers.slice(start_index, headers.size)
114
-
115
- last_response.
116
- tap { |a| a.shift if a.first.index('HTTP/') == 0 }. # drop the HTTP status line
117
- map { |h| h.split(/:\s*/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines
118
- each { |key, value|
119
- # join multiple values with a comma
120
- if self[key]
121
- self[key] << ', ' << value
122
- else
123
- self[key] = value
124
- end
125
- }
126
- end
127
-
128
- protected
129
-
130
- def names
131
- @names
132
- end
133
- end
134
-
135
- # hash with stringified keys
136
- class ParamsHash < Hash
137
- def [](key)
138
- super(convert_key(key))
139
- end
140
-
141
- def []=(key, value)
142
- super(convert_key(key), value)
143
- end
144
-
145
- def delete(key)
146
- super(convert_key(key))
147
- end
148
-
149
- def include?(key)
150
- super(convert_key(key))
151
- end
152
-
153
- alias_method :has_key?, :include?
154
- alias_method :member?, :include?
155
- alias_method :key?, :include?
156
-
157
- def update(params)
158
- params.each do |key, value|
159
- self[key] = value
160
- end
161
- self
162
- end
163
- alias_method :merge!, :update
164
-
165
- def merge(params)
166
- dup.update(params)
167
- end
168
-
169
- def replace(other)
170
- clear
171
- update(other)
172
- end
173
-
174
- def merge_query(query, encoder = nil)
175
- if query && !query.empty?
176
- update((encoder || Utils.default_params_encoder).decode(query))
177
- end
178
- self
179
- end
180
-
181
- def to_query(encoder = nil)
182
- (encoder || Utils.default_params_encoder).encode(self)
183
- end
184
-
185
- private
186
-
187
- def convert_key(key)
188
- key.to_s
189
- end
190
- end
9
+ module_function
191
10
 
192
11
  def build_query(params)
193
12
  FlatParamsEncoder.encode(params)
@@ -197,17 +16,27 @@ module Faraday
197
16
  NestedParamsEncoder.encode(params)
198
17
  end
199
18
 
200
- ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/
19
+ def default_space_encoding
20
+ @default_space_encoding ||= '+'
21
+ end
22
+
23
+ class << self
24
+ attr_writer :default_space_encoding
25
+ end
201
26
 
202
- def escape(s)
203
- s.to_s.gsub(ESCAPE_RE) {|match|
27
+ ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/.freeze
28
+
29
+ def escape(str)
30
+ str.to_s.gsub(ESCAPE_RE) do |match|
204
31
  '%' + match.unpack('H2' * match.bytesize).join('%').upcase
205
- }.tr(' ', '+')
32
+ end.gsub(' ', default_space_encoding)
206
33
  end
207
34
 
208
- def unescape(s) CGI.unescape s.to_s end
35
+ def unescape(str)
36
+ CGI.unescape str.to_s
37
+ end
209
38
 
210
- DEFAULT_SEP = /[&;] */n
39
+ DEFAULT_SEP = /[&;] */n.freeze
211
40
 
212
41
  # Adapted from Rack
213
42
  def parse_query(query)
@@ -226,55 +55,18 @@ module Faraday
226
55
  attr_writer :default_params_encoder
227
56
  end
228
57
 
229
- # Stolen from Rack
230
- def normalize_params(params, name, v = nil)
231
- name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
232
- k = $1 || ''
233
- after = $' || ''
234
-
235
- return if k.empty?
236
-
237
- if after == ""
238
- if params[k]
239
- params[k] = Array[params[k]] unless params[k].kind_of?(Array)
240
- params[k] << v
241
- else
242
- params[k] = v
243
- end
244
- elsif after == "[]"
245
- params[k] ||= []
246
- raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
247
- params[k] << v
248
- elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
249
- child_key = $1
250
- params[k] ||= []
251
- raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
252
- if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
253
- normalize_params(params[k].last, child_key, v)
254
- else
255
- params[k] << normalize_params({}, child_key, v)
256
- end
257
- else
258
- params[k] ||= {}
259
- raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash)
260
- params[k] = normalize_params(params[k], after, v)
261
- end
262
-
263
- return params
264
- end
265
-
266
58
  # Normalize URI() behavior across Ruby versions
267
59
  #
268
60
  # url - A String or URI.
269
61
  #
270
62
  # Returns a parsed URI.
271
- def URI(url)
63
+ def URI(url) # rubocop:disable Naming/MethodName
272
64
  if url.respond_to?(:host)
273
65
  url
274
66
  elsif url.respond_to?(:to_str)
275
67
  default_uri_parser.call(url)
276
68
  else
277
- raise ArgumentError, "bad argument (expected URI object or URI string)"
69
+ raise ArgumentError, 'bad argument (expected URI object or URI string)'
278
70
  end
279
71
  end
280
72
 
@@ -287,27 +79,28 @@ module Faraday
287
79
 
288
80
  def default_uri_parser=(parser)
289
81
  @default_uri_parser = if parser.respond_to?(:call) || parser.nil?
290
- parser
291
- else
292
- parser.method(:parse)
293
- end
82
+ parser
83
+ else
84
+ parser.method(:parse)
85
+ end
294
86
  end
295
87
 
296
- # Receives a String or URI and returns just the path with the query string sorted.
88
+ # Receives a String or URI and returns just
89
+ # the path with the query string sorted.
297
90
  def normalize_path(url)
298
91
  url = URI(url)
299
92
  (url.path.start_with?('/') ? url.path : '/' + url.path) +
300
- (url.query ? "?#{sort_query_params(url.query)}" : "")
93
+ (url.query ? "?#{sort_query_params(url.query)}" : '')
301
94
  end
302
95
 
303
96
  # Recursive hash update
304
97
  def deep_merge!(target, hash)
305
98
  hash.each do |key, value|
306
- if Hash === value and Hash === target[key]
307
- target[key] = deep_merge(target[key], value)
308
- else
309
- target[key] = value
310
- end
99
+ target[key] = if value.is_a?(Hash) && target[key].is_a?(Hash)
100
+ deep_merge(target[key], value)
101
+ else
102
+ value
103
+ end
311
104
  end
312
105
  target
313
106
  end
@@ -317,8 +110,6 @@ module Faraday
317
110
  deep_merge!(source.dup, hash)
318
111
  end
319
112
 
320
- protected
321
-
322
113
  def sort_query_params(query)
323
114
  query.split('&').sort.join('&')
324
115
  end
data/lib/faraday.rb CHANGED
@@ -1,248 +1,166 @@
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.15.4"
22
+ VERSION = '1.0.1'
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: Tells faraday to ignore the environment proxy (http_proxy).
50
+ # Tells Faraday to ignore the environment proxy (http_proxy).
51
+ # Defaults to `false`.
52
+ # @return [Boolean]
38
53
  attr_accessor :ignore_env_proxy
39
54
 
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
- #
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
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
94
-
95
- def respond_to?(symbol, include_private = false)
106
+ def respond_to_missing?(symbol, include_private = false)
96
107
  default_connection.respond_to?(symbol, include_private) || super
97
108
  end
98
109
 
99
- private
110
+ private
111
+
100
112
  # Internal: Proxies method calls on the Faraday constant to
101
- # #default_connection.
113
+ # .default_connection.
102
114
  def method_missing(name, *args, &block)
103
- 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
104
120
  end
105
121
  end
106
122
 
107
123
  self.ignore_env_proxy = false
108
- self.root_path = File.expand_path "..", __FILE__
109
- self.lib_path = File.expand_path "../faraday", __FILE__
124
+ self.root_path = File.expand_path __dir__
125
+ self.lib_path = File.expand_path 'faraday', __dir__
110
126
  self.default_adapter = :net_http
111
127
 
112
- # Gets the default connection used for simple scripts.
113
- #
114
- # 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>.
115
137
  def self.default_connection
116
138
  @default_connection ||= Connection.new(default_connection_options)
117
139
  end
118
140
 
119
- # Gets the default connection options used when calling Faraday#new.
141
+ # Gets the default connection options used when calling {Faraday#new}.
120
142
  #
121
- # Returns a Faraday::ConnectionOptions.
143
+ # @return [Faraday::ConnectionOptions]
122
144
  def self.default_connection_options
123
145
  @default_connection_options ||= ConnectionOptions.new
124
146
  end
125
147
 
126
- # Public: Sets the default options used when calling Faraday#new.
148
+ # Sets the default options used when calling {Faraday#new}.
149
+ #
150
+ # @param options [Hash, Faraday::ConnectionOptions]
127
151
  def self.default_connection_options=(options)
128
152
  @default_connection = nil
129
153
  @default_connection_options = ConnectionOptions.from(options)
130
154
  end
131
155
 
132
- unless const_defined? :Timer
156
+ unless defined?(::Faraday::Timer)
133
157
  require 'timeout'
134
158
  Timer = Timeout
135
159
  end
136
160
 
137
- # Public: Adds the ability for other modules to register and lookup
138
- # middleware classes.
139
- module MiddlewareRegistry
140
- # Public: Register middleware class(es) on the current module.
141
- #
142
- # mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
143
- # as fully qualified constant, or a Proc that will be lazily
144
- # called to return the former.
145
- #
146
- # Examples
147
- #
148
- # module Faraday
149
- # class Whatever
150
- # # Middleware looked up by :foo returns Faraday::Whatever::Foo.
151
- # register_middleware :foo => Foo
152
- #
153
- # # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
154
- # register_middleware :bar => :Bar
155
- #
156
- # # Middleware looked up by :baz requires 'baz' and returns Faraday::Whatever.const_get(:Baz)
157
- # register_middleware :baz => [:Baz, 'baz']
158
- # end
159
- # end
160
- #
161
- # Returns nothing.
162
- def register_middleware(autoload_path = nil, mapping = nil)
163
- if mapping.nil?
164
- mapping = autoload_path
165
- autoload_path = nil
166
- end
167
- middleware_mutex do
168
- @middleware_autoload_path = autoload_path if autoload_path
169
- (@registered_middleware ||= {}).update(mapping)
170
- end
171
- end
161
+ require_libs 'utils', 'options', 'connection', 'rack_builder', 'parameters',
162
+ 'middleware', 'adapter', 'request', 'response', 'error',
163
+ 'file_part', 'param_part'
172
164
 
173
- # Public: Lookup middleware class with a registered Symbol shortcut.
174
- #
175
- # key - The Symbol key for the registered middleware.
176
- #
177
- # Examples
178
- #
179
- # module Faraday
180
- # class Whatever
181
- # register_middleware :foo => Foo
182
- # end
183
- # end
184
- #
185
- # Faraday::Whatever.lookup_middleware(:foo)
186
- # # => Faraday::Whatever::Foo
187
- #
188
- # Returns a middleware Class.
189
- def lookup_middleware(key)
190
- load_middleware(key) ||
191
- raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
192
- end
193
-
194
- def middleware_mutex(&block)
195
- @middleware_mutex ||= begin
196
- require 'monitor'
197
- Monitor.new
198
- end
199
- @middleware_mutex.synchronize(&block)
200
- end
201
-
202
- def fetch_middleware(key)
203
- defined?(@registered_middleware) && @registered_middleware[key]
204
- end
205
-
206
- def load_middleware(key)
207
- value = fetch_middleware(key)
208
- case value
209
- when Module
210
- value
211
- when Symbol, String
212
- middleware_mutex do
213
- @registered_middleware[key] = const_get(value)
214
- end
215
- when Proc
216
- middleware_mutex do
217
- @registered_middleware[key] = value.call
218
- end
219
- when Array
220
- middleware_mutex do
221
- const, path = value
222
- if root = @middleware_autoload_path
223
- path = "#{root}/#{path}"
224
- end
225
- require(path)
226
- @registered_middleware[key] = const
227
- end
228
- load_middleware(key)
229
- end
230
- end
231
- end
232
-
233
- def self.const_missing(name)
234
- if name.to_sym == :Builder
235
- warn "Faraday::Builder is now Faraday::RackBuilder."
236
- const_set name, RackBuilder
237
- else
238
- super
239
- end
240
- end
241
-
242
- require_libs "utils", "options", "connection", "rack_builder", "parameters",
243
- "middleware", "adapter", "request", "response", "upload_io", "error"
244
-
245
- if !ENV["FARADAY_NO_AUTOLOAD"]
246
- require_lib 'autoload'
247
- end
165
+ require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
248
166
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'webmock/rspec'
4
+ WebMock.disable_net_connect!(allow_localhost: true)
5
+
6
+ require_relative '../support/helper_methods'
7
+ require_relative '../support/disabling_stub'
8
+ require_relative '../support/streaming_response_checker'
9
+ require_relative '../support/shared_examples/adapter'
10
+ require_relative '../support/shared_examples/request_method'
11
+
12
+ RSpec.configure do |config|
13
+ config.include Faraday::HelperMethods
14
+ end