faraday 1.0.0 → 1.1.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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +3 -4
  4. data/examples/client_spec.rb +1 -1
  5. data/lib/faraday/adapter/em_http.rb +3 -2
  6. data/lib/faraday/adapter/excon.rb +2 -2
  7. data/lib/faraday/adapter/httpclient.rb +2 -1
  8. data/lib/faraday/adapter/net_http.rb +18 -8
  9. data/lib/faraday/adapter/typhoeus.rb +1 -1
  10. data/lib/faraday/adapter.rb +1 -0
  11. data/lib/faraday/adapter_registry.rb +3 -1
  12. data/lib/faraday/autoload.rb +1 -1
  13. data/lib/faraday/connection.rb +3 -3
  14. data/lib/faraday/dependency_loader.rb +3 -1
  15. data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
  16. data/lib/faraday/encoders/nested_params_encoder.rb +7 -2
  17. data/lib/faraday/error.rb +8 -0
  18. data/lib/faraday/options.rb +4 -8
  19. data/lib/faraday/rack_builder.rb +13 -12
  20. data/lib/faraday/request/authorization.rb +3 -1
  21. data/lib/faraday/request/multipart.rb +10 -3
  22. data/lib/faraday/request/url_encoded.rb +3 -1
  23. data/lib/faraday/request.rb +20 -10
  24. data/lib/faraday/response/raise_error.rb +12 -1
  25. data/lib/faraday/response.rb +4 -1
  26. data/lib/faraday/utils/headers.rb +2 -2
  27. data/lib/faraday/utils.rb +11 -3
  28. data/lib/faraday.rb +2 -2
  29. data/spec/faraday/adapter/em_http_spec.rb +1 -1
  30. data/spec/faraday/adapter/em_synchrony_spec.rb +1 -1
  31. data/spec/faraday/adapter/patron_spec.rb +1 -1
  32. data/spec/faraday/adapter/test_spec.rb +260 -0
  33. data/spec/faraday/params_encoders/flat_spec.rb +8 -0
  34. data/spec/faraday/params_encoders/nested_spec.rb +8 -0
  35. data/spec/faraday/rack_builder_spec.rb +150 -1
  36. data/spec/faraday/request/authorization_spec.rb +2 -2
  37. data/spec/faraday/request/multipart_spec.rb +41 -13
  38. data/spec/faraday/request/url_encoded_spec.rb +13 -0
  39. data/spec/faraday/request_spec.rb +16 -5
  40. data/spec/faraday/response/middleware_spec.rb +16 -0
  41. data/spec/faraday/response/raise_error_spec.rb +33 -0
  42. data/spec/support/shared_examples/request_method.rb +3 -3
  43. metadata +21 -6
  44. data/UPGRADING.md +0 -55
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d3d650c5d465a0862cddd7ab2ed27fbfb4a6e5f2cfcbb54ba753bcc1ce9e40e
4
- data.tar.gz: 953336deeb3dd2e0228368209bdffc7815b376772269c439e21374bc22b70484
3
+ metadata.gz: 47236f354f9fda55254c8bd2b9e3552de12e8894fcecea0035ffbeb3ccc46b9c
4
+ data.tar.gz: f6eb36d3da02314fe799368c1ff4c17c5841f0badd18c5a89772e00bae43065c
5
5
  SHA512:
6
- metadata.gz: b80c084a8714d995dec61c918891a63720dbcdddf2eb3ecb7106f40a03bb341c4f03ffe976365c5450b2dcffd0b1f52c1cd4320ea24591dfaef7d5fdbbe12562
7
- data.tar.gz: 67f8134fae885d3bb2e621afa701d6d535409bbb0141ad699f4d290059d61bf856915f622e214251f05e8b893a69bf68439118074712a22e75e3705b249dbb85
6
+ metadata.gz: 300153861873b2a5b3a3a9f552c47a2b70dd083ffd5478d8eca2c4c2005e5a74d09d962b51fb9a780ea90bb1ac87bdfa902639e86b2147d5b4d4374791789bd1
7
+ data.tar.gz: 3628d85508cdd669111ae6175b6b5a191380d0d405f345612f6342e487aec0f3cc9d2e1726a2cdde7d82ee86fae82aa41f4a62ea330343598383ae3f644b5a85
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2019 Rick Olson, Zack Hobson
1
+ Copyright (c) 2009-2020 Rick Olson, Zack Hobson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
- # ![Faraday](./docs/assets/img/repo-card-slim.png)
1
+ # [![Faraday](./docs/assets/img/repo-card-slim.png)][website]
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
- ![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/test_coverage)](https://codeclimate.com/github/lostisland/faraday/test_coverage)
4
+ [![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
6
5
  [![Maintainability](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/maintainability)](https://codeclimate.com/github/lostisland/faraday/maintainability)
7
6
  [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
8
7
 
@@ -43,7 +42,7 @@ Open the issues page and check for the `help wanted` label!
43
42
  But before you start coding, please read our [Contributing Guide][contributing]
44
43
 
45
44
  ## Copyright
46
- © 2009 - 2019, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
45
+ © 2009 - 2020, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
47
46
 
48
47
  [website]: https://lostisland.github.io/faraday
49
48
  [faraday_team]: https://lostisland.github.io/faraday/team
@@ -19,7 +19,7 @@ class Client
19
19
  end
20
20
  end
21
21
 
22
- Rspec.describe Client do
22
+ RSpec.describe Client do
23
23
  let(:stubs) { Faraday::Adapter::Test::Stubs.new }
24
24
  let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
25
25
  let(:client) { Client.new(conn) }
@@ -142,7 +142,8 @@ module Faraday
142
142
 
143
143
  raise Faraday::ConnectionFailed, e
144
144
  rescue StandardError => e
145
- if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
145
+ if defined?(::OpenSSL::SSL::SSLError) && \
146
+ e.is_a?(::OpenSSL::SSL::SSLError)
146
147
  raise Faraday::SSLError, e
147
148
  end
148
149
 
@@ -230,7 +231,7 @@ module Faraday
230
231
 
231
232
  def add(&block)
232
233
  if running?
233
- perform_request { yield }
234
+ perform_request(&block)
234
235
  else
235
236
  @registered_procs << block
236
237
  end
@@ -34,9 +34,9 @@ module Faraday
34
34
 
35
35
  @app.call(env)
36
36
  rescue ::Excon::Errors::SocketError => e
37
- raise Faraday::TimeoutError, e if e.message =~ /\btimeout\b/
37
+ raise Faraday::TimeoutError, e if e.message.match?(/\btimeout\b/)
38
38
 
39
- raise Faraday::SSLError, e if e.message =~ /\bcertificate\b/
39
+ raise Faraday::SSLError, e if e.message.match?(/\bcertificate\b/)
40
40
 
41
41
  raise Faraday::ConnectionFailed, e
42
42
  rescue ::Excon::Errors::Timeout => e
@@ -66,7 +66,8 @@ module Faraday
66
66
  rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, IOError, SocketError
67
67
  raise Faraday::ConnectionFailed, $ERROR_INFO
68
68
  rescue StandardError => e
69
- if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
69
+ if defined?(::OpenSSL::SSL::SSLError) && \
70
+ e.is_a?(::OpenSSL::SSL::SSLError)
70
71
  raise Faraday::SSLError, e
71
72
  end
72
73
 
@@ -30,8 +30,10 @@ module Faraday
30
30
  Zlib::GzipFile::Error
31
31
  ]
32
32
 
33
- exceptions << OpenSSL::SSL::SSLError if defined?(OpenSSL)
34
- exceptions << Net::OpenTimeout if defined?(Net::OpenTimeout)
33
+ if defined?(::OpenSSL::SSL::SSLError)
34
+ exceptions << ::OpenSSL::SSL::SSLError
35
+ end
36
+ exceptions << ::Net::OpenTimeout if defined?(::Net::OpenTimeout)
35
37
 
36
38
  NET_HTTP_EXCEPTIONS = exceptions.freeze
37
39
 
@@ -137,16 +139,24 @@ module Faraday
137
139
  end
138
140
 
139
141
  def request_via_get_method(http, env, &block)
140
- http.get env[:url].request_uri, env[:request_headers], &block
142
+ # Must use Net::HTTP#start and pass it a block otherwise the server's
143
+ # TCP socket does not close correctly.
144
+ http.start do |opened_http|
145
+ opened_http.get env[:url].request_uri, env[:request_headers], &block
146
+ end
141
147
  end
142
148
 
143
149
  def request_via_request_method(http, env, &block)
144
- if block_given?
145
- http.request create_request(env) do |response|
146
- response.read_body(&block)
150
+ # Must use Net::HTTP#start and pass it a block otherwise the server's
151
+ # TCP socket does not close correctly.
152
+ http.start do |opened_http|
153
+ if block_given?
154
+ opened_http.request create_request(env) do |response|
155
+ response.read_body(&block)
156
+ end
157
+ else
158
+ opened_http.request create_request(env)
147
159
  end
148
- else
149
- http.request create_request(env)
150
160
  end
151
161
  end
152
162
 
@@ -3,7 +3,7 @@
3
3
  module Faraday
4
4
  class Adapter
5
5
  # Typhoeus adapter. This class is just a stub, the real adapter is in
6
- # https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
6
+ # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
7
7
  class Typhoeus < Faraday::Adapter
8
8
  # Needs to define this method in order to support Typhoeus <= 1.3.0
9
9
  def call; end
@@ -27,6 +27,7 @@ module Faraday
27
27
  # This module marks an Adapter as supporting parallel requests.
28
28
  module Parallelism
29
29
  attr_writer :supports_parallel
30
+
30
31
  def supports_parallel?
31
32
  @supports_parallel
32
33
  end
@@ -12,7 +12,9 @@ module Faraday
12
12
  end
13
13
 
14
14
  def get(name)
15
- klass = @constants[name]
15
+ klass = @lock.synchronize do
16
+ @constants[name]
17
+ end
16
18
  return klass if klass
17
19
 
18
20
  Object.const_get(name).tap { |c| set(c, name) }
@@ -23,7 +23,7 @@ module Faraday
23
23
  #
24
24
  # @return [void]
25
25
  def autoload_all(prefix, options)
26
- if prefix =~ %r{^faraday(/|$)}i
26
+ if prefix.match? %r{^faraday(/|$)}i
27
27
  prefix = File.join(Faraday.root_path, prefix)
28
28
  end
29
29
 
@@ -429,7 +429,7 @@ module Faraday
429
429
  # @return [String] the new path prefix
430
430
  def path_prefix=(value)
431
431
  url_prefix.path = if value
432
- value = '/' + value unless value[0, 1] == '/'
432
+ value = "/#{value}" unless value[0, 1] == '/'
433
433
  value
434
434
  end
435
435
  end
@@ -520,7 +520,7 @@ module Faraday
520
520
  base = url_prefix
521
521
  if url && base.path && base.path !~ %r{/$}
522
522
  base = base.dup
523
- base.path = base.path + '/' # ensure trailing slash
523
+ base.path = "#{base.path}/" # ensure trailing slash
524
524
  end
525
525
  uri = url ? base + url : base
526
526
  if params
@@ -593,7 +593,7 @@ module Faraday
593
593
  uri = ENV['http_proxy']
594
594
  return unless uri && !uri.empty?
595
595
 
596
- uri = 'http://' + uri if uri !~ /^http/i
596
+ uri = "http://#{uri}" unless uri.match?(/^http/i)
597
597
  uri
598
598
  end
599
599
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ruby2_keywords'
4
+
3
5
  module Faraday
4
6
  # DependencyLoader helps Faraday adapters and middleware load dependencies.
5
7
  module DependencyLoader
@@ -13,7 +15,7 @@ module Faraday
13
15
  self.load_error = e
14
16
  end
15
17
 
16
- def new(*)
18
+ ruby2_keywords def new(*)
17
19
  unless loaded?
18
20
  raise "missing dependency for #{self}: #{load_error.message}"
19
21
  end
@@ -33,9 +33,9 @@ module Faraday
33
33
  key = key.to_s if key.is_a?(Symbol)
34
34
  [key, value]
35
35
  end
36
- # Useful default for OAuth and caching.
36
+
37
37
  # Only to be used for non-Array inputs. Arrays should preserve order.
38
- params.sort!
38
+ params.sort! if @sort_params
39
39
  end
40
40
 
41
41
  # The params have form [['key1', 'value1'], ['key2', 'value2']].
@@ -94,5 +94,12 @@ module Faraday
94
94
  end
95
95
  end
96
96
  end
97
+
98
+ class << self
99
+ attr_accessor :sort_params
100
+ end
101
+
102
+ # Useful default for OAuth and caching.
103
+ @sort_params = true
97
104
  end
98
105
  end
@@ -21,9 +21,9 @@ module Faraday
21
21
  key = key.to_s if key.is_a?(Symbol)
22
22
  [key, value]
23
23
  end
24
- # Useful default for OAuth and caching.
24
+
25
25
  # Only to be used for non-Array inputs. Arrays should preserve order.
26
- params.sort!
26
+ params.sort! if @sort_params
27
27
  end
28
28
 
29
29
  # The params have form [['key1', 'value1'], ['key2', 'value2']].
@@ -161,10 +161,15 @@ module Faraday
161
161
  # for your requests.
162
162
  module NestedParamsEncoder
163
163
  class << self
164
+ attr_accessor :sort_params
165
+
164
166
  extend Forwardable
165
167
  def_delegators :'Faraday::Utils', :escape, :unescape
166
168
  end
167
169
 
170
+ # Useful default for OAuth and caching.
171
+ @sort_params = true
172
+
168
173
  extend EncodeMethods
169
174
  extend DecodeMethods
170
175
  end
data/lib/faraday/error.rb CHANGED
@@ -38,6 +38,14 @@ module Faraday
38
38
  # :headers - String key/value hash of HTTP response header
39
39
  # values.
40
40
  # :body - Optional string HTTP response body.
41
+ # :request - Hash
42
+ # :method - Symbol with the request HTTP method.
43
+ # :url_path - String with the url path requested.
44
+ # :params - String key/value hash of query params
45
+ # present in the request.
46
+ # :headers - String key/value hash of HTTP request
47
+ # header values.
48
+ # :body - String HTTP request body.
41
49
  #
42
50
  # If a subclass has to call this, then it should pass a string message
43
51
  # to `super`. See NilStatusError.
@@ -103,12 +103,10 @@ module Faraday
103
103
  end
104
104
 
105
105
  # Public
106
- def each_key
106
+ def each_key(&block)
107
107
  return to_enum(:each_key) unless block_given?
108
108
 
109
- keys.each do |key|
110
- yield(key)
111
- end
109
+ keys.each(&block)
112
110
  end
113
111
 
114
112
  # Public
@@ -119,12 +117,10 @@ module Faraday
119
117
  alias has_key? key?
120
118
 
121
119
  # Public
122
- def each_value
120
+ def each_value(&block)
123
121
  return to_enum(:each_value) unless block_given?
124
122
 
125
- values.each do |value|
126
- yield(value)
127
- end
123
+ values.each(&block)
128
124
  end
129
125
 
130
126
  # Public
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ruby2_keywords'
3
4
  require 'faraday/adapter_registry'
4
5
 
5
6
  module Faraday
@@ -27,7 +28,7 @@ module Faraday
27
28
 
28
29
  attr_reader :name
29
30
 
30
- def initialize(klass, *args, &block)
31
+ ruby2_keywords def initialize(klass, *args, &block)
31
32
  @name = klass.to_s
32
33
  REGISTRY.set(klass) if klass.respond_to?(:name)
33
34
  @args = args
@@ -89,7 +90,7 @@ module Faraday
89
90
  @handlers.frozen?
90
91
  end
91
92
 
92
- def use(klass, *args, &block)
93
+ ruby2_keywords def use(klass, *args, &block)
93
94
  if klass.is_a? Symbol
94
95
  use_symbol(Faraday::Middleware, klass, *args, &block)
95
96
  else
@@ -99,15 +100,15 @@ module Faraday
99
100
  end
100
101
  end
101
102
 
102
- def request(key, *args, &block)
103
+ ruby2_keywords def request(key, *args, &block)
103
104
  use_symbol(Faraday::Request, key, *args, &block)
104
105
  end
105
106
 
106
- def response(key, *args, &block)
107
+ ruby2_keywords def response(key, *args, &block)
107
108
  use_symbol(Faraday::Response, key, *args, &block)
108
109
  end
109
110
 
110
- def adapter(klass = NO_ARGUMENT, *args, &block)
111
+ ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
111
112
  return @adapter if klass == NO_ARGUMENT
112
113
 
113
114
  klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
@@ -116,7 +117,7 @@ module Faraday
116
117
 
117
118
  ## methods to push onto the various positions in the stack:
118
119
 
119
- def insert(index, *args, &block)
120
+ ruby2_keywords def insert(index, *args, &block)
120
121
  raise_if_locked
121
122
  index = assert_index(index)
122
123
  handler = self.class::Handler.new(*args, &block)
@@ -125,12 +126,12 @@ module Faraday
125
126
 
126
127
  alias insert_before insert
127
128
 
128
- def insert_after(index, *args, &block)
129
+ ruby2_keywords def insert_after(index, *args, &block)
129
130
  index = assert_index(index)
130
131
  insert(index + 1, *args, &block)
131
132
  end
132
133
 
133
- def swap(index, *args, &block)
134
+ ruby2_keywords def swap(index, *args, &block)
134
135
  raise_if_locked
135
136
  index = assert_index(index)
136
137
  @handlers.delete_at(index)
@@ -186,7 +187,7 @@ module Faraday
186
187
  end
187
188
 
188
189
  # ENV Keys
189
- # :method - a symbolized request method (:get, :post)
190
+ # :http_method - a symbolized request HTTP method (:get, :post)
190
191
  # :body - the request body that will eventually be converted to a string.
191
192
  # :url - URI instance for the current request.
192
193
  # :status - HTTP response status code
@@ -207,7 +208,7 @@ module Faraday
207
208
  request.options.params_encoder
208
209
  )
209
210
 
210
- Env.new(request.method, request.body, exclusive_url,
211
+ Env.new(request.http_method, request.body, exclusive_url,
211
212
  request.options, request.headers, connection.ssl,
212
213
  connection.parallel_manager)
213
214
  end
@@ -231,10 +232,10 @@ module Faraday
231
232
  end
232
233
 
233
234
  def is_adapter?(klass) # rubocop:disable Naming/PredicateName
234
- klass.ancestors.include?(Faraday::Adapter)
235
+ klass <= Faraday::Adapter
235
236
  end
236
237
 
237
- def use_symbol(mod, key, *args, &block)
238
+ ruby2_keywords def use_symbol(mod, key, *args, &block)
238
239
  use(mod.lookup_middleware(key), *args, &block)
239
240
  end
240
241
 
@@ -4,7 +4,9 @@ module Faraday
4
4
  class Request
5
5
  # Request middleware for the Authorization HTTP header
6
6
  class Authorization < Faraday::Middleware
7
- KEY = 'Authorization' unless defined? KEY
7
+ unless defined?(::Faraday::Request::Authorization::KEY)
8
+ KEY = 'Authorization'
9
+ end
8
10
 
9
11
  # @param type [String, Symbol]
10
12
  # @param token [String, Symbol, Hash]
@@ -8,10 +8,15 @@ module Faraday
8
8
  # Middleware for supporting multi-part requests.
9
9
  class Multipart < UrlEncoded
10
10
  self.mime_type = 'multipart/form-data'
11
- unless defined? DEFAULT_BOUNDARY_PREFIX
11
+ unless defined?(::Faraday::Request::Multipart::DEFAULT_BOUNDARY_PREFIX)
12
12
  DEFAULT_BOUNDARY_PREFIX = '-----------RubyMultipartPost'
13
13
  end
14
14
 
15
+ def initialize(app = nil, options = {})
16
+ super(app)
17
+ @options = options
18
+ end
19
+
15
20
  # Checks for files in the payload, otherwise leaves everything untouched.
16
21
  #
17
22
  # @param env [Faraday::Env]
@@ -30,7 +35,7 @@ module Faraday
30
35
  type = request_type(env)
31
36
  env.body.respond_to?(:each_key) && !env.body.empty? && (
32
37
  (type.empty? && has_multipart?(env.body)) ||
33
- (type == self.class.mime_type)
38
+ (type == self.class.mime_type)
34
39
  )
35
40
  end
36
41
 
@@ -79,7 +84,9 @@ module Faraday
79
84
  # @param pieces [Array]
80
85
  def process_params(params, prefix = nil, pieces = nil, &block)
81
86
  params.inject(pieces || []) do |all, (key, value)|
82
- key = "#{prefix}[#{key}]" if prefix
87
+ if prefix
88
+ key = @options[:flat_encode] ? prefix.to_s : "#{prefix}[#{key}]"
89
+ end
83
90
 
84
91
  case value
85
92
  when Array