faraday 1.8.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +117 -1
  3. data/README.md +16 -9
  4. data/examples/client_spec.rb +1 -1
  5. data/examples/client_test.rb +2 -2
  6. data/lib/faraday/adapter/test.rb +10 -4
  7. data/lib/faraday/adapter.rb +2 -5
  8. data/lib/faraday/connection.rb +5 -86
  9. data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
  10. data/lib/faraday/error.rb +3 -8
  11. data/lib/faraday/logging/formatter.rb +1 -0
  12. data/lib/faraday/middleware.rb +0 -1
  13. data/lib/faraday/middleware_registry.rb +15 -79
  14. data/lib/faraday/options.rb +3 -3
  15. data/lib/faraday/rack_builder.rb +22 -19
  16. data/lib/faraday/request/authorization.rb +28 -41
  17. data/lib/faraday/request/instrumentation.rb +2 -0
  18. data/lib/faraday/request/json.rb +55 -0
  19. data/lib/faraday/request/url_encoded.rb +2 -0
  20. data/lib/faraday/request.rb +11 -31
  21. data/lib/faraday/response/json.rb +54 -0
  22. data/lib/faraday/response/logger.rb +4 -4
  23. data/lib/faraday/response/raise_error.rb +9 -1
  24. data/lib/faraday/response.rb +8 -19
  25. data/lib/faraday/utils/headers.rb +1 -1
  26. data/lib/faraday/utils.rb +10 -5
  27. data/lib/faraday/version.rb +1 -1
  28. data/lib/faraday.rb +11 -39
  29. data/spec/faraday/connection_spec.rb +102 -51
  30. data/spec/faraday/options/env_spec.rb +2 -2
  31. data/spec/faraday/rack_builder_spec.rb +26 -42
  32. data/spec/faraday/request/authorization_spec.rb +19 -32
  33. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  34. data/spec/faraday/request/json_spec.rb +111 -0
  35. data/spec/faraday/request/url_encoded_spec.rb +0 -1
  36. data/spec/faraday/request_spec.rb +0 -11
  37. data/spec/faraday/response/json_spec.rb +117 -0
  38. data/spec/faraday/response/raise_error_spec.rb +7 -4
  39. data/spec/faraday/utils_spec.rb +62 -1
  40. data/spec/support/fake_safe_buffer.rb +1 -1
  41. data/spec/support/helper_methods.rb +0 -37
  42. data/spec/support/shared_examples/adapter.rb +0 -1
  43. data/spec/support/shared_examples/request_method.rb +5 -18
  44. metadata +11 -147
  45. data/lib/faraday/adapter/typhoeus.rb +0 -15
  46. data/lib/faraday/autoload.rb +0 -87
  47. data/lib/faraday/dependency_loader.rb +0 -37
  48. data/lib/faraday/file_part.rb +0 -128
  49. data/lib/faraday/param_part.rb +0 -53
  50. data/lib/faraday/request/basic_authentication.rb +0 -20
  51. data/lib/faraday/request/multipart.rb +0 -106
  52. data/lib/faraday/request/retry.rb +0 -239
  53. data/lib/faraday/request/token_authentication.rb +0 -20
  54. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  55. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  56. data/spec/faraday/adapter/excon_spec.rb +0 -49
  57. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  58. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  59. data/spec/faraday/adapter/patron_spec.rb +0 -18
  60. data/spec/faraday/adapter/rack_spec.rb +0 -8
  61. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  62. data/spec/faraday/composite_read_io_spec.rb +0 -80
  63. data/spec/faraday/request/multipart_spec.rb +0 -302
  64. data/spec/faraday/request/retry_spec.rb +0 -242
  65. data/spec/faraday/response/middleware_spec.rb +0 -68
  66. data/spec/support/webmock_rack_app.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6441da03048f7371bd0cb898dd3e27c7be6382c67fc4901883e9dbe9841b56f
4
- data.tar.gz: 0beb3f2da75282655fc46cd5540de96d5fef259dd35193ec9eb0d3163cc959d2
3
+ metadata.gz: 2123a105dd09a93bec6fe115277fd7c58afa269bc1036524a422a3e45df2b40c
4
+ data.tar.gz: bdd311dabe80e2ecd28f7bf941ea0e551840b2bc783dd781b56999842086a31f
5
5
  SHA512:
6
- metadata.gz: 1ab1aa0e0c68c9348d9e180a903f01f2517bd38672fe1bdf056ceab4729e841944173e47dc26aa1b4fe16b2779f1a8102914ed594ea92d08feff6dc944412f0b
7
- data.tar.gz: bc43fbac427062861e79d5328b60886a5b9c8783f707faa1fcfad064b9c6945f6a4e2a97dbad9ecadc1c1620b5327d651a764962764b7fc86e6d2fbd84ad0b4b
6
+ metadata.gz: 943a1dd4a67287874fc3724d3d9ec7e41540cd3a08b528201b1af806d5dd56bceb976f54df6d0394fb25bffeb0bcb224831700c9ef46c1daf24cb3e13fd2ad52
7
+ data.tar.gz: 3ae115b30da8ec49253888f4a81522c14667a8da6b248b0d56192252b8d9bf9a6914f703530d467c2a83415a0862b73ccc17f799d9c1d0fba8289296b7cb0faf
data/CHANGELOG.md CHANGED
@@ -1,5 +1,114 @@
1
1
  # Faraday Changelog
2
2
 
3
+ ## [1.8.0](https://github.com/lostisland/faraday/releases/tag/v1.8.0) (2021-09-18)
4
+
5
+ ### Features
6
+
7
+ * Backport authorization procs (#1322, @jarl-dk)
8
+
9
+ ## [v1.7.0](https://github.com/lostisland/faraday/releases/tag/v1.7.0) (2021-08-09)
10
+
11
+ ### Features
12
+
13
+ * Add strict_mode to Test::Stubs (#1298, @yykamei)
14
+
15
+ ## [v1.6.0](https://github.com/lostisland/faraday/releases/tag/v1.6.0) (2021-08-01)
16
+
17
+ ### Misc
18
+
19
+ * Use external Rack adapter (#1296, @iMacTia)
20
+
21
+ ## [v1.5.1](https://github.com/lostisland/faraday/releases/tag/v1.5.1) (2021-07-11)
22
+
23
+ ### Fixes
24
+
25
+ * Fix JRuby incompatibility after moving out EM adapters (#1294, @ahorek)
26
+
27
+ ### Documentation
28
+
29
+ * Update YARD to follow RackBuilder (#1292, @kachick)
30
+
31
+ ## [v1.5.0](https://github.com/lostisland/faraday/releases/tag/v1.5.0) (2021-07-04)
32
+
33
+ ### Misc
34
+
35
+ * Use external httpclient adapter (#1289, @iMacTia)
36
+ * Use external patron adapter (#1290, @iMacTia)
37
+
38
+ ## [v1.4.3](https://github.com/lostisland/faraday/releases/tag/v1.4.3) (2021-06-24)
39
+
40
+ ### Fixes
41
+
42
+ * Silence warning (#1286, @gurgeous)
43
+ * Always dup url_prefix in Connection#build_exclusive_url (#1288, @alexeyds)
44
+
45
+ ## [v1.4.2](https://github.com/lostisland/faraday/releases/tag/v1.4.2) (2021-05-22)
46
+
47
+ ### Fixes
48
+ * Add proxy setting when url_prefix is changed (#1276, @ci)
49
+ * Default proxy scheme to http:// if necessary, fixes #1282 (#1283, @gurgeous)
50
+
51
+ ### Documentation
52
+ * Improve introduction page (#1273, @gurgeous)
53
+ * Docs: add more middleware examples (#1277, @gurgeous)
54
+
55
+ ### Misc
56
+ * Use external `em_http` and `em_synchrony` adapters (#1274, @iMacTia)
57
+
58
+ ## [v1.4.1](https://github.com/lostisland/faraday/releases/tag/v1.4.1) (2021-04-18)
59
+
60
+ ### Fixes
61
+
62
+ * Fix dependencies from external adapter gems (#1269, @iMacTia)
63
+
64
+ ## [v1.4.0](https://github.com/lostisland/faraday/releases/tag/v1.4.0) (2021-04-16)
65
+
66
+ ### Highlights
67
+
68
+ With this release, we continue the work of gradually moving out adapters into their own gems 🎉
69
+ Thanks to @MikeRogers0 for helping the Faraday team in progressing with this quest 👏
70
+
71
+ And thanks to @olleolleolle efforts, Faraday is becoming more inclusive than ever 🤗
72
+ Faraday's `master` branch has been renamed into `main`, we have an official policy on inclusive language and even a rubocop plugin to check for non-inclusive words ❤️!
73
+ Checkout the "Misc" section below for more details 🙌 !
74
+
75
+ ### Fixes
76
+
77
+ * Fix NoMethodError undefined method 'coverage' (#1255, @Maroo-b)
78
+
79
+ ### Documentation
80
+
81
+ * Some docs on EventMachine adapters. (#1232, @damau)
82
+ * CONTRIBUTING: Fix grammar and layout (#1261, @olleolleolle)
83
+
84
+ ### Misc
85
+
86
+ * Replacing Net::HTTP::Persistent with faraday-net_http_persistent (#1250, @MikeRogers0)
87
+ * CI: Configure the regenerated Coveralls token (#1256, @olleolleolle)
88
+ * Replace Excon adapter with Faraday::Excon gem, and fix autoloading issue with Faraday::NetHttpPersistent (#1257, @iMacTia)
89
+ * Drop CodeClimate (#1259, @olleolleolle)
90
+ * CI: Rename default branch to main (#1263, @olleolleolle)
91
+ * Drop RDoc support file .document (#1264, @olleolleolle, @iMacTia)
92
+ * CONTRIBUTING: add a policy on inclusive language (#1262, @olleolleolle)
93
+ * Add rubocop-inclusivity (#1267, @olleolleolle, @iMacTia)
94
+
95
+ ## [v1.3.1](https://github.com/lostisland/faraday/releases/tag/v1.3.1) (2021-04-16)
96
+
97
+ ### Fixes
98
+
99
+ * Escape colon in path segment (#1237, @yarafan)
100
+ * Handle IPv6 address String on Faraday::Connection#proxy_from_env (#1252, @cosmo0920)
101
+
102
+ ### Documentation
103
+
104
+ * Fix broken Rubydoc.info links (#1236, @nickcampbell18)
105
+ * Add httpx to list of external adapters (#1246, @HoneyryderChuck)
106
+
107
+ ### Misc
108
+
109
+ * Refactor CI to remove duplicated line (#1230, @tricknotes)
110
+ * Gemspec: Pick a good ruby2_keywords release (#1241, @olleolleolle)
111
+
3
112
  ## [v1.3.0](https://github.com/lostisland/faraday/releases/tag/v1.3.0) (2020-12-31)
4
113
 
5
114
  ### Highlights
@@ -104,7 +213,7 @@ Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS
104
213
  * Website: add search bar (#1116)
105
214
  * Fix request/response mix-up in docs text (#1132)
106
215
 
107
- ## v1.0
216
+ ## [v1.0](https://github.com/lostisland/faraday/releases/tag/v1.0.0) (2020-01-22)
108
217
 
109
218
  Features:
110
219
 
@@ -148,6 +257,13 @@ Misc:
148
257
  * Describe clearing cached stubs #1045 (@viraptor)
149
258
  * Add project metadata to the gemspec #1046 (@orien)
150
259
 
260
+ ## v0.17.4
261
+
262
+ Fixes:
263
+
264
+ * NetHttp adapter: wrap Errno::EADDRNOTAVAIL (#1114, @embs)
265
+ * Fix === for subclasses of deprecated classes (#1243, @mervync)
266
+
151
267
  ## v0.17.3
152
268
 
153
269
  Fixes:
data/README.md CHANGED
@@ -2,12 +2,18 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
4
  [![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
5
- [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
5
+ [![GitHub Discussions](https://img.shields.io/github/discussions/lostisland/faraday?logo=github)](https://github.com/lostisland/faraday/discussions)
6
6
 
7
7
 
8
- Faraday is an HTTP client library that provides a common interface over many
9
- adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
10
- processing the request/response cycle.
8
+ Faraday is an HTTP client library abstraction layer that provides a common interface over many
9
+ adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle.
10
+ You probably don't want to use Faraday directly in your project, as it will lack an actual client library to perform
11
+ requests. Instead, you probably want to have a look at [Awesome Faraday][awesome] for a list of available adapters.
12
+
13
+ ## FARADAY 2.0
14
+
15
+ You're reading the README and looking at the code of our upcoming v2.0 release (the `main` branch, currently in alpha).
16
+ If you're here to read about our latest v1.x release, then please head over to the [1.x branch](https://github.com/lostisland/faraday/tree/1.x).
11
17
 
12
18
  ## Getting Started
13
19
 
@@ -16,10 +22,10 @@ Need more details? See the [Faraday API Documentation][apidoc] to see how it wor
16
22
 
17
23
  ## Supported Ruby versions
18
24
 
19
- This library aims to support and is [tested against][actions] the following Ruby
20
- implementations:
21
-
22
- * Ruby 2.4+
25
+ This library aims to support and is [tested against][actions] the currently officially supported Ruby
26
+ implementations. This means that, even without a major release, we could add or drop support for Ruby versions,
27
+ following their [EOL](https://endoflife.date/ruby).
28
+ Currently that means we support Ruby 2.6+
23
29
 
24
30
  If something doesn't work on one of these Ruby versions, it's a bug.
25
31
 
@@ -41,8 +47,9 @@ Open the issues page and check for the `help wanted` label!
41
47
  But before you start coding, please read our [Contributing Guide][contributing]
42
48
 
43
49
  ## Copyright
44
- © 2009 - 2020, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
50
+ © 2009 - 2021, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
45
51
 
52
+ [awesome]: https://github.com/lostisland/awesome-faraday/#adapters
46
53
  [website]: https://lostisland.github.io/faraday
47
54
  [faraday_team]: https://lostisland.github.io/faraday/team
48
55
  [contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
@@ -56,7 +56,7 @@ RSpec.describe Client do
56
56
 
57
57
  it 'handles exception' do
58
58
  stubs.get('/ebi') do
59
- raise Faraday::ConnectionFailed, nil
59
+ raise Faraday::ConnectionFailed
60
60
  end
61
61
 
62
62
  expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
@@ -42,7 +42,7 @@ class ClientTest < Test::Unit::TestCase
42
42
  stubs.verify_stubbed_calls
43
43
  end
44
44
 
45
- def test_sushi_404
45
+ def test_sushi_not_found
46
46
  stubs = Faraday::Adapter::Test::Stubs.new
47
47
  stubs.get('/ebi') do
48
48
  [
@@ -60,7 +60,7 @@ class ClientTest < Test::Unit::TestCase
60
60
  def test_sushi_exception
61
61
  stubs = Faraday::Adapter::Test::Stubs.new
62
62
  stubs.get('/ebi') do
63
- raise Faraday::ConnectionFailed, nil
63
+ raise Faraday::ConnectionFailed
64
64
  end
65
65
 
66
66
  cli = client(stubs)
@@ -55,6 +55,7 @@ module Faraday
55
55
  @stack = {}
56
56
  @consumed = {}
57
57
  @strict_mode = strict_mode
58
+ @stubs_mutex = Monitor.new
58
59
  yield(self) if block_given?
59
60
  end
60
61
 
@@ -70,10 +71,13 @@ module Faraday
70
71
  stack = @stack[request_method]
71
72
  consumed = (@consumed[request_method] ||= [])
72
73
 
73
- stub, meta = matches?(stack, env)
74
- if stub
75
- consumed << stack.delete(stub)
76
- return stub, meta
74
+ @stubs_mutex.synchronize do
75
+ stub, meta = matches?(stack, env)
76
+ if stub
77
+ removed = stack.delete(stub)
78
+ consumed << removed unless removed.nil?
79
+ return stub, meta
80
+ end
77
81
  end
78
82
  matches?(consumed, env)
79
83
  end
@@ -260,3 +264,5 @@ module Faraday
260
264
  end
261
265
  end
262
266
  end
267
+
268
+ Faraday::Adapter.register_middleware(test: Faraday::Adapter::Test)
@@ -5,14 +5,9 @@ module Faraday
5
5
  # responsible for fulfilling a Faraday request.
6
6
  class Adapter
7
7
  extend MiddlewareRegistry
8
- extend DependencyLoader
9
8
 
10
9
  CONTENT_LENGTH = 'Content-Length'
11
10
 
12
- register_middleware File.expand_path('adapter', __dir__),
13
- test: [:Test, 'test'],
14
- typhoeus: [:Typhoeus, 'typhoeus']
15
-
16
11
  # This module marks an Adapter as supporting parallel requests.
17
12
  module Parallelism
18
13
  attr_writer :supports_parallel
@@ -103,3 +98,5 @@ module Faraday
103
98
  }.freeze
104
99
  end
105
100
  end
101
+
102
+ require 'faraday/adapter/test'
@@ -64,7 +64,7 @@ module Faraday
64
64
  options = ConnectionOptions.from(options)
65
65
 
66
66
  if url.is_a?(Hash) || url.is_a?(ConnectionOptions)
67
- options = options.merge(url)
67
+ options = Utils.deep_merge(options, url)
68
68
  url = options.url
69
69
  end
70
70
 
@@ -117,7 +117,7 @@ module Faraday
117
117
 
118
118
  extend Forwardable
119
119
 
120
- def_delegators :builder, :build, :use, :request, :response, :adapter, :app
120
+ def_delegators :builder, :use, :request, :response, :adapter, :app
121
121
 
122
122
  # Closes the underlying resources and/or connections. In the case of
123
123
  # persistent connections, this closes all currently open connections
@@ -283,77 +283,6 @@ module Faraday
283
283
  RUBY
284
284
  end
285
285
 
286
- # Sets up the Authorization header with these credentials, encoded
287
- # with base64.
288
- #
289
- # @param login [String] The authentication login.
290
- # @param pass [String] The authentication password.
291
- #
292
- # @example
293
- #
294
- # conn.basic_auth 'Aladdin', 'open sesame'
295
- # conn.headers['Authorization']
296
- # # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
297
- #
298
- # @return [void]
299
- def basic_auth(login, pass)
300
- warn <<~TEXT
301
- WARNING: `Faraday::Connection#basic_auth` is deprecated; it will be removed in version 2.0.
302
- While initializing your connection, use `#request(:basic_auth, ...)` instead.
303
- See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
304
- TEXT
305
- set_authorization_header(:basic_auth, login, pass)
306
- end
307
-
308
- # Sets up the Authorization header with the given token.
309
- #
310
- # @param token [String]
311
- # @param options [Hash] extra token options.
312
- #
313
- # @example
314
- #
315
- # conn.token_auth 'abcdef', foo: 'bar'
316
- # conn.headers['Authorization']
317
- # # => "Token token=\"abcdef\",
318
- # foo=\"bar\""
319
- #
320
- # @return [void]
321
- def token_auth(token, options = nil)
322
- warn <<~TEXT
323
- WARNING: `Faraday::Connection#token_auth` is deprecated; it will be removed in version 2.0.
324
- While initializing your connection, use `#request(:token_auth, ...)` instead.
325
- See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
326
- TEXT
327
- set_authorization_header(:token_auth, token, options)
328
- end
329
-
330
- # Sets up a custom Authorization header.
331
- #
332
- # @param type [String] authorization type
333
- # @param token [String, Hash] token. A String value is taken literally, and
334
- # a Hash is encoded into comma-separated key/value pairs.
335
- #
336
- # @example
337
- #
338
- # conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
339
- # conn.headers['Authorization']
340
- # # => "Bearer mF_9.B5f-4.1JqM"
341
- #
342
- # conn.authorization :Token, token: 'abcdef', foo: 'bar'
343
- # conn.headers['Authorization']
344
- # # => "Token token=\"abcdef\",
345
- # foo=\"bar\""
346
- #
347
- # @return [void]
348
- def authorization(type, token)
349
- warn <<~TEXT
350
- WARNING: `Faraday::Connection#authorization` is deprecated; it will be removed in version 2.0.
351
- While initializing your connection, use `#request(:authorization, ...)` instead.
352
- See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
353
- TEXT
354
- set_authorization_header(:authorization, type, token)
355
- end
356
-
357
286
  # Check if the adapter is parallel-capable.
358
287
  #
359
288
  # @yield if the adapter isn't parallel-capable, or if no adapter is set yet.
@@ -441,7 +370,7 @@ module Faraday
441
370
  end
442
371
 
443
372
  def set_basic_auth(user, password)
444
- header = Faraday::Request::BasicAuthentication.header(user, password)
373
+ header = Faraday::Utils.basic_header_from(user, password)
445
374
  headers[Faraday::Request::Authorization::KEY] = header
446
375
  end
447
376
 
@@ -545,14 +474,12 @@ module Faraday
545
474
  if url && base.path && base.path !~ %r{/$}
546
475
  base.path = "#{base.path}/" # ensure trailing slash
547
476
  end
548
- url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url
477
+ url = url.to_s.gsub(':', '%3A') if url && URI.parse(url.to_s).opaque
549
478
  uri = url ? base + url : base
550
479
  if params
551
480
  uri.query = params.to_query(params_encoder || options.params_encoder)
552
481
  end
553
- # rubocop:disable Style/SafeNavigation
554
482
  uri.query = nil if uri.query && uri.query.empty?
555
- # rubocop:enable Style/SafeNavigation
556
483
  uri
557
484
  end
558
485
 
@@ -584,14 +511,6 @@ module Faraday
584
511
  yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
585
512
  end
586
513
 
587
- def set_authorization_header(header_type, *args)
588
- header = Faraday::Request
589
- .lookup_middleware(header_type)
590
- .header(*args)
591
-
592
- headers[Faraday::Request::Authorization::KEY] = header
593
- end
594
-
595
514
  def proxy_from_env(url)
596
515
  return if Faraday.ignore_env_proxy
597
516
 
@@ -636,7 +555,7 @@ module Faraday
636
555
  end
637
556
 
638
557
  def support_parallel?(adapter)
639
- adapter&.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
558
+ adapter.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
640
559
  end
641
560
  end
642
561
  end
@@ -102,7 +102,7 @@ module Faraday
102
102
  subkeys = key.scan(SUBKEYS_REGEX)
103
103
  subkeys.each_with_index do |subkey, i|
104
104
  is_array = subkey =~ /[\[\]]+\Z/
105
- subkey = $` if is_array
105
+ subkey = Regexp.last_match.pre_match if is_array
106
106
  last_subkey = i == subkeys.length - 1
107
107
 
108
108
  context = prepare_context(context, subkey, is_array, last_subkey)
@@ -124,7 +124,7 @@ module Faraday
124
124
  value_type = is_array ? Array : Hash
125
125
  if context[subkey] && !context[subkey].is_a?(value_type)
126
126
  raise TypeError, "expected #{value_type.name} " \
127
- "(got #{context[subkey].class.name}) for param `#{subkey}'"
127
+ "(got #{context[subkey].class.name}) for param `#{subkey}'"
128
128
  end
129
129
 
130
130
  context[subkey] ||= value_type.new
data/lib/faraday/error.rb CHANGED
@@ -6,7 +6,7 @@ module Faraday
6
6
  class Error < StandardError
7
7
  attr_reader :response, :wrapped_exception
8
8
 
9
- def initialize(exc, response = nil)
9
+ def initialize(exc = nil, response = nil)
10
10
  @wrapped_exception = nil unless defined?(@wrapped_exception)
11
11
  @response = nil unless defined?(@response)
12
12
  super(exc_msg_and_response!(exc, response))
@@ -52,6 +52,7 @@ module Faraday
52
52
  # :body - Optional string HTTP response body.
53
53
  # :request - Hash
54
54
  # :method - Symbol with the request HTTP method.
55
+ # :url - URI object with the url requested.
55
56
  # :url_path - String with the url path requested.
56
57
  # :params - String key/value hash of query params
57
58
  # present in the request.
@@ -140,13 +141,7 @@ module Faraday
140
141
  class SSLError < Error
141
142
  end
142
143
 
143
- # Raised by FaradayMiddleware::ResponseMiddleware
144
+ # Raised by middlewares that parse the response, like the JSON response middleware.
144
145
  class ParsingError < Error
145
146
  end
146
-
147
- # Exception used to control the Retry middleware.
148
- #
149
- # @see Faraday::Request::Retry
150
- class RetriableResponse < Error
151
- end
152
147
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'pp'
4
+
4
5
  module Faraday
5
6
  module Logging
6
7
  # Serves as an integration point to customize logging
@@ -4,7 +4,6 @@ module Faraday
4
4
  # Middleware is the basic base class of any Faraday middleware.
5
5
  class Middleware
6
6
  extend MiddlewareRegistry
7
- extend DependencyLoader
8
7
 
9
8
  attr_reader :app, :options
10
9
 
@@ -6,59 +6,26 @@ module Faraday
6
6
  # Adds the ability for other modules to register and lookup
7
7
  # middleware classes.
8
8
  module MiddlewareRegistry
9
+ def registered_middleware
10
+ @registered_middleware ||= {}
11
+ end
12
+
9
13
  # Register middleware class(es) on the current module.
10
14
  #
11
- # @param autoload_path [String] Middleware autoload path
12
- # @param mapping [Hash{
13
- # Symbol => Module,
14
- # Symbol => Array<Module, Symbol, String>,
15
- # }] Middleware mapping from a lookup symbol to a reference to the
16
- # middleware.
17
- # Classes can be expressed as:
18
- # - a fully qualified constant
19
- # - a Symbol
20
- # - a Proc that will be lazily called to return the former
21
- # - an array is given, its first element is the constant or symbol,
22
- # and its second is a file to `require`.
15
+ # @param mappings [Hash] Middleware mappings from a lookup symbol to a middleware class.
23
16
  # @return [void]
24
17
  #
25
18
  # @example Lookup by a constant
26
19
  #
27
20
  # module Faraday
28
- # class Whatever
21
+ # class Whatever < Middleware
29
22
  # # Middleware looked up by :foo returns Faraday::Whatever::Foo.
30
- # register_middleware foo: Foo
31
- # end
32
- # end
33
- #
34
- # @example Lookup by a symbol
35
- #
36
- # module Faraday
37
- # class Whatever
38
- # # Middleware looked up by :bar returns
39
- # # Faraday::Whatever.const_get(:Bar)
40
- # register_middleware bar: :Bar
41
- # end
42
- # end
43
- #
44
- # @example Lookup by a symbol and string in an array
45
- #
46
- # module Faraday
47
- # class Whatever
48
- # # Middleware looked up by :baz requires 'baz' and returns
49
- # # Faraday::Whatever.const_get(:Baz)
50
- # register_middleware baz: [:Baz, 'baz']
23
+ # register_middleware(foo: Whatever)
51
24
  # end
52
25
  # end
53
- #
54
- def register_middleware(autoload_path = nil, mapping = nil)
55
- if mapping.nil?
56
- mapping = autoload_path
57
- autoload_path = nil
58
- end
26
+ def register_middleware(**mappings)
59
27
  middleware_mutex do
60
- @middleware_autoload_path = autoload_path if autoload_path
61
- (@registered_middleware ||= {}).update(mapping)
28
+ registered_middleware.update(mappings)
62
29
  end
63
30
  end
64
31
 
@@ -66,7 +33,7 @@ module Faraday
66
33
  #
67
34
  # @param key [Symbol] key for the registered middleware.
68
35
  def unregister_middleware(key)
69
- @registered_middleware.delete(key)
36
+ registered_middleware.delete(key)
70
37
  end
71
38
 
72
39
  # Lookup middleware class with a registered Symbol shortcut.
@@ -78,16 +45,15 @@ module Faraday
78
45
  # @example
79
46
  #
80
47
  # module Faraday
81
- # class Whatever
82
- # register_middleware foo: Foo
48
+ # class Whatever < Middleware
49
+ # register_middleware(foo: Whatever)
83
50
  # end
84
51
  # end
85
52
  #
86
- # Faraday::Whatever.lookup_middleware(:foo)
87
- # # => Faraday::Whatever::Foo
88
- #
53
+ # Faraday::Middleware.lookup_middleware(:foo)
54
+ # # => Faraday::Whatever
89
55
  def lookup_middleware(key)
90
- load_middleware(key) ||
56
+ registered_middleware[key] ||
91
57
  raise(Faraday::Error, "#{key.inspect} is not registered on #{self}")
92
58
  end
93
59
 
@@ -95,35 +61,5 @@ module Faraday
95
61
  @middleware_mutex ||= Monitor.new
96
62
  @middleware_mutex.synchronize(&block)
97
63
  end
98
-
99
- def fetch_middleware(key)
100
- defined?(@registered_middleware) && @registered_middleware[key]
101
- end
102
-
103
- def load_middleware(key)
104
- value = fetch_middleware(key)
105
- case value
106
- when Module
107
- value
108
- when Symbol, String
109
- middleware_mutex do
110
- @registered_middleware[key] = const_get(value)
111
- end
112
- when Proc
113
- middleware_mutex do
114
- @registered_middleware[key] = value.call
115
- end
116
- when Array
117
- middleware_mutex do
118
- const, path = value
119
- if (root = @middleware_autoload_path)
120
- path = "#{root}/#{path}"
121
- end
122
- require(path)
123
- @registered_middleware[key] = const
124
- end
125
- load_middleware(key)
126
- end
127
- end
128
64
  end
129
65
  end
@@ -104,7 +104,7 @@ module Faraday
104
104
 
105
105
  # Public
106
106
  def each_key(&block)
107
- return to_enum(:each_key) unless block_given?
107
+ return to_enum(:each_key) unless block
108
108
 
109
109
  keys.each(&block)
110
110
  end
@@ -118,7 +118,7 @@ module Faraday
118
118
 
119
119
  # Public
120
120
  def each_value(&block)
121
- return to_enum(:each_value) unless block_given?
121
+ return to_enum(:each_value) unless block
122
122
 
123
123
  values.each(&block)
124
124
  end
@@ -168,7 +168,7 @@ module Faraday
168
168
  end
169
169
 
170
170
  def self.memoized(key, &block)
171
- unless block_given?
171
+ unless block
172
172
  raise ArgumentError, '#memoized must be called with a block'
173
173
  end
174
174