faraday 0.17.6 → 1.0.0.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +18 -358
  4. data/lib/faraday/adapter/em_http.rb +142 -99
  5. data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
  6. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  7. data/lib/faraday/adapter/em_synchrony.rb +104 -60
  8. data/lib/faraday/adapter/excon.rb +97 -57
  9. data/lib/faraday/adapter/httpclient.rb +61 -39
  10. data/lib/faraday/adapter/net_http.rb +103 -51
  11. data/lib/faraday/adapter/net_http_persistent.rb +49 -28
  12. data/lib/faraday/adapter/patron.rb +54 -35
  13. data/lib/faraday/adapter/rack.rb +28 -12
  14. data/lib/faraday/adapter/test.rb +86 -53
  15. data/lib/faraday/adapter/typhoeus.rb +4 -1
  16. data/lib/faraday/adapter.rb +36 -22
  17. data/lib/faraday/adapter_registry.rb +28 -0
  18. data/lib/faraday/autoload.rb +47 -36
  19. data/lib/faraday/connection.rb +321 -179
  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 +21 -79
  24. data/lib/faraday/logging/formatter.rb +92 -0
  25. data/lib/faraday/middleware.rb +4 -28
  26. data/lib/faraday/middleware_registry.rb +129 -0
  27. data/lib/faraday/options/connection_options.rb +22 -0
  28. data/lib/faraday/options/env.rb +181 -0
  29. data/lib/faraday/options/proxy_options.rb +28 -0
  30. data/lib/faraday/options/request_options.rb +21 -0
  31. data/lib/faraday/options/ssl_options.rb +59 -0
  32. data/lib/faraday/options.rb +33 -184
  33. data/lib/faraday/parameters.rb +4 -197
  34. data/lib/faraday/rack_builder.rb +66 -55
  35. data/lib/faraday/request/authorization.rb +42 -30
  36. data/lib/faraday/request/basic_authentication.rb +14 -7
  37. data/lib/faraday/request/instrumentation.rb +45 -27
  38. data/lib/faraday/request/multipart.rb +72 -49
  39. data/lib/faraday/request/retry.rb +197 -171
  40. data/lib/faraday/request/token_authentication.rb +15 -10
  41. data/lib/faraday/request/url_encoded.rb +41 -23
  42. data/lib/faraday/request.rb +68 -38
  43. data/lib/faraday/response/logger.rb +22 -69
  44. data/lib/faraday/response/raise_error.rb +36 -18
  45. data/lib/faraday/response.rb +22 -15
  46. data/lib/faraday/upload_io.rb +31 -30
  47. data/lib/faraday/utils/headers.rb +139 -0
  48. data/lib/faraday/utils/params_hash.rb +61 -0
  49. data/lib/faraday/utils.rb +28 -245
  50. data/lib/faraday.rb +93 -174
  51. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  52. metadata +25 -51
  53. data/CHANGELOG.md +0 -232
  54. data/Rakefile +0 -13
  55. data/lib/faraday/deprecate.rb +0 -109
  56. data/spec/faraday/deprecate_spec.rb +0 -147
  57. data/spec/faraday/error_spec.rb +0 -102
  58. data/spec/faraday/response/raise_error_spec.rb +0 -106
  59. data/spec/spec_helper.rb +0 -105
  60. data/test/adapters/default_test.rb +0 -14
  61. data/test/adapters/em_http_test.rb +0 -30
  62. data/test/adapters/em_synchrony_test.rb +0 -32
  63. data/test/adapters/excon_test.rb +0 -30
  64. data/test/adapters/httpclient_test.rb +0 -34
  65. data/test/adapters/integration.rb +0 -263
  66. data/test/adapters/logger_test.rb +0 -136
  67. data/test/adapters/net_http_persistent_test.rb +0 -114
  68. data/test/adapters/net_http_test.rb +0 -79
  69. data/test/adapters/patron_test.rb +0 -40
  70. data/test/adapters/rack_test.rb +0 -38
  71. data/test/adapters/test_middleware_test.rb +0 -157
  72. data/test/adapters/typhoeus_test.rb +0 -38
  73. data/test/authentication_middleware_test.rb +0 -65
  74. data/test/composite_read_io_test.rb +0 -109
  75. data/test/connection_test.rb +0 -738
  76. data/test/env_test.rb +0 -268
  77. data/test/helper.rb +0 -75
  78. data/test/live_server.rb +0 -67
  79. data/test/middleware/instrumentation_test.rb +0 -88
  80. data/test/middleware/retry_test.rb +0 -282
  81. data/test/middleware_stack_test.rb +0 -260
  82. data/test/multibyte.txt +0 -1
  83. data/test/options_test.rb +0 -333
  84. data/test/parameters_test.rb +0 -157
  85. data/test/request_middleware_test.rb +0 -126
  86. data/test/response_middleware_test.rb +0 -72
  87. data/test/strawberry.rb +0 -2
  88. data/test/utils_test.rb +0 -98
@@ -1,36 +1,54 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
- class Request::UrlEncoded < Faraday::Middleware
3
- CONTENT_TYPE = 'Content-Type'.freeze unless defined? CONTENT_TYPE
4
+ class Request
5
+ # Middleware for supporting urlencoded requests.
6
+ class UrlEncoded < Faraday::Middleware
7
+ CONTENT_TYPE = 'Content-Type' unless defined? CONTENT_TYPE
4
8
 
5
- class << self
6
- attr_accessor :mime_type
7
- end
8
- self.mime_type = 'application/x-www-form-urlencoded'.freeze
9
+ class << self
10
+ attr_accessor :mime_type
11
+ end
12
+ self.mime_type = 'application/x-www-form-urlencoded'
9
13
 
10
- def call(env)
11
- match_content_type(env) do |data|
12
- params = Faraday::Utils::ParamsHash[data]
13
- env.body = params.to_query(env.params_encoder)
14
+ # Encodes as "application/x-www-form-urlencoded" if not already encoded or
15
+ # of another type.
16
+ #
17
+ # @param env [Faraday::Env]
18
+ def call(env)
19
+ match_content_type(env) do |data|
20
+ params = Faraday::Utils::ParamsHash[data]
21
+ env.body = params.to_query(env.params_encoder)
22
+ end
23
+ @app.call env
14
24
  end
15
- @app.call env
16
- end
17
25
 
18
- def match_content_type(env)
19
- if process_request?(env)
26
+ # @param env [Faraday::Env]
27
+ # @yield [request_body] Body of the request
28
+ def match_content_type(env)
29
+ return unless process_request?(env)
30
+
20
31
  env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
21
32
  yield(env.body) unless env.body.respond_to?(:to_str)
22
33
  end
23
- end
24
34
 
25
- def process_request?(env)
26
- type = request_type(env)
27
- env.body and (type.empty? or type == self.class.mime_type)
28
- end
35
+ # @param env [Faraday::Env]
36
+ #
37
+ # @return [Boolean] True if the request has a body and its Content-Type is
38
+ # urlencoded.
39
+ def process_request?(env)
40
+ type = request_type(env)
41
+ env.body && (type.empty? || (type == self.class.mime_type))
42
+ end
29
43
 
30
- def request_type(env)
31
- type = env.request_headers[CONTENT_TYPE].to_s
32
- type = type.split(';', 2).first if type.index(';')
33
- type
44
+ # @param env [Faraday::Env]
45
+ #
46
+ # @return [String]
47
+ def request_type(env)
48
+ type = env.request_headers[CONTENT_TYPE].to_s
49
+ type = type.split(';', 2).first if type.index(';')
50
+ type
51
+ end
34
52
  end
35
53
  end
36
54
  end
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
- # Used to setup urls, params, headers, and the request body in a sane manner.
4
+ # Used to setup URLs, params, headers, and the request body in a sane manner.
3
5
  #
6
+ # @example
4
7
  # @connection.post do |req|
5
8
  # req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
6
9
  # req.headers['b'] = '2' # Header
@@ -9,27 +12,53 @@ module Faraday
9
12
  # req.body = 'abc'
10
13
  # end
11
14
  #
15
+ # @!attribute method
16
+ # @return [Symbol] the HTTP method of the Request
17
+ # @!attribute path
18
+ # @return [URI, String] the path
19
+ # @!attribute params
20
+ # @return [Hash] query parameters
21
+ # @!attribute headers
22
+ # @return [Faraday::Utils::Headers] headers
23
+ # @!attribute body
24
+ # @return [Hash] body
25
+ # @!attribute options
26
+ # @return [RequestOptions] options
27
+ #
28
+ # rubocop:disable Style/StructInheritance
12
29
  class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
13
- extend MiddlewareRegistry
30
+ # rubocop:enable Style/StructInheritance
14
31
 
15
- alias_method :http_method, :method
32
+ extend MiddlewareRegistry
16
33
 
17
- register_middleware File.expand_path('../request', __FILE__),
18
- :url_encoded => [:UrlEncoded, 'url_encoded'],
19
- :multipart => [:Multipart, 'multipart'],
20
- :retry => [:Retry, 'retry'],
21
- :authorization => [:Authorization, 'authorization'],
22
- :basic_auth => [:BasicAuthentication, 'basic_authentication'],
23
- :token_auth => [:TokenAuthentication, 'token_authentication'],
24
- :instrumentation => [:Instrumentation, 'instrumentation']
34
+ register_middleware File.expand_path('request', __dir__),
35
+ url_encoded: [:UrlEncoded, 'url_encoded'],
36
+ multipart: [:Multipart, 'multipart'],
37
+ retry: [:Retry, 'retry'],
38
+ authorization: [:Authorization, 'authorization'],
39
+ basic_auth: [
40
+ :BasicAuthentication,
41
+ 'basic_authentication'
42
+ ],
43
+ token_auth: [
44
+ :TokenAuthentication,
45
+ 'token_authentication'
46
+ ],
47
+ instrumentation: [:Instrumentation, 'instrumentation']
25
48
 
49
+ # @param request_method [String]
50
+ # @yield [request] for block customization, if block given
51
+ # @yieldparam request [Request]
52
+ # @return [Request]
26
53
  def self.create(request_method)
27
54
  new(request_method).tap do |request|
28
55
  yield(request) if block_given?
29
56
  end
30
57
  end
31
58
 
32
- # Public: Replace params, preserving the existing hash type
59
+ # Replace params, preserving the existing hash type.
60
+ #
61
+ # @param hash [Hash] new params
33
62
  def params=(hash)
34
63
  if params
35
64
  params.replace hash
@@ -38,7 +67,9 @@ module Faraday
38
67
  end
39
68
  end
40
69
 
41
- # Public: Replace request headers, preserving the existing hash type
70
+ # Replace request headers, preserving the existing hash type.
71
+ #
72
+ # @param hash [Hash] new headers
42
73
  def headers=(hash)
43
74
  if headers
44
75
  headers.replace hash
@@ -47,9 +78,14 @@ module Faraday
47
78
  end
48
79
  end
49
80
 
81
+ # Update path and params.
82
+ #
83
+ # @param path [URI, String]
84
+ # @param params [Hash, nil]
85
+ # @return [void]
50
86
  def url(path, params = nil)
51
87
  if path.respond_to? :query
52
- if query = path.query
88
+ if (query = path.query)
53
89
  path = path.dup
54
90
  path.query = nil
55
91
  end
@@ -63,25 +99,35 @@ module Faraday
63
99
  self.params.update(params) if params
64
100
  end
65
101
 
102
+ # @param key [Object] key to look up in headers
103
+ # @return [Object] value of the given header name
66
104
  def [](key)
67
105
  headers[key]
68
106
  end
69
107
 
108
+ # @param key [Object] key of header to write
109
+ # @param value [Object] value of header
70
110
  def []=(key, value)
71
111
  headers[key] = value
72
112
  end
73
113
 
114
+ # Marshal serialization support.
115
+ #
116
+ # @return [Hash] the hash ready to be serialized in Marshal.
74
117
  def marshal_dump
75
118
  {
76
- :method => method,
77
- :body => body,
78
- :headers => headers,
79
- :path => path,
80
- :params => params,
81
- :options => options
119
+ method: method,
120
+ body: body,
121
+ headers: headers,
122
+ path: path,
123
+ params: params,
124
+ options: options
82
125
  }
83
126
  end
84
127
 
128
+ # Marshal serialization support.
129
+ # Restores the instance variables according to the +serialised+.
130
+ # @param serialised [Hash] the serialised object.
85
131
  def marshal_load(serialised)
86
132
  self.method = serialised[:method]
87
133
  self.body = serialised[:body]
@@ -91,26 +137,10 @@ module Faraday
91
137
  self.options = serialised[:options]
92
138
  end
93
139
 
94
- # ENV Keys
95
- # :method - a symbolized request method (:get, :post)
96
- # :body - the request body that will eventually be converted to a string.
97
- # :url - URI instance for the current request.
98
- # :status - HTTP response status code
99
- # :request_headers - hash of HTTP Headers to be sent to the server
100
- # :response_headers - Hash of HTTP headers from the server
101
- # :parallel_manager - sent if the connection is in parallel mode
102
- # :request - Hash of options for configuring the request.
103
- # :timeout - open/read timeout Integer in seconds
104
- # :open_timeout - read timeout Integer in seconds
105
- # :proxy - Hash of proxy options
106
- # :uri - Proxy Server URI
107
- # :user - Proxy server username
108
- # :password - Proxy server password
109
- # :ssl - Hash of options for configuring SSL requests.
140
+ # @return [Env] the Env for this Request
110
141
  def to_env(connection)
111
142
  Env.new(method, body, connection.build_exclusive_url(path, params),
112
- options, headers, connection.ssl, connection.parallel_manager)
143
+ options, headers, connection.ssl, connection.parallel_manager)
113
144
  end
114
145
  end
115
146
  end
116
-
@@ -1,80 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'forwardable'
4
+ require 'faraday/logging/formatter'
2
5
 
3
6
  module Faraday
4
- class Response::Logger < Response::Middleware
5
- extend Forwardable
6
-
7
- DEFAULT_OPTIONS = { :headers => true, :bodies => false }
8
-
9
- def initialize(app, logger = nil, options = {})
10
- super(app)
11
- @logger = logger || begin
12
- require 'logger'
13
- ::Logger.new($stdout)
14
- end
15
- @filter = []
16
- @options = DEFAULT_OPTIONS.merge(options)
17
- yield self if block_given?
18
- end
19
-
20
- def_delegators :@logger, :debug, :info, :warn, :error, :fatal
21
-
22
- def call(env)
23
- info('request') { "#{env.method.upcase} #{apply_filters(env.url.to_s)}" }
24
- debug('request') { apply_filters( dump_headers env.request_headers ) } if log_headers?(:request)
25
- debug('request') { apply_filters( dump_body(env[:body]) ) } if env[:body] && log_body?(:request)
26
- super
27
- end
28
-
29
- def on_complete(env)
30
- info('response') { "Status #{env.status.to_s}" }
31
- debug('response') { apply_filters( dump_headers env.response_headers ) } if log_headers?(:response)
32
- debug('response') { apply_filters( dump_body env[:body] ) } if env[:body] && log_body?(:response)
33
- end
34
-
35
- def filter(filter_word, filter_replacement)
36
- @filter.push([ filter_word, filter_replacement ])
37
- end
38
-
39
- private
40
-
41
- def dump_headers(headers)
42
- headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
43
- end
44
-
45
- def dump_body(body)
46
- if body.respond_to?(:to_str)
47
- body.to_str
48
- else
49
- pretty_inspect(body)
50
- end
51
- end
52
-
53
- def pretty_inspect(body)
54
- require 'pp' unless body.respond_to?(:pretty_inspect)
55
- body.pretty_inspect
56
- end
57
-
58
- def log_headers?(type)
59
- case @options[:headers]
60
- when Hash then @options[:headers][type]
61
- else @options[:headers]
7
+ class Response
8
+ # Logger is a middleware that logs internal events in the HTTP request
9
+ # lifecycle to a given Logger object. By default, this logs to STDOUT. See
10
+ # Faraday::Logging::Formatter to see specifically what is logged.
11
+ class Logger < Middleware
12
+ def initialize(app, logger = nil, options = {})
13
+ super(app)
14
+ logger ||= begin
15
+ require 'logger'
16
+ ::Logger.new($stdout)
17
+ end
18
+ formatter_class = options.delete(:formatter) || Logging::Formatter
19
+ @formatter = formatter_class.new(logger: logger, options: options)
20
+ yield @formatter if block_given?
62
21
  end
63
- end
64
22
 
65
- def log_body?(type)
66
- case @options[:bodies]
67
- when Hash then @options[:bodies][type]
68
- else @options[:bodies]
23
+ def call(env)
24
+ @formatter.request(env)
25
+ super
69
26
  end
70
- end
71
27
 
72
- def apply_filters(output)
73
- @filter.each do |pattern, replacement|
74
- output = output.to_s.gsub(pattern, replacement)
28
+ def on_complete(env)
29
+ @formatter.response(env)
75
30
  end
76
- output
77
31
  end
78
-
79
32
  end
80
33
  end
@@ -1,25 +1,43 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
- class Response::RaiseError < Response::Middleware
3
- ClientErrorStatuses = 400...600
4
+ class Response
5
+ # RaiseError is a Faraday middleware that raises exceptions on common HTTP
6
+ # client or server error responses.
7
+ class RaiseError < Middleware
8
+ # rubocop:disable Naming/ConstantName
9
+ ClientErrorStatuses = (400...500).freeze
10
+ ServerErrorStatuses = (500...600).freeze
11
+ # rubocop:enable Naming/ConstantName
4
12
 
5
- def on_complete(env)
6
- case env[:status]
7
- when 404
8
- raise Faraday::ResourceNotFound, response_values(env)
9
- when 407
10
- # mimic the behavior that we get with proxy requests with HTTPS
11
- raise Faraday::ConnectionFailed.new(
12
- %{407 "Proxy Authentication Required "},
13
- response_values(env))
14
- when ClientErrorStatuses
15
- raise Faraday::ClientError, response_values(env)
16
- when nil
17
- raise Faraday::NilStatusError, response_values(env)
13
+ def on_complete(env)
14
+ case env[:status]
15
+ when 400
16
+ raise Faraday::BadRequestError, response_values(env)
17
+ when 401
18
+ raise Faraday::UnauthorizedError, response_values(env)
19
+ when 403
20
+ raise Faraday::ForbiddenError, response_values(env)
21
+ when 404
22
+ raise Faraday::ResourceNotFound, response_values(env)
23
+ when 407
24
+ # mimic the behavior that we get with proxy requests with HTTPS
25
+ msg = %(407 "Proxy Authentication Required")
26
+ raise Faraday::ProxyAuthError.new(msg, response_values(env))
27
+ when 409
28
+ raise Faraday::ConflictError, response_values(env)
29
+ when 422
30
+ raise Faraday::UnprocessableEntityError, response_values(env)
31
+ when ClientErrorStatuses
32
+ raise Faraday::ClientError, response_values(env)
33
+ when ServerErrorStatuses
34
+ raise Faraday::ServerError, response_values(env)
35
+ end
18
36
  end
19
- end
20
37
 
21
- def response_values(env)
22
- {:status => env.status, :headers => env.response_headers, :body => env.body}
38
+ def response_values(env)
39
+ { status: env.status, headers: env.response_headers, body: env.body }
40
+ end
23
41
  end
24
42
  end
25
43
  end
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'forwardable'
2
4
 
3
5
  module Faraday
6
+ # Response represents an HTTP response from making an HTTP request.
4
7
  class Response
5
8
  # Used for simple response middleware.
6
9
  class Middleware < Faraday::Middleware
@@ -20,9 +23,9 @@ module Faraday
20
23
  extend Forwardable
21
24
  extend MiddlewareRegistry
22
25
 
23
- register_middleware File.expand_path('../response', __FILE__),
24
- :raise_error => [:RaiseError, 'raise_error'],
25
- :logger => [:Logger, 'logger']
26
+ register_middleware File.expand_path('response', __dir__),
27
+ raise_error: [:RaiseError, 'raise_error'],
28
+ logger: [:Logger, 'logger']
26
29
 
27
30
  def initialize(env = nil)
28
31
  @env = Env.from(env) if env
@@ -31,8 +34,6 @@ module Faraday
31
34
 
32
35
  attr_reader :env
33
36
 
34
- def_delegators :env, :to_hash
35
-
36
37
  def status
37
38
  finished? ? env.status : nil
38
39
  end
@@ -54,32 +55,37 @@ module Faraday
54
55
  !!env
55
56
  end
56
57
 
57
- def on_complete(&block)
58
+ def on_complete
58
59
  if !finished?
59
- @on_complete_callbacks << block
60
+ @on_complete_callbacks << Proc.new
60
61
  else
61
62
  yield(env)
62
63
  end
63
- return self
64
+ self
64
65
  end
65
66
 
66
67
  def finish(env)
67
- raise "response already finished" if finished?
68
+ raise 'response already finished' if finished?
69
+
68
70
  @env = env.is_a?(Env) ? env : Env.from(env)
69
71
  @on_complete_callbacks.each { |callback| callback.call(@env) }
70
- return self
72
+ self
71
73
  end
72
74
 
73
75
  def success?
74
76
  finished? && env.success?
75
77
  end
76
78
 
79
+ def to_hash
80
+ {
81
+ status: env.status, body: env.body,
82
+ response_headers: env.response_headers
83
+ }
84
+ end
85
+
77
86
  # because @on_complete_callbacks cannot be marshalled
78
87
  def marshal_dump
79
- !finished? ? nil : {
80
- :status => @env.status, :body => @env.body,
81
- :response_headers => @env.response_headers
82
- }
88
+ finished? ? to_hash : nil
83
89
  end
84
90
 
85
91
  def marshal_load(env)
@@ -90,8 +96,9 @@ module Faraday
90
96
  # Useful for applying request params after restoring a marshalled Response.
91
97
  def apply_request(request_env)
92
98
  raise "response didn't finish yet" unless finished?
99
+
93
100
  @env = Env.from(request_env).update(@env)
94
- return self
101
+ self
95
102
  end
96
103
  end
97
104
  end
@@ -1,55 +1,61 @@
1
- begin
2
- require 'multipart/post'
3
- require 'stringio'
4
- rescue LoadError
5
- begin
6
- require 'composite_io'
7
- require 'parts'
8
- require 'stringio'
9
- rescue LoadError
10
- $stderr.puts "Install the multipart-post gem."
11
- raise
12
- end
13
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+
5
+ # multipart-post gem
6
+ require 'composite_io'
7
+ require 'parts'
14
8
 
15
9
  module Faraday
16
- # Similar but not compatible with ::CompositeReadIO provided by multipart-post.
10
+ # Similar to, but not compatible with CompositeReadIO provided by the
11
+ # multipart-post gem.
12
+ # https://github.com/nicksieger/multipart-post/blob/master/lib/composite_io.rb
17
13
  class CompositeReadIO
18
14
  def initialize(*parts)
19
15
  @parts = parts.flatten
20
- @ios = @parts.map { |part| part.to_io }
16
+ @ios = @parts.map(&:to_io)
21
17
  @index = 0
22
18
  end
23
19
 
20
+ # @return [Integer] sum of the lengths of all the parts
24
21
  def length
25
22
  @parts.inject(0) { |sum, part| sum + part.length }
26
23
  end
27
24
 
25
+ # Rewind each of the IOs and reset the index to 0.
26
+ #
27
+ # @return [void]
28
28
  def rewind
29
- @ios.each { |io| io.rewind }
29
+ @ios.each(&:rewind)
30
30
  @index = 0
31
31
  end
32
32
 
33
33
  # Read from IOs in order until `length` bytes have been received.
34
+ #
35
+ # @param length [Integer, nil]
36
+ # @param outbuf [String, nil]
34
37
  def read(length = nil, outbuf = nil)
35
38
  got_result = false
36
- outbuf = outbuf ? outbuf.replace("") : ""
39
+ outbuf = outbuf ? (+outbuf).replace('') : +''
37
40
 
38
- while io = current_io
39
- if result = io.read(length)
41
+ while (io = current_io)
42
+ if (result = io.read(length))
40
43
  got_result ||= !result.nil?
41
- result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
44
+ result.force_encoding('BINARY') if result.respond_to?(:force_encoding)
42
45
  outbuf << result
43
46
  length -= result.length if length
44
- break if length == 0
47
+ break if length&.zero?
45
48
  end
46
49
  advance_io
47
50
  end
48
- (!got_result && length) ? nil : outbuf
51
+ !got_result && length ? nil : outbuf
49
52
  end
50
53
 
54
+ # Close each of the IOs.
55
+ #
56
+ # @return [void]
51
57
  def close
52
- @ios.each { |io| io.close }
58
+ @ios.each(&:close)
53
59
  end
54
60
 
55
61
  def ensure_open_and_readable
@@ -67,11 +73,6 @@ module Faraday
67
73
  end
68
74
  end
69
75
 
70
- if defined?(::Multipart::Post::UploadIO)
71
- UploadIO = ::Multipart::Post::UploadIO
72
- Parts = ::Multipart::Post::Parts
73
- else
74
- UploadIO = ::UploadIO
75
- Parts = ::Parts
76
- end
76
+ UploadIO = ::UploadIO
77
+ Parts = ::Parts
77
78
  end