faraday 0.15.4 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +18 -344
  4. data/lib/faraday.rb +93 -175
  5. data/lib/faraday/adapter.rb +36 -22
  6. data/lib/faraday/adapter/em_http.rb +142 -99
  7. data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
  8. data/lib/faraday/adapter/em_synchrony.rb +104 -60
  9. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  10. data/lib/faraday/adapter/excon.rb +100 -55
  11. data/lib/faraday/adapter/httpclient.rb +61 -39
  12. data/lib/faraday/adapter/net_http.rb +104 -51
  13. data/lib/faraday/adapter/net_http_persistent.rb +48 -27
  14. data/lib/faraday/adapter/patron.rb +54 -35
  15. data/lib/faraday/adapter/rack.rb +28 -12
  16. data/lib/faraday/adapter/test.rb +86 -53
  17. data/lib/faraday/adapter/typhoeus.rb +4 -1
  18. data/lib/faraday/adapter_registry.rb +28 -0
  19. data/lib/faraday/autoload.rb +47 -36
  20. data/lib/faraday/connection.rb +321 -179
  21. data/lib/faraday/dependency_loader.rb +37 -0
  22. data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
  23. data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
  24. data/lib/faraday/error.rb +67 -33
  25. data/lib/faraday/file_part.rb +128 -0
  26. data/lib/faraday/logging/formatter.rb +92 -0
  27. data/lib/faraday/middleware.rb +4 -28
  28. data/lib/faraday/middleware_registry.rb +129 -0
  29. data/lib/faraday/options.rb +35 -186
  30. data/lib/faraday/options/connection_options.rb +22 -0
  31. data/lib/faraday/options/env.rb +181 -0
  32. data/lib/faraday/options/proxy_options.rb +28 -0
  33. data/lib/faraday/options/request_options.rb +21 -0
  34. data/lib/faraday/options/ssl_options.rb +59 -0
  35. data/lib/faraday/param_part.rb +53 -0
  36. data/lib/faraday/parameters.rb +4 -197
  37. data/lib/faraday/rack_builder.rb +67 -56
  38. data/lib/faraday/request.rb +68 -36
  39. data/lib/faraday/request/authorization.rb +42 -30
  40. data/lib/faraday/request/basic_authentication.rb +14 -7
  41. data/lib/faraday/request/instrumentation.rb +45 -27
  42. data/lib/faraday/request/multipart.rb +79 -48
  43. data/lib/faraday/request/retry.rb +198 -169
  44. data/lib/faraday/request/token_authentication.rb +15 -10
  45. data/lib/faraday/request/url_encoded.rb +41 -23
  46. data/lib/faraday/response.rb +23 -16
  47. data/lib/faraday/response/logger.rb +22 -69
  48. data/lib/faraday/response/raise_error.rb +36 -14
  49. data/lib/faraday/utils.rb +28 -245
  50. data/lib/faraday/utils/headers.rb +139 -0
  51. data/lib/faraday/utils/params_hash.rb +61 -0
  52. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  53. metadata +21 -5
  54. data/lib/faraday/upload_io.rb +0 -67
@@ -1,8 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
4
  class Adapter
5
+ # HTTPClient adapter.
3
6
  class HTTPClient < Faraday::Adapter
4
7
  dependency 'httpclient'
5
8
 
9
+ # @return [HTTPClient]
6
10
  def client
7
11
  @client ||= ::HTTPClient.new
8
12
  end
@@ -13,65 +17,76 @@ module Faraday
13
17
  # enable compression
14
18
  client.transparent_gzip_decompression = true
15
19
 
16
- if req = env[:request]
17
- if proxy = req[:proxy]
20
+ if (req = env[:request])
21
+ if (proxy = req[:proxy])
18
22
  configure_proxy proxy
19
23
  end
20
24
 
21
- if bind = req[:bind]
25
+ if (bind = req[:bind])
22
26
  configure_socket bind
23
27
  end
24
28
 
25
29
  configure_timeouts req
26
30
  end
27
31
 
28
- if env[:url].scheme == 'https' && ssl = env[:ssl]
32
+ if env[:url].scheme == 'https' && (ssl = env[:ssl])
29
33
  configure_ssl ssl
30
34
  end
31
35
 
32
36
  configure_client
33
37
 
34
- # TODO Don't stream yet.
38
+ # TODO: Don't stream yet.
35
39
  # https://github.com/nahi/httpclient/pull/90
36
40
  env[:body] = env[:body].read if env[:body].respond_to? :read
37
41
 
38
42
  resp = client.request env[:method], env[:url],
39
- :body => env[:body],
40
- :header => env[:request_headers]
43
+ body: env[:body],
44
+ header: env[:request_headers]
41
45
 
46
+ if (req = env[:request]).stream_response?
47
+ warn "Streaming downloads for #{self.class.name} " \
48
+ 'are not yet implemented.'
49
+ req.on_data.call(resp.body, resp.body.bytesize)
50
+ end
42
51
  save_response env, resp.status, resp.body, resp.headers, resp.reason
43
52
 
44
53
  @app.call env
45
54
  rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
46
- raise Faraday::Error::TimeoutError, $!
47
- rescue ::HTTPClient::BadResponseError => err
48
- if err.message.include?('status 407')
49
- raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
50
- else
51
- raise Faraday::Error::ClientError, $!
55
+ raise Faraday::TimeoutError, $ERROR_INFO
56
+ rescue ::HTTPClient::BadResponseError => e
57
+ if e.message.include?('status 407')
58
+ raise Faraday::ConnectionFailed,
59
+ %(407 "Proxy Authentication Required ")
52
60
  end
53
- rescue Errno::ECONNREFUSED, IOError, SocketError
54
- raise Faraday::Error::ConnectionFailed, $!
55
- rescue => err
56
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
57
- raise Faraday::SSLError, err
58
- else
59
- raise
61
+
62
+ raise Faraday::ClientError, $ERROR_INFO
63
+ rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, IOError, SocketError
64
+ raise Faraday::ConnectionFailed, $ERROR_INFO
65
+ rescue StandardError => e
66
+ if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
67
+ raise Faraday::SSLError, e
60
68
  end
69
+
70
+ raise
61
71
  end
62
72
 
73
+ # @param bind [Hash]
63
74
  def configure_socket(bind)
64
75
  client.socket_local.host = bind[:host]
65
76
  client.socket_local.port = bind[:port]
66
77
  end
67
78
 
79
+ # Configure proxy URI and any user credentials.
80
+ #
81
+ # @param proxy [Hash]
68
82
  def configure_proxy(proxy)
69
83
  client.proxy = proxy[:uri]
70
- if proxy[:user] && proxy[:password]
71
- client.set_proxy_auth proxy[:user], proxy[:password]
72
- end
84
+ return unless proxy[:user] && proxy[:password]
85
+
86
+ client.set_proxy_auth(proxy[:user], proxy[:password])
73
87
  end
74
88
 
89
+ # @param ssl [Hash]
75
90
  def configure_ssl(ssl)
76
91
  ssl_config = client.ssl_config
77
92
  ssl_config.verify_mode = ssl_verify_mode(ssl)
@@ -84,40 +99,47 @@ module Faraday
84
99
  ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
85
100
  end
86
101
 
102
+ # @param req [Hash]
87
103
  def configure_timeouts(req)
88
- if req[:timeout]
89
- client.connect_timeout = req[:timeout]
90
- client.receive_timeout = req[:timeout]
91
- client.send_timeout = req[:timeout]
92
- end
104
+ configure_timeout(req) if req[:timeout]
105
+ configure_open_timeout(req) if req[:open_timeout]
106
+ end
93
107
 
94
- if req[:open_timeout]
95
- client.connect_timeout = req[:open_timeout]
96
- client.send_timeout = req[:open_timeout]
97
- end
108
+ def configure_timeout(req)
109
+ client.connect_timeout = req[:timeout]
110
+ client.receive_timeout = req[:timeout]
111
+ client.send_timeout = req[:timeout]
112
+ end
113
+
114
+ def configure_open_timeout(req)
115
+ client.connect_timeout = req[:open_timeout]
116
+ client.send_timeout = req[:open_timeout]
98
117
  end
99
118
 
100
119
  def configure_client
101
- @config_block.call(client) if @config_block
120
+ @config_block&.call(client)
102
121
  end
103
122
 
123
+ # @param ssl [Hash]
124
+ # @return [OpenSSL::X509::Store]
104
125
  def ssl_cert_store(ssl)
105
126
  return ssl[:cert_store] if ssl[:cert_store]
127
+
106
128
  # Memoize the cert store so that the same one is passed to
107
- # HTTPClient each time, to avoid resyncing SSL sesions when
129
+ # HTTPClient each time, to avoid resyncing SSL sessions when
108
130
  # it's changed
109
- @cert_store ||= begin
131
+ @ssl_cert_store ||= begin
110
132
  # Use the default cert store by default, i.e. system ca certs
111
- cert_store = OpenSSL::X509::Store.new
112
- cert_store.set_default_paths
113
- cert_store
133
+ OpenSSL::X509::Store.new.tap(&:set_default_paths)
114
134
  end
115
135
  end
116
136
 
137
+ # @param ssl [Hash]
117
138
  def ssl_verify_mode(ssl)
118
139
  ssl[:verify_mode] || begin
119
140
  if ssl.fetch(:verify, true)
120
- OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
141
+ OpenSSL::SSL::VERIFY_PEER |
142
+ OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
121
143
  else
122
144
  OpenSSL::SSL::VERIFY_NONE
123
145
  end
@@ -1,16 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'net/https'
3
5
  rescue LoadError
4
- warn "Warning: no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
6
+ warn 'Warning: no such file to load -- net/https. ' \
7
+ 'Make sure openssl is installed if you want ssl support'
5
8
  require 'net/http'
6
9
  end
7
10
  require 'zlib'
8
11
 
9
12
  module Faraday
10
13
  class Adapter
14
+ # Net::HTTP adapter.
11
15
  class NetHttp < Faraday::Adapter
12
- NET_HTTP_EXCEPTIONS = [
16
+ exceptions = [
13
17
  IOError,
18
+ Errno::EADDRNOTAVAIL,
14
19
  Errno::ECONNABORTED,
15
20
  Errno::ECONNREFUSED,
16
21
  Errno::ECONNRESET,
@@ -22,34 +27,40 @@ module Faraday
22
27
  Net::HTTPHeaderSyntaxError,
23
28
  Net::ProtocolError,
24
29
  SocketError,
25
- Zlib::GzipFile::Error,
30
+ Zlib::GzipFile::Error
26
31
  ]
27
32
 
28
- NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
29
- NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)
33
+ exceptions << OpenSSL::SSL::SSLError if defined?(OpenSSL)
34
+ exceptions << Net::OpenTimeout if defined?(Net::OpenTimeout)
35
+
36
+ NET_HTTP_EXCEPTIONS = exceptions.freeze
30
37
 
31
38
  def initialize(app = nil, opts = {}, &block)
32
- @cert_store = nil
39
+ @ssl_cert_store = nil
33
40
  super(app, opts, &block)
34
41
  end
35
42
 
36
43
  def call(env)
37
44
  super
38
45
  with_net_http_connection(env) do |http|
39
- configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
46
+ if (env[:url].scheme == 'https') && env[:ssl]
47
+ configure_ssl(http, env[:ssl])
48
+ end
40
49
  configure_request(http, env[:request])
41
50
 
42
51
  begin
43
52
  http_response = perform_request(http, env)
44
- rescue *NET_HTTP_EXCEPTIONS => err
45
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
46
- raise Faraday::SSLError, err
47
- else
48
- raise Error::ConnectionFailed, err
53
+ rescue *NET_HTTP_EXCEPTIONS => e
54
+ if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
55
+ raise Faraday::SSLError, e
49
56
  end
57
+
58
+ raise Faraday::ConnectionFailed, e
50
59
  end
51
60
 
52
- save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
61
+ save_response(env, http_response.code.to_i,
62
+ http_response.body || '', nil,
63
+ http_response.message) do |response_headers|
53
64
  http_response.each_header do |key, value|
54
65
  response_headers[key] = value
55
66
  end
@@ -57,19 +68,19 @@ module Faraday
57
68
  end
58
69
 
59
70
  @app.call env
60
- rescue Timeout::Error, Errno::ETIMEDOUT => err
61
- raise Faraday::Error::TimeoutError, err
71
+ rescue Timeout::Error, Errno::ETIMEDOUT => e
72
+ raise Faraday::TimeoutError, e
62
73
  end
63
74
 
64
75
  private
65
76
 
66
77
  def create_request(env)
67
78
  request = Net::HTTPGenericRequest.new \
68
- env[:method].to_s.upcase, # request method
69
- !!env[:body], # is there request body
70
- :head != env[:method], # is there response body
71
- env[:url].request_uri, # request uri path
72
- env[:request_headers] # request headers
79
+ env[:method].to_s.upcase, # request method
80
+ !!env[:body], # is there request body
81
+ env[:method] != :head, # is there response body
82
+ env[:url].request_uri, # request uri path
83
+ env[:request_headers] # request headers
73
84
 
74
85
  if env[:body].respond_to?(:read)
75
86
  request.body_stream = env[:body]
@@ -80,9 +91,44 @@ module Faraday
80
91
  end
81
92
 
82
93
  def perform_request(http, env)
83
- if :get == env[:method] and !env[:body]
94
+ if env[:request].stream_response?
95
+ size = 0
96
+ yielded = false
97
+ http_response = request_with_wrapped_block(http, env) do |chunk|
98
+ if chunk.bytesize.positive? || size.positive?
99
+ yielded = true
100
+ size += chunk.bytesize
101
+ env[:request].on_data.call(chunk, size)
102
+ end
103
+ end
104
+ env[:request].on_data.call('', 0) unless yielded
105
+ # Net::HTTP returns something,
106
+ # but it's not meaningful according to the docs.
107
+ http_response.body = nil
108
+ http_response
109
+ else
110
+ request_with_wrapped_block(http, env)
111
+ end
112
+ end
113
+
114
+ def request_with_wrapped_block(http, env, &block)
115
+ if (env[:method] == :get) && !env[:body]
84
116
  # prefer `get` to `request` because the former handles gzip (ruby 1.9)
85
- http.get env[:url].request_uri, env[:request_headers]
117
+ request_via_get_method(http, env, &block)
118
+ else
119
+ request_via_request_method(http, env, &block)
120
+ end
121
+ end
122
+
123
+ def request_via_get_method(http, env, &block)
124
+ http.get env[:url].request_uri, env[:request_headers], &block
125
+ end
126
+
127
+ def request_via_request_method(http, env, &block)
128
+ if block_given?
129
+ http.request create_request(env) do |response|
130
+ response.read_body(&block)
131
+ end
86
132
  else
87
133
  http.request create_request(env)
88
134
  end
@@ -93,49 +139,56 @@ module Faraday
93
139
  end
94
140
 
95
141
  def net_http_connection(env)
96
- if proxy = env[:request][:proxy]
97
- Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port, proxy[:user], proxy[:password])
98
- else
99
- Net::HTTP
100
- end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
142
+ klass = if (proxy = env[:request][:proxy])
143
+ Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
144
+ proxy[:user], proxy[:password])
145
+ else
146
+ Net::HTTP
147
+ end
148
+ port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
149
+ klass.new(env[:url].hostname, port)
101
150
  end
102
151
 
103
152
  def configure_ssl(http, ssl)
104
- http.use_ssl = true
105
- http.verify_mode = ssl_verify_mode(ssl)
106
- http.cert_store = ssl_cert_store(ssl)
107
-
108
- http.cert = ssl[:client_cert] if ssl[:client_cert]
109
- http.key = ssl[:client_key] if ssl[:client_key]
110
- http.ca_file = ssl[:ca_file] if ssl[:ca_file]
111
- http.ca_path = ssl[:ca_path] if ssl[:ca_path]
153
+ http.use_ssl = true
154
+ http.verify_mode = ssl_verify_mode(ssl)
155
+ http.cert_store = ssl_cert_store(ssl)
156
+
157
+ http.cert = ssl[:client_cert] if ssl[:client_cert]
158
+ http.key = ssl[:client_key] if ssl[:client_key]
159
+ http.ca_file = ssl[:ca_file] if ssl[:ca_file]
160
+ http.ca_path = ssl[:ca_path] if ssl[:ca_path]
112
161
  http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
113
- http.ssl_version = ssl[:version] if ssl[:version]
114
- http.min_version = ssl[:min_version] if ssl[:min_version]
115
- http.max_version = ssl[:max_version] if ssl[:max_version]
162
+ http.ssl_version = ssl[:version] if ssl[:version]
163
+ http.min_version = ssl[:min_version] if ssl[:min_version]
164
+ http.max_version = ssl[:max_version] if ssl[:max_version]
116
165
  end
117
166
 
118
167
  def configure_request(http, req)
119
168
  if req[:timeout]
120
- http.read_timeout = req[:timeout]
121
- http.open_timeout = req[:timeout]
122
- http.write_timeout = req[:timeout] if http.respond_to?(:write_timeout=)
169
+ http.read_timeout = req[:timeout]
170
+ http.open_timeout = req[:timeout]
171
+ if http.respond_to?(:write_timeout=)
172
+ http.write_timeout = req[:timeout]
173
+ end
174
+ end
175
+ http.open_timeout = req[:open_timeout] if req[:open_timeout]
176
+ if req[:write_timeout] && http.respond_to?(:write_timeout=)
177
+ http.write_timeout = req[:write_timeout]
123
178
  end
124
- http.open_timeout = req[:open_timeout] if req[:open_timeout]
125
- http.write_timeout = req[:write_timeout] if req[:write_timeout] && http.respond_to?(:write_timeout=)
126
- # Only set if Net::Http supports it, since Ruby 2.5.
127
- http.max_retries = 0 if http.respond_to?(:max_retries=)
179
+ # Only set if Net::Http supports it, since Ruby 2.5.
180
+ http.max_retries = 0 if http.respond_to?(:max_retries=)
128
181
 
129
- @config_block.call(http) if @config_block
182
+ @config_block&.call(http)
130
183
  end
131
184
 
132
185
  def ssl_cert_store(ssl)
133
186
  return ssl[:cert_store] if ssl[:cert_store]
134
- return @cert_store if @cert_store
135
- # Use the default cert store by default, i.e. system ca certs
136
- @cert_store = OpenSSL::X509::Store.new
137
- @cert_store.set_default_paths
138
- @cert_store
187
+
188
+ @ssl_cert_store ||= begin
189
+ # Use the default cert store by default, i.e. system ca certs
190
+ OpenSSL::X509::Store.new.tap(&:set_default_paths)
191
+ end
139
192
  end
140
193
 
141
194
  def ssl_verify_mode(ssl)
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
4
  class Adapter
5
+ # Net::HTTP::Persistent adapter.
3
6
  class NetHttpPersistent < NetHttp
4
7
  dependency 'net/http/persistent'
5
8
 
@@ -7,61 +10,79 @@ module Faraday
7
10
 
8
11
  def net_http_connection(env)
9
12
  @cached_connection ||=
10
- if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
11
- options = {name: 'Faraday'}
12
- options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size)
13
+ if Net::HTTP::Persistent.instance_method(:initialize)
14
+ .parameters.first == %i[key name]
15
+ options = { name: 'Faraday' }
16
+ if @connection_options.key?(:pool_size)
17
+ options[:pool_size] = @connection_options[:pool_size]
18
+ end
13
19
  Net::HTTP::Persistent.new(options)
14
20
  else
15
21
  Net::HTTP::Persistent.new('Faraday')
16
22
  end
17
23
 
18
24
  proxy_uri = proxy_uri(env)
19
- @cached_connection.proxy = proxy_uri if @cached_connection.proxy_uri != proxy_uri
25
+ if @cached_connection.proxy_uri != proxy_uri
26
+ @cached_connection.proxy = proxy_uri
27
+ end
20
28
  @cached_connection
21
29
  end
22
30
 
23
31
  def proxy_uri(env)
24
32
  proxy_uri = nil
25
33
  if (proxy = env[:request][:proxy])
26
- proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
34
+ proxy_uri = if proxy[:uri].is_a?(::URI::HTTP)
35
+ proxy[:uri].dup
36
+ else
37
+ ::URI.parse(proxy[:uri].to_s)
38
+ end
27
39
  proxy_uri.user = proxy_uri.password = nil
28
- # awful patch for net-http-persistent 2.8 not unescaping user/password
29
- (class << proxy_uri; self; end).class_eval do
30
- define_method(:user) { proxy[:user] }
31
- define_method(:password) { proxy[:password] }
32
- end if proxy[:user]
40
+ # awful patch for net-http-persistent 2.8
41
+ # not unescaping user/password
42
+ if proxy[:user]
43
+ (class << proxy_uri; self; end).class_eval do
44
+ define_method(:user) { proxy[:user] }
45
+ define_method(:password) { proxy[:password] }
46
+ end
47
+ end
33
48
  end
34
49
  proxy_uri
35
50
  end
36
51
 
37
52
  def perform_request(http, env)
38
53
  http.request env[:url], create_request(env)
39
- rescue Errno::ETIMEDOUT => error
40
- raise Faraday::Error::TimeoutError, error
41
- rescue Net::HTTP::Persistent::Error => error
42
- if error.message.include? 'Timeout'
43
- raise Faraday::Error::TimeoutError, error
44
- elsif error.message.include? 'connection refused'
45
- raise Faraday::Error::ConnectionFailed, error
46
- else
47
- raise
54
+ rescue Errno::ETIMEDOUT => e
55
+ raise Faraday::TimeoutError, e
56
+ rescue Net::HTTP::Persistent::Error => e
57
+ raise Faraday::TimeoutError, e if e.message.include? 'Timeout'
58
+
59
+ if e.message.include? 'connection refused'
60
+ raise Faraday::ConnectionFailed, e
48
61
  end
62
+
63
+ raise
49
64
  end
50
65
 
66
+ SSL_CONFIGURATIONS = {
67
+ certificate: :client_cert,
68
+ private_key: :client_key,
69
+ ca_file: :ca_file,
70
+ ssl_version: :version,
71
+ min_version: :min_version,
72
+ max_version: :max_version
73
+ }.freeze
74
+
51
75
  def configure_ssl(http, ssl)
52
76
  http_set(http, :verify_mode, ssl_verify_mode(ssl))
53
- http_set(http, :cert_store, ssl_cert_store(ssl))
77
+ http_set(http, :cert_store, ssl_cert_store(ssl))
54
78
 
55
- http_set(http, :certificate, ssl[:client_cert]) if ssl[:client_cert]
56
- http_set(http, :private_key, ssl[:client_key]) if ssl[:client_key]
57
- http_set(http, :ca_file, ssl[:ca_file]) if ssl[:ca_file]
58
- http_set(http, :ssl_version, ssl[:version]) if ssl[:version]
79
+ SSL_CONFIGURATIONS
80
+ .select { |_, key| ssl[key] }
81
+ .each { |target, key| http_set(http, target, ssl[key]) }
59
82
  end
60
83
 
61
84
  def http_set(http, attr, value)
62
- if http.send(attr) != value
63
- http.send("#{attr}=", value)
64
- end
85
+ http.send("#{attr}=", value) if http.send(attr) != value
65
86
  end
66
87
  end
67
88
  end