faraday 0.9.1 → 0.16.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 (98) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE.md +1 -1
  3. data/README.md +30 -195
  4. data/lib/faraday/adapter/em_http.rb +148 -99
  5. data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
  6. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  7. data/lib/faraday/adapter/em_synchrony.rb +107 -49
  8. data/lib/faraday/adapter/excon.rb +102 -55
  9. data/lib/faraday/adapter/httpclient.rb +80 -36
  10. data/lib/faraday/adapter/net_http.rb +119 -44
  11. data/lib/faraday/adapter/net_http_persistent.rb +68 -27
  12. data/lib/faraday/adapter/patron.rb +76 -34
  13. data/lib/faraday/adapter/rack.rb +28 -12
  14. data/lib/faraday/adapter/test.rb +136 -52
  15. data/lib/faraday/adapter/typhoeus.rb +7 -115
  16. data/lib/faraday/adapter.rb +43 -20
  17. data/lib/faraday/adapter_registry.rb +28 -0
  18. data/lib/faraday/autoload.rb +47 -36
  19. data/lib/faraday/connection.rb +359 -165
  20. data/lib/faraday/dependency_loader.rb +37 -0
  21. data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
  22. data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
  23. data/lib/faraday/error.rb +71 -24
  24. data/lib/faraday/file_part.rb +128 -0
  25. data/lib/faraday/logging/formatter.rb +92 -0
  26. data/lib/faraday/middleware.rb +4 -28
  27. data/lib/faraday/middleware_registry.rb +129 -0
  28. data/lib/faraday/options/connection_options.rb +22 -0
  29. data/lib/faraday/options/env.rb +181 -0
  30. data/lib/faraday/options/proxy_options.rb +28 -0
  31. data/lib/faraday/options/request_options.rb +21 -0
  32. data/lib/faraday/options/ssl_options.rb +59 -0
  33. data/lib/faraday/options.rb +57 -185
  34. data/lib/faraday/param_part.rb +53 -0
  35. data/lib/faraday/parameters.rb +4 -180
  36. data/lib/faraday/rack_builder.rb +74 -38
  37. data/lib/faraday/request/authorization.rb +42 -31
  38. data/lib/faraday/request/basic_authentication.rb +14 -7
  39. data/lib/faraday/request/instrumentation.rb +45 -27
  40. data/lib/faraday/request/multipart.rb +81 -45
  41. data/lib/faraday/request/retry.rb +212 -121
  42. data/lib/faraday/request/token_authentication.rb +15 -10
  43. data/lib/faraday/request/url_encoded.rb +41 -23
  44. data/lib/faraday/request.rb +84 -30
  45. data/lib/faraday/response/logger.rb +22 -48
  46. data/lib/faraday/response/raise_error.rb +36 -14
  47. data/lib/faraday/response.rb +29 -18
  48. data/lib/faraday/utils/headers.rb +139 -0
  49. data/lib/faraday/utils/params_hash.rb +61 -0
  50. data/lib/faraday/utils.rb +28 -216
  51. data/lib/faraday.rb +102 -204
  52. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  53. metadata +24 -94
  54. data/.document +0 -6
  55. data/CHANGELOG.md +0 -20
  56. data/CONTRIBUTING.md +0 -36
  57. data/Gemfile +0 -25
  58. data/Rakefile +0 -71
  59. data/faraday.gemspec +0 -34
  60. data/lib/faraday/upload_io.rb +0 -67
  61. data/script/cached-bundle +0 -46
  62. data/script/console +0 -7
  63. data/script/generate_certs +0 -42
  64. data/script/package +0 -7
  65. data/script/proxy-server +0 -42
  66. data/script/release +0 -17
  67. data/script/s3-put +0 -71
  68. data/script/server +0 -36
  69. data/script/test +0 -172
  70. data/test/adapters/default_test.rb +0 -14
  71. data/test/adapters/em_http_test.rb +0 -20
  72. data/test/adapters/em_synchrony_test.rb +0 -20
  73. data/test/adapters/excon_test.rb +0 -20
  74. data/test/adapters/httpclient_test.rb +0 -21
  75. data/test/adapters/integration.rb +0 -254
  76. data/test/adapters/logger_test.rb +0 -82
  77. data/test/adapters/net_http_persistent_test.rb +0 -20
  78. data/test/adapters/net_http_test.rb +0 -14
  79. data/test/adapters/patron_test.rb +0 -20
  80. data/test/adapters/rack_test.rb +0 -31
  81. data/test/adapters/test_middleware_test.rb +0 -114
  82. data/test/adapters/typhoeus_test.rb +0 -28
  83. data/test/authentication_middleware_test.rb +0 -65
  84. data/test/composite_read_io_test.rb +0 -111
  85. data/test/connection_test.rb +0 -522
  86. data/test/env_test.rb +0 -218
  87. data/test/helper.rb +0 -81
  88. data/test/live_server.rb +0 -67
  89. data/test/middleware/instrumentation_test.rb +0 -88
  90. data/test/middleware/retry_test.rb +0 -177
  91. data/test/middleware_stack_test.rb +0 -173
  92. data/test/multibyte.txt +0 -1
  93. data/test/options_test.rb +0 -252
  94. data/test/parameters_test.rb +0 -64
  95. data/test/request_middleware_test.rb +0 -142
  96. data/test/response_middleware_test.rb +0 -72
  97. data/test/strawberry.rb +0 -2
  98. data/test/utils_test.rb +0 -58
data/lib/faraday/utils.rb CHANGED
@@ -1,164 +1,12 @@
1
- require 'thread'
2
- Faraday.require_libs 'parameters'
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday/utils/headers'
4
+ require 'faraday/utils/params_hash'
3
5
 
4
6
  module Faraday
7
+ # Utils contains various static helper methods.
5
8
  module Utils
6
- extend self
7
-
8
- # Adapted from Rack::Utils::HeaderHash
9
- class Headers < ::Hash
10
- def self.from(value)
11
- new(value)
12
- end
13
-
14
- def initialize(hash = nil)
15
- super()
16
- @names = {}
17
- self.update(hash || {})
18
- end
19
-
20
- # need to synchronize concurrent writes to the shared KeyMap
21
- keymap_mutex = Mutex.new
22
-
23
- # symbol -> string mapper + cache
24
- KeyMap = Hash.new do |map, key|
25
- value = if key.respond_to?(:to_str)
26
- key
27
- else
28
- key.to_s.split('_'). # :user_agent => %w(user agent)
29
- each { |w| w.capitalize! }. # => %w(User Agent)
30
- join('-') # => "User-Agent"
31
- end
32
- keymap_mutex.synchronize { map[key] = value }
33
- end
34
- KeyMap[:etag] = "ETag"
35
-
36
- def [](k)
37
- k = KeyMap[k]
38
- super(k) || super(@names[k.downcase])
39
- end
40
-
41
- def []=(k, v)
42
- k = KeyMap[k]
43
- k = (@names[k.downcase] ||= k)
44
- # join multiple values with a comma
45
- v = v.to_ary.join(', ') if v.respond_to? :to_ary
46
- super(k, v)
47
- end
48
-
49
- def fetch(k, *args, &block)
50
- k = KeyMap[k]
51
- key = @names.fetch(k.downcase, k)
52
- super(key, *args, &block)
53
- end
54
-
55
- def delete(k)
56
- k = KeyMap[k]
57
- if k = @names[k.downcase]
58
- @names.delete k.downcase
59
- super(k)
60
- end
61
- end
62
-
63
- def include?(k)
64
- @names.include? k.downcase
65
- end
66
-
67
- alias_method :has_key?, :include?
68
- alias_method :member?, :include?
69
- alias_method :key?, :include?
70
-
71
- def merge!(other)
72
- other.each { |k, v| self[k] = v }
73
- self
74
- end
75
- alias_method :update, :merge!
76
-
77
- def merge(other)
78
- hash = dup
79
- hash.merge! other
80
- end
81
-
82
- def replace(other)
83
- clear
84
- self.update other
85
- self
86
- end
87
-
88
- def to_hash() ::Hash.new.update(self) end
89
-
90
- def parse(header_string)
91
- return unless header_string && !header_string.empty?
92
- header_string.split(/\r\n/).
93
- tap { |a| a.shift if a.first.index('HTTP/') == 0 }. # drop the HTTP status line
94
- map { |h| h.split(/:\s+/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines
95
- each { |key, value|
96
- # join multiple values with a comma
97
- if self[key]
98
- self[key] << ', ' << value
99
- else
100
- self[key] = value
101
- end
102
- }
103
- end
104
- end
105
-
106
- # hash with stringified keys
107
- class ParamsHash < Hash
108
- def [](key)
109
- super(convert_key(key))
110
- end
111
-
112
- def []=(key, value)
113
- super(convert_key(key), value)
114
- end
115
-
116
- def delete(key)
117
- super(convert_key(key))
118
- end
119
-
120
- def include?(key)
121
- super(convert_key(key))
122
- end
123
-
124
- alias_method :has_key?, :include?
125
- alias_method :member?, :include?
126
- alias_method :key?, :include?
127
-
128
- def update(params)
129
- params.each do |key, value|
130
- self[key] = value
131
- end
132
- self
133
- end
134
- alias_method :merge!, :update
135
-
136
- def merge(params)
137
- dup.update(params)
138
- end
139
-
140
- def replace(other)
141
- clear
142
- update(other)
143
- end
144
-
145
- def merge_query(query, encoder = nil)
146
- if query && !query.empty?
147
- update((encoder || Utils.default_params_encoder).decode(query))
148
- end
149
- self
150
- end
151
-
152
- def to_query(encoder = nil)
153
- (encoder || Utils.default_params_encoder).encode(self)
154
- end
155
-
156
- private
157
-
158
- def convert_key(key)
159
- key.to_s
160
- end
161
- end
9
+ module_function
162
10
 
163
11
  def build_query(params)
164
12
  FlatParamsEncoder.encode(params)
@@ -168,17 +16,19 @@ module Faraday
168
16
  NestedParamsEncoder.encode(params)
169
17
  end
170
18
 
171
- ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/
19
+ ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/.freeze
172
20
 
173
- def escape(s)
174
- s.to_s.gsub(ESCAPE_RE) {|match|
21
+ def escape(str)
22
+ str.to_s.gsub(ESCAPE_RE) do |match|
175
23
  '%' + match.unpack('H2' * match.bytesize).join('%').upcase
176
- }.tr(' ', '+')
24
+ end.tr(' ', '+')
177
25
  end
178
26
 
179
- def unescape(s) CGI.unescape s.to_s end
27
+ def unescape(str)
28
+ CGI.unescape str.to_s
29
+ end
180
30
 
181
- DEFAULT_SEP = /[&;] */n
31
+ DEFAULT_SEP = /[&;] */n.freeze
182
32
 
183
33
  # Adapted from Rack
184
34
  def parse_query(query)
@@ -197,55 +47,18 @@ module Faraday
197
47
  attr_writer :default_params_encoder
198
48
  end
199
49
 
200
- # Stolen from Rack
201
- def normalize_params(params, name, v = nil)
202
- name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
203
- k = $1 || ''
204
- after = $' || ''
205
-
206
- return if k.empty?
207
-
208
- if after == ""
209
- if params[k]
210
- params[k] = Array[params[k]] unless params[k].kind_of?(Array)
211
- params[k] << v
212
- else
213
- params[k] = v
214
- end
215
- elsif after == "[]"
216
- params[k] ||= []
217
- raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
218
- params[k] << v
219
- elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
220
- child_key = $1
221
- params[k] ||= []
222
- raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
223
- if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
224
- normalize_params(params[k].last, child_key, v)
225
- else
226
- params[k] << normalize_params({}, child_key, v)
227
- end
228
- else
229
- params[k] ||= {}
230
- raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash)
231
- params[k] = normalize_params(params[k], after, v)
232
- end
233
-
234
- return params
235
- end
236
-
237
50
  # Normalize URI() behavior across Ruby versions
238
51
  #
239
52
  # url - A String or URI.
240
53
  #
241
54
  # Returns a parsed URI.
242
- def URI(url)
55
+ def URI(url) # rubocop:disable Naming/MethodName
243
56
  if url.respond_to?(:host)
244
57
  url
245
58
  elsif url.respond_to?(:to_str)
246
59
  default_uri_parser.call(url)
247
60
  else
248
- raise ArgumentError, "bad argument (expected URI object or URI string)"
61
+ raise ArgumentError, 'bad argument (expected URI object or URI string)'
249
62
  end
250
63
  end
251
64
 
@@ -258,27 +71,28 @@ module Faraday
258
71
 
259
72
  def default_uri_parser=(parser)
260
73
  @default_uri_parser = if parser.respond_to?(:call) || parser.nil?
261
- parser
262
- else
263
- parser.method(:parse)
264
- end
74
+ parser
75
+ else
76
+ parser.method(:parse)
77
+ end
265
78
  end
266
79
 
267
- # 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.
268
82
  def normalize_path(url)
269
83
  url = URI(url)
270
84
  (url.path.start_with?('/') ? url.path : '/' + url.path) +
271
- (url.query ? "?#{sort_query_params(url.query)}" : "")
85
+ (url.query ? "?#{sort_query_params(url.query)}" : '')
272
86
  end
273
87
 
274
88
  # Recursive hash update
275
89
  def deep_merge!(target, hash)
276
90
  hash.each do |key, value|
277
- if Hash === value and Hash === target[key]
278
- target[key] = deep_merge(target[key], value)
279
- else
280
- target[key] = value
281
- 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
282
96
  end
283
97
  target
284
98
  end
@@ -288,8 +102,6 @@ module Faraday
288
102
  deep_merge!(source.dup, hash)
289
103
  end
290
104
 
291
- protected
292
-
293
105
  def sort_query_params(query)
294
106
  query.split('&').sort.join('&')
295
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.1"
22
+ VERSION = '0.16.0'
23
+ METHODS_WITH_QUERY = %w[get head delete connect 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
@@ -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