11# frozen_string_literal: true
22
33require "http/base64"
4+ require "http/chainable/helpers"
45require "http/headers"
56
67module HTTP
@@ -169,15 +170,12 @@ def timeout(options)
169170 when Numeric then [ HTTP ::Timeout ::Global , { global : options } ]
170171 when Hash then [ HTTP ::Timeout ::PerOperation , options . dup ]
171172 when :null then [ HTTP ::Timeout ::Null , { } ]
172- else raise ArgumentError , "Use `.timeout(global_timeout_in_seconds)` or `.timeout(connect: x, write: y, read: z)`."
173-
173+ else raise ArgumentError ,
174+ "Use `.timeout(global_timeout_in_seconds)` " \
175+ "or `.timeout(connect: x, write: y, read: z)`."
174176 end
175177
176- %i[ global read write connect ] . each do |k |
177- next unless options . key? k
178-
179- options [ :"#{ k } _timeout" ] = options . delete k
180- end
178+ normalize_timeout_keys! ( options )
181179
182180 branch default_options . merge (
183181 timeout_class : klass ,
@@ -223,8 +221,7 @@ def timeout(options)
223221 # @return [HTTP::Client, Object]
224222 # @api public
225223 def persistent ( host , timeout : 5 )
226- options = { keep_alive_timeout : timeout }
227- p_client = branch default_options . merge ( options ) . with_persistent host
224+ p_client = branch default_options . merge ( keep_alive_timeout : timeout ) . with_persistent ( host )
228225 return p_client unless block_given?
229226
230227 yield p_client
@@ -242,13 +239,7 @@ def persistent(host, timeout: 5)
242239 # @return [HTTP::Client]
243240 # @api public
244241 def via ( *proxy )
245- proxy_hash = { } #: Hash[Symbol, untyped]
246- proxy_hash [ :proxy_address ] = proxy [ 0 ] if proxy [ 0 ] . is_a? ( String )
247- proxy_hash [ :proxy_port ] = proxy [ 1 ] if proxy [ 1 ] . is_a? ( Integer )
248- proxy_hash [ :proxy_username ] = proxy [ 2 ] if proxy [ 2 ] . is_a? ( String )
249- proxy_hash [ :proxy_password ] = proxy [ 3 ] if proxy [ 3 ] . is_a? ( String )
250- proxy_hash [ :proxy_headers ] = proxy [ 2 ] if proxy [ 2 ] . is_a? ( Hash )
251- proxy_hash [ :proxy_headers ] = proxy [ 4 ] if proxy [ 4 ] . is_a? ( Hash )
242+ proxy_hash = build_proxy_hash ( proxy )
252243
253244 raise ( RequestError , "invalid HTTP proxy: #{ proxy_hash } " ) unless ( 2 ..5 ) . cover? ( proxy_hash . keys . size )
254245
0 commit comments