Skip to content

Commit aa9451f

Browse files
committed
Abstract timeout option parsing
Rubocop told me the method started to be too complex. lib/http/chainable.rb:94:5: C: Metrics/PerceivedComplexity: Perceived complexity for timeout is too high. [10/8] def timeout(options) ... ^^^^^^^^^^^^^^^^^^^^ 80 files inspected, 1 offense detected
1 parent 0b150cb commit aa9451f

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

lib/http/chainable.rb

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,7 @@ def timeout(options)
9494
klass, options = case options
9595
when Numeric then [HTTP::Timeout::Global, {:global_timeout => options}]
9696
when Hash
97-
options = options.dup
98-
%i[read write connect].each do |k|
99-
next unless options.key? k
100-
101-
if options.key?("#{k}_timeout".to_sym)
102-
raise ArgumentError, "can't pass both #{k} and #{"#{k}_timeout".to_sym}"
103-
end
104-
105-
options["#{k}_timeout".to_sym] = options.delete k
106-
end
107-
108-
options.each do |key, value|
109-
unless HTTP::Timeout::PerOperation::SETTINGS.member?(key) && value.is_a?(Numeric)
110-
raise ArgumentError, "invalid option #{key.inspect}, must be numeric " \
111-
"`.timeout(connect: x, write: y, read: z)`."
112-
end
113-
end
114-
115-
raise ArgumentError, "at least one option" if options.empty?
116-
117-
[HTTP::Timeout::PerOperation, options.dup]
97+
[HTTP::Timeout::PerOperation, HTTP::Timeout::PerOperation.parse_options(options)]
11898
when :null then [HTTP::Timeout::Null, {}]
11999
else raise ArgumentError, "Use `.timeout(:null)`, " \
120100
"`.timeout(global_timeout_in_seconds)` or " \

lib/http/timeout/per_operation.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ class PerOperation < Null
1212
READ_TIMEOUT = 0.25
1313

1414
SETTINGS = Set.new(%i[read_timeout write_timeout connect_timeout])
15+
16+
class << self
17+
def parse_options(options)
18+
options = options.dup.then { |opts| expand_names(opts) }
19+
options.each do |key, value|
20+
unless SETTINGS.member?(key) && value.is_a?(Numeric)
21+
raise ArgumentError, "invalid option #{key.inspect}, must be numeric " \
22+
"`.timeout(connect: x, write: y, read: z)`."
23+
end
24+
end
25+
26+
raise ArgumentError, "at least one option" if options.empty?
27+
28+
options
29+
end
30+
31+
private
32+
33+
def expand_names(options)
34+
%i[read write connect].each do |k|
35+
next unless options.key? k
36+
37+
if options.key?("#{k}_timeout".to_sym)
38+
raise ArgumentError, "can't pass both #{k} and #{"#{k}_timeout".to_sym}"
39+
end
40+
41+
options["#{k}_timeout".to_sym] = options.delete k
42+
end
43+
44+
options
45+
end
46+
end
47+
1548
def initialize(*args)
1649
super
1750

0 commit comments

Comments
 (0)