faraday 0.15.4 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +276 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +18 -344
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +65 -0
  7. data/examples/client_test.rb +79 -0
  8. data/lib/faraday/adapter/em_http.rb +144 -101
  9. data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
  10. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  11. data/lib/faraday/adapter/em_synchrony.rb +104 -60
  12. data/lib/faraday/adapter/excon.rb +98 -56
  13. data/lib/faraday/adapter/httpclient.rb +83 -59
  14. data/lib/faraday/adapter/net_http.rb +130 -63
  15. data/lib/faraday/adapter/net_http_persistent.rb +51 -28
  16. data/lib/faraday/adapter/patron.rb +80 -43
  17. data/lib/faraday/adapter/rack.rb +30 -13
  18. data/lib/faraday/adapter/test.rb +86 -53
  19. data/lib/faraday/adapter/typhoeus.rb +4 -1
  20. data/lib/faraday/adapter.rb +82 -22
  21. data/lib/faraday/adapter_registry.rb +30 -0
  22. data/lib/faraday/autoload.rb +47 -36
  23. data/lib/faraday/connection.rb +312 -182
  24. data/lib/faraday/dependency_loader.rb +37 -0
  25. data/lib/faraday/encoders/flat_params_encoder.rb +98 -0
  26. data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
  27. data/lib/faraday/error.rb +103 -37
  28. data/lib/faraday/file_part.rb +128 -0
  29. data/lib/faraday/logging/formatter.rb +105 -0
  30. data/lib/faraday/middleware.rb +12 -28
  31. data/lib/faraday/middleware_registry.rb +129 -0
  32. data/lib/faraday/options/connection_options.rb +22 -0
  33. data/lib/faraday/options/env.rb +181 -0
  34. data/lib/faraday/options/proxy_options.rb +28 -0
  35. data/lib/faraday/options/request_options.rb +22 -0
  36. data/lib/faraday/options/ssl_options.rb +59 -0
  37. data/lib/faraday/options.rb +35 -186
  38. data/lib/faraday/param_part.rb +53 -0
  39. data/lib/faraday/parameters.rb +4 -197
  40. data/lib/faraday/rack_builder.rb +67 -56
  41. data/lib/faraday/request/authorization.rb +44 -30
  42. data/lib/faraday/request/basic_authentication.rb +14 -7
  43. data/lib/faraday/request/instrumentation.rb +45 -27
  44. data/lib/faraday/request/multipart.rb +79 -48
  45. data/lib/faraday/request/retry.rb +198 -169
  46. data/lib/faraday/request/token_authentication.rb +15 -10
  47. data/lib/faraday/request/url_encoded.rb +43 -23
  48. data/lib/faraday/request.rb +68 -36
  49. data/lib/faraday/response/logger.rb +22 -69
  50. data/lib/faraday/response/raise_error.rb +38 -14
  51. data/lib/faraday/response.rb +27 -17
  52. data/lib/faraday/utils/headers.rb +139 -0
  53. data/lib/faraday/utils/params_hash.rb +61 -0
  54. data/lib/faraday/utils.rb +36 -245
  55. data/lib/faraday.rb +94 -176
  56. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  57. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  58. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  59. data/spec/faraday/adapter/excon_spec.rb +49 -0
  60. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  61. data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
  62. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  63. data/spec/faraday/adapter/patron_spec.rb +18 -0
  64. data/spec/faraday/adapter/rack_spec.rb +8 -0
  65. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  66. data/spec/faraday/adapter_registry_spec.rb +28 -0
  67. data/spec/faraday/adapter_spec.rb +55 -0
  68. data/spec/faraday/composite_read_io_spec.rb +80 -0
  69. data/spec/faraday/connection_spec.rb +691 -0
  70. data/spec/faraday/error_spec.rb +45 -0
  71. data/spec/faraday/middleware_spec.rb +26 -0
  72. data/spec/faraday/options/env_spec.rb +70 -0
  73. data/spec/faraday/options/options_spec.rb +297 -0
  74. data/spec/faraday/options/proxy_options_spec.rb +37 -0
  75. data/spec/faraday/options/request_options_spec.rb +19 -0
  76. data/spec/faraday/params_encoders/flat_spec.rb +34 -0
  77. data/spec/faraday/params_encoders/nested_spec.rb +134 -0
  78. data/spec/faraday/rack_builder_spec.rb +196 -0
  79. data/spec/faraday/request/authorization_spec.rb +88 -0
  80. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  81. data/spec/faraday/request/multipart_spec.rb +274 -0
  82. data/spec/faraday/request/retry_spec.rb +242 -0
  83. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  84. data/spec/faraday/request_spec.rb +109 -0
  85. data/spec/faraday/response/logger_spec.rb +220 -0
  86. data/spec/faraday/response/middleware_spec.rb +68 -0
  87. data/spec/faraday/response/raise_error_spec.rb +106 -0
  88. data/spec/faraday/response_spec.rb +75 -0
  89. data/spec/faraday/utils/headers_spec.rb +82 -0
  90. data/spec/faraday/utils_spec.rb +56 -0
  91. data/spec/faraday_spec.rb +37 -0
  92. data/spec/spec_helper.rb +132 -0
  93. data/spec/support/disabling_stub.rb +14 -0
  94. data/spec/support/fake_safe_buffer.rb +15 -0
  95. data/spec/support/helper_methods.rb +133 -0
  96. data/spec/support/shared_examples/adapter.rb +104 -0
  97. data/spec/support/shared_examples/params_encoder.rb +18 -0
  98. data/spec/support/shared_examples/request_method.rb +234 -0
  99. data/spec/support/streaming_response_checker.rb +35 -0
  100. data/spec/support/webmock_rack_app.rb +68 -0
  101. metadata +78 -9
  102. data/lib/faraday/upload_io.rb +0 -67
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Requires Ruby with rspec and faraday gems.
4
+ # rspec client_spec.rb
5
+
6
+ require 'faraday'
7
+ require 'json'
8
+
9
+ # Example API client
10
+ class Client
11
+ def initialize(conn)
12
+ @conn = conn
13
+ end
14
+
15
+ def sushi(jname)
16
+ res = @conn.get("/#{jname}")
17
+ data = JSON.parse(res.body)
18
+ data['name']
19
+ end
20
+ end
21
+
22
+ Rspec.describe Client do
23
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
24
+ let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
25
+ let(:client) { Client.new(conn) }
26
+
27
+ it 'parses name' do
28
+ stubs.get('/ebi') do |env|
29
+ # optional: you can inspect the Faraday::Env
30
+ expect(env.url.path).to eq('/ebi')
31
+ [
32
+ 200,
33
+ { 'Content-Type': 'application/javascript' },
34
+ '{"name": "shrimp"}'
35
+ ]
36
+ end
37
+
38
+ # uncomment to trigger stubs.verify_stubbed_calls failure
39
+ # stubs.get('/unused') { [404, {}, ''] }
40
+
41
+ expect(client.sushi('ebi')).to eq('shrimp')
42
+ stubs.verify_stubbed_calls
43
+ end
44
+
45
+ it 'handles 404' do
46
+ stubs.get('/ebi') do
47
+ [
48
+ 404,
49
+ { 'Content-Type': 'application/javascript' },
50
+ '{}'
51
+ ]
52
+ end
53
+ expect(client.sushi('ebi')).to be_nil
54
+ stubs.verify_stubbed_calls
55
+ end
56
+
57
+ it 'handles exception' do
58
+ stubs.get('/ebi') do
59
+ raise Faraday::ConnectionFailed, nil
60
+ end
61
+
62
+ expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
63
+ stubs.verify_stubbed_calls
64
+ end
65
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Requires Ruby with test-unit and faraday gems.
4
+ # ruby client_test.rb
5
+
6
+ require 'faraday'
7
+ require 'json'
8
+ require 'test/unit'
9
+
10
+ # Example API client
11
+ class Client
12
+ def initialize(conn)
13
+ @conn = conn
14
+ end
15
+
16
+ def sushi(jname)
17
+ res = @conn.get("/#{jname}")
18
+ data = JSON.parse(res.body)
19
+ data['name']
20
+ end
21
+ end
22
+
23
+ # Example API client test
24
+ class ClientTest < Test::Unit::TestCase
25
+ def test_sushi_name
26
+ stubs = Faraday::Adapter::Test::Stubs.new
27
+ stubs.get('/ebi') do |env|
28
+ # optional: you can inspect the Faraday::Env
29
+ assert_equal '/ebi', env.url.path
30
+ [
31
+ 200,
32
+ { 'Content-Type': 'application/javascript' },
33
+ '{"name": "shrimp"}'
34
+ ]
35
+ end
36
+
37
+ # uncomment to trigger stubs.verify_stubbed_calls failure
38
+ # stubs.get('/unused') { [404, {}, ''] }
39
+
40
+ cli = client(stubs)
41
+ assert_equal 'shrimp', cli.sushi('ebi')
42
+ stubs.verify_stubbed_calls
43
+ end
44
+
45
+ def test_sushi_404
46
+ stubs = Faraday::Adapter::Test::Stubs.new
47
+ stubs.get('/ebi') do
48
+ [
49
+ 404,
50
+ { 'Content-Type': 'application/javascript' },
51
+ '{}'
52
+ ]
53
+ end
54
+
55
+ cli = client(stubs)
56
+ assert_nil cli.sushi('ebi')
57
+ stubs.verify_stubbed_calls
58
+ end
59
+
60
+ def test_sushi_exception
61
+ stubs = Faraday::Adapter::Test::Stubs.new
62
+ stubs.get('/ebi') do
63
+ raise Faraday::ConnectionFailed, nil
64
+ end
65
+
66
+ cli = client(stubs)
67
+ assert_raise Faraday::ConnectionFailed do
68
+ cli.sushi('ebi')
69
+ end
70
+ stubs.verify_stubbed_calls
71
+ end
72
+
73
+ def client(stubs)
74
+ conn = Faraday.new do |builder|
75
+ builder.adapter :test, stubs
76
+ end
77
+ Client.new(conn)
78
+ end
79
+ end
@@ -1,10 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
4
  class Adapter
3
- # EventMachine adapter is useful for either asynchronous requests
4
- # when in EM reactor loop or for making parallel requests in
5
+ # EventMachine adapter. This adapter is useful for either asynchronous
6
+ # requests when in an EM reactor loop, or for making parallel requests in
5
7
  # synchronous code.
6
8
  class EMHttp < Faraday::Adapter
9
+ # Options is a module containing helpers to convert the Faraday env object
10
+ # into options hashes for EMHTTP method calls.
7
11
  module Options
12
+ # @return [Hash]
8
13
  def connection_config(env)
9
14
  options = {}
10
15
  configure_proxy(options, env)
@@ -16,10 +21,10 @@ module Faraday
16
21
 
17
22
  def request_config(env)
18
23
  options = {
19
- :body => read_body(env),
20
- :head => env[:request_headers],
21
- # :keepalive => true,
22
- # :file => 'path/to/file', # stream data off disk
24
+ body: read_body(env),
25
+ head: env[:request_headers]
26
+ # keepalive: true,
27
+ # file: 'path/to/file', # stream data off disk
23
28
  }
24
29
  configure_compression(options, env)
25
30
  options
@@ -30,44 +35,52 @@ module Faraday
30
35
  body.respond_to?(:read) ? body.read : body
31
36
  end
32
37
 
38
+ # Reads out proxy settings from env into options
33
39
  def configure_proxy(options, env)
34
- if proxy = request_options(env)[:proxy]
35
- options[:proxy] = {
36
- :host => proxy[:uri].host,
37
- :port => proxy[:uri].port,
38
- :authorization => [proxy[:user], proxy[:password]]
39
- }
40
- end
40
+ proxy = request_options(env)[:proxy]
41
+ return unless proxy
42
+
43
+ options[:proxy] = {
44
+ host: proxy[:uri].host,
45
+ port: proxy[:uri].port,
46
+ authorization: [proxy[:user], proxy[:password]]
47
+ }
41
48
  end
42
49
 
50
+ # Reads out host and port settings from env into options
43
51
  def configure_socket(options, env)
44
- if bind = request_options(env)[:bind]
45
- options[:bind] = {
46
- :host => bind[:host],
47
- :port => bind[:port]
48
- }
49
- end
52
+ bind = request_options(env)[:bind]
53
+ return unless bind
54
+
55
+ options[:bind] = {
56
+ host: bind[:host],
57
+ port: bind[:port]
58
+ }
50
59
  end
51
60
 
61
+ # Reads out SSL certificate settings from env into options
52
62
  def configure_ssl(options, env)
53
- if env[:url].scheme == 'https' && env[:ssl]
54
- options[:ssl] = {
55
- :cert_chain_file => env[:ssl][:ca_file],
56
- :verify_peer => env[:ssl].fetch(:verify, true)
57
- }
58
- end
63
+ return unless env[:url].scheme == 'https' && env[:ssl]
64
+
65
+ options[:ssl] = {
66
+ cert_chain_file: env[:ssl][:ca_file],
67
+ verify_peer: env[:ssl].fetch(:verify, true)
68
+ }
59
69
  end
60
70
 
71
+ # Reads out timeout settings from env into options
61
72
  def configure_timeout(options, env)
62
- timeout, open_timeout = request_options(env).values_at(:timeout, :open_timeout)
63
- options[:connect_timeout] = options[:inactivity_timeout] = timeout
64
- options[:connect_timeout] = open_timeout if open_timeout
73
+ req = request_options(env)
74
+ options[:inactivity_timeout] = request_timeout(:read, req)
75
+ options[:connect_timeout] = request_timeout(:open, req)
65
76
  end
66
77
 
78
+ # Reads out compression header settings from env into options
67
79
  def configure_compression(options, env)
68
- if env[:method] == :get and not options[:head].key? 'accept-encoding'
69
- options[:head]['accept-encoding'] = 'gzip, compressed'
70
- end
80
+ return unless (env[:method] == :get) &&
81
+ !options[:head].key?('accept-encoding')
82
+
83
+ options[:head]['accept-encoding'] = 'gzip, compressed'
71
84
  end
72
85
 
73
86
  def request_options(env)
@@ -81,7 +94,8 @@ module Faraday
81
94
 
82
95
  self.supports_parallel = true
83
96
 
84
- def self.setup_parallel_manager(options = nil)
97
+ # @return [Manager]
98
+ def self.setup_parallel_manager(_options = nil)
85
99
  Manager.new
86
100
  end
87
101
 
@@ -94,95 +108,114 @@ module Faraday
94
108
  def perform_request(env)
95
109
  if parallel?(env)
96
110
  manager = env[:parallel_manager]
97
- manager.add {
98
- perform_single_request(env).
99
- callback { env[:response].finish(env) }
100
- }
101
- else
102
- unless EventMachine.reactor_running?
103
- error = nil
104
- # start EM, block until request is completed
105
- EventMachine.run do
106
- perform_single_request(env).
107
- callback { EventMachine.stop }.
108
- errback { |client|
109
- error = error_message(client)
110
- EventMachine.stop
111
- }
111
+ manager.add do
112
+ perform_single_request(env)
113
+ .callback { env[:response].finish(env) }
114
+ end
115
+ elsif EventMachine.reactor_running?
116
+ # EM is running: instruct upstream that this is an async request
117
+ env[:parallel_manager] = true
118
+ perform_single_request(env)
119
+ .callback { env[:response].finish(env) }
120
+ .errback do
121
+ # TODO: no way to communicate the error in async mode
122
+ raise NotImplementedError
112
123
  end
113
- raise_error(error) if error
114
- else
115
- # EM is running: instruct upstream that this is an async request
116
- env[:parallel_manager] = true
117
- perform_single_request(env).
118
- callback { env[:response].finish(env) }.
119
- errback {
120
- # TODO: no way to communicate the error in async mode
121
- raise NotImplementedError
122
- }
124
+ else
125
+ error = nil
126
+ # start EM, block until request is completed
127
+ EventMachine.run do
128
+ perform_single_request(env)
129
+ .callback { EventMachine.stop }
130
+ .errback do |client|
131
+ error = error_message(client)
132
+ EventMachine.stop
133
+ end
123
134
  end
135
+ raise_error(error) if error
124
136
  end
125
- rescue EventMachine::Connectify::CONNECTError => err
126
- if err.message.include?("Proxy Authentication Required")
127
- raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
128
- else
129
- raise Error::ConnectionFailed, err
137
+ rescue EventMachine::Connectify::CONNECTError => e
138
+ if e.message.include?('Proxy Authentication Required')
139
+ raise Faraday::ConnectionFailed,
140
+ %(407 "Proxy Authentication Required ")
130
141
  end
131
- rescue => err
132
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
133
- raise Faraday::SSLError, err
134
- else
135
- raise
142
+
143
+ raise Faraday::ConnectionFailed, e
144
+ rescue StandardError => e
145
+ if defined?(::OpenSSL::SSL::SSLError) && \
146
+ e.is_a?(::OpenSSL::SSL::SSLError)
147
+ raise Faraday::SSLError, e
136
148
  end
149
+
150
+ raise
137
151
  end
138
152
 
139
153
  # TODO: reuse the connection to support pipelining
140
154
  def perform_single_request(env)
141
155
  req = create_request(env)
142
- req.setup_request(env[:method], request_config(env)).callback { |client|
156
+ req = req.setup_request(env[:method], request_config(env))
157
+ req.callback do |client|
158
+ if env[:request].stream_response?
159
+ warn "Streaming downloads for #{self.class.name} " \
160
+ 'are not yet implemented.'
161
+ env[:request].on_data.call(
162
+ client.response,
163
+ client.response.bytesize
164
+ )
165
+ end
143
166
  status = client.response_header.status
144
167
  reason = client.response_header.http_reason
145
- save_response(env, status, client.response, nil, reason) do |resp_headers|
168
+ save_response(env, status, client.response, nil, reason) do |headers|
146
169
  client.response_header.each do |name, value|
147
- resp_headers[name.to_sym] = value
170
+ headers[name.to_sym] = value
148
171
  end
149
172
  end
150
- }
173
+ end
151
174
  end
152
175
 
153
176
  def create_request(env)
154
- EventMachine::HttpRequest.new(env[:url], connection_config(env).merge(@connection_options))
177
+ EventMachine::HttpRequest.new(
178
+ env[:url], connection_config(env).merge(@connection_options)
179
+ )
155
180
  end
156
181
 
157
182
  def error_message(client)
158
- client.error or "request failed"
183
+ client.error || 'request failed'
159
184
  end
160
185
 
161
186
  def raise_error(msg)
162
- errklass = Faraday::Error::ClientError
163
- if msg == Errno::ETIMEDOUT
164
- errklass = Faraday::Error::TimeoutError
165
- msg = "request timed out"
187
+ error_class = Faraday::ClientError
188
+ if timeout_message?(msg)
189
+ error_class = Faraday::TimeoutError
190
+ msg = 'request timed out'
166
191
  elsif msg == Errno::ECONNREFUSED
167
- errklass = Faraday::Error::ConnectionFailed
168
- msg = "connection refused"
169
- elsif msg == "connection closed by server"
170
- errklass = Faraday::Error::ConnectionFailed
192
+ error_class = Faraday::ConnectionFailed
193
+ msg = 'connection refused'
194
+ elsif msg == 'connection closed by server'
195
+ error_class = Faraday::ConnectionFailed
171
196
  end
172
- raise errklass, msg
197
+ raise error_class, msg
173
198
  end
174
199
 
200
+ def timeout_message?(msg)
201
+ msg == Errno::ETIMEDOUT ||
202
+ (msg.is_a?(String) && msg.include?('timeout error'))
203
+ end
204
+
205
+ # @return [Boolean]
175
206
  def parallel?(env)
176
207
  !!env[:parallel_manager]
177
208
  end
178
209
 
179
- # The parallel manager is designed to start an EventMachine loop
210
+ # This parallel manager is designed to start an EventMachine loop
180
211
  # and block until all registered requests have been completed.
181
212
  class Manager
213
+ # @see reset
182
214
  def initialize
183
215
  reset
184
216
  end
185
217
 
218
+ # Re-initializes instance variables
186
219
  def reset
187
220
  @registered_procs = []
188
221
  @num_registered = 0
@@ -191,27 +224,30 @@ module Faraday
191
224
  @running = false
192
225
  end
193
226
 
194
- def running?() @running end
227
+ # @return [Boolean]
228
+ def running?
229
+ @running
230
+ end
195
231
 
196
- def add
232
+ def add(&block)
197
233
  if running?
198
234
  perform_request { yield }
199
235
  else
200
- @registered_procs << Proc.new
236
+ @registered_procs << block
201
237
  end
202
238
  @num_registered += 1
203
239
  end
204
240
 
205
241
  def run
206
- if @num_registered > 0
242
+ if @num_registered.positive?
207
243
  @running = true
208
244
  EventMachine.run do
209
245
  @registered_procs.each do |proc|
210
246
  perform_request(&proc)
211
247
  end
212
248
  end
213
- if @errors.size > 0
214
- raise Faraday::Error::ClientError, @errors.first || "connection failed"
249
+ unless @errors.empty?
250
+ raise Faraday::ClientError, @errors.first || 'connection failed'
215
251
  end
216
252
  end
217
253
  ensure
@@ -220,24 +256,31 @@ module Faraday
220
256
 
221
257
  def perform_request
222
258
  client = yield
223
- client.callback { @num_succeeded += 1; check_finished }
224
- client.errback { @errors << client.error; check_finished }
259
+ client.callback do
260
+ @num_succeeded += 1
261
+ check_finished
262
+ end
263
+ client.errback do
264
+ @errors << client.error
265
+ check_finished
266
+ end
225
267
  end
226
268
 
227
269
  def check_finished
228
- if @num_succeeded + @errors.size == @num_registered
229
- EventMachine.stop
230
- end
270
+ EventMachine.stop if @num_succeeded + @errors.size == @num_registered
231
271
  end
232
272
  end
233
273
  end
234
274
  end
235
275
  end
236
276
 
237
- begin
238
- require 'openssl'
239
- rescue LoadError
240
- warn "Warning: no such file to load -- openssl. Make sure it is installed if you want HTTPS support"
241
- else
242
- require 'faraday/adapter/em_http_ssl_patch'
243
- end if Faraday::Adapter::EMHttp.loaded?
277
+ if Faraday::Adapter::EMHttp.loaded?
278
+ begin
279
+ require 'openssl'
280
+ rescue LoadError
281
+ warn 'Warning: no such file to load -- openssl. ' \
282
+ 'Make sure it is installed if you want HTTPS support'
283
+ else
284
+ require 'faraday/adapter/em_http_ssl_patch'
285
+ end
286
+ end
@@ -1,43 +1,49 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'openssl'
2
4
  require 'em-http'
3
5
 
6
+ # EventMachine patch to make SSL work.
4
7
  module EmHttpSslPatch
5
8
  def ssl_verify_peer(cert_string)
6
- cert = nil
7
9
  begin
8
- cert = OpenSSL::X509::Certificate.new(cert_string)
10
+ @last_seen_cert = OpenSSL::X509::Certificate.new(cert_string)
9
11
  rescue OpenSSL::X509::CertificateError
10
12
  return false
11
13
  end
12
14
 
13
- @last_seen_cert = cert
14
-
15
- if certificate_store.verify(@last_seen_cert)
16
- begin
17
- certificate_store.add_cert(@last_seen_cert)
18
- rescue OpenSSL::X509::StoreError => e
19
- raise e unless e.message == 'cert already in hash table'
20
- end
21
- true
22
- else
23
- raise OpenSSL::SSL::SSLError.new(%(unable to verify the server certificate for "#{host}"))
15
+ unless certificate_store.verify(@last_seen_cert)
16
+ raise OpenSSL::SSL::SSLError,
17
+ %(unable to verify the server certificate for "#{host}")
18
+ end
19
+
20
+ begin
21
+ certificate_store.add_cert(@last_seen_cert)
22
+ rescue OpenSSL::X509::StoreError => e
23
+ raise e unless e.message == 'cert already in hash table'
24
24
  end
25
+ true
25
26
  end
26
27
 
27
28
  def ssl_handshake_completed
28
29
  return true unless verify_peer?
29
30
 
30
- unless OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
31
- raise OpenSSL::SSL::SSLError.new(%(host "#{host}" does not match the server certificate))
32
- else
33
- true
31
+ unless verified_cert_identity?
32
+ raise OpenSSL::SSL::SSLError,
33
+ %(host "#{host}" does not match the server certificate)
34
34
  end
35
+
36
+ true
35
37
  end
36
38
 
37
39
  def verify_peer?
38
40
  parent.connopts.tls[:verify_peer]
39
41
  end
40
42
 
43
+ def verified_cert_identity?
44
+ OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
45
+ end
46
+
41
47
  def host
42
48
  parent.uri.host
43
49
  end
@@ -53,4 +59,4 @@ module EmHttpSslPatch
53
59
  end
54
60
  end
55
61
 
56
- EventMachine::HttpStubConnection.send(:include, EmHttpSslPatch)
62
+ EventMachine::HttpStubConnection.include(EmHttpSslPatch)
@@ -1,16 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
4
  class Adapter
3
5
  class EMSynchrony < Faraday::Adapter
6
+ # A parallel manager for EMSynchrony.
4
7
  class ParallelManager
5
-
6
- # Add requests to queue. The `request` argument should be a
7
- # `EM::HttpRequest` object.
8
+ # Add requests to queue.
9
+ #
10
+ # @param request [EM::HttpRequest]
11
+ # @param method [Symbol, String] HTTP method
12
+ # @param args [Array] the rest of the positional arguments
8
13
  def add(request, method, *args, &block)
9
14
  queue << {
10
- :request => request,
11
- :method => method,
12
- :args => args,
13
- :block => block
15
+ request: request,
16
+ method: method,
17
+ args: args,
18
+ block: block
14
19
  }
15
20
  end
16
21
 
@@ -19,19 +24,18 @@ module Faraday
19
24
  def run
20
25
  result = nil
21
26
  if !EM.reactor_running?
22
- EM.run {
27
+ EM.run do
23
28
  Fiber.new do
24
29
  result = perform
25
30
  EM.stop
26
31
  end.resume
27
- }
32
+ end
28
33
  else
29
34
  result = perform
30
35
  end
31
36
  result
32
37
  end
33
38
 
34
-
35
39
  private
36
40
 
37
41
  # The request queue.
@@ -59,8 +63,7 @@ module Faraday
59
63
  # Block fiber until all requests have returned.
60
64
  multi.perform
61
65
  end
62
-
63
- end # ParallelManager
64
- end # EMSynchrony
65
- end # Adapter
66
- end # Faraday
66
+ end
67
+ end
68
+ end
69
+ end