Skip to content

Commit 648610c

Browse files
stoivoixti
authored andcommitted
normalize_options instead of parse_options
rubocop offences be rubocop -a 15:37:07 lib/http/timeout/per_operation.rb:17:9: C: Metrics/AbcSize: Assignment Branch Condition size for normalize_options is too high. [<5, 23, 9> 25.2/17] def normalize_options(options) ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lib/http/timeout/per_operation.rb:17:9: C: Metrics/CyclomaticComplexity: Cyclomatic complexity for normalize_options is too high. [10/7] def normalize_options(options) ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lib/http/timeout/per_operation.rb:17:9: C: Metrics/MethodLength: Method has too many lines. [11/10] def normalize_options(options) ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lib/http/timeout/per_operation.rb:17:9: C: Metrics/PerceivedComplexity: Perceived complexity for normalize_options is too high. [10/8] def normalize_options(options) ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 80/80 files |================================================================================================================ 100 =================================================================================================================>| Time: 00:00:00 80 files inspected, 4 offenses detected
1 parent 875201a commit 648610c

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

lib/http/chainable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def timeout(options)
9595
klass, options = case options
9696
when Numeric then [HTTP::Timeout::Global, {:global_timeout => options}]
9797
when Hash
98-
[HTTP::Timeout::PerOperation, HTTP::Timeout::PerOperation.parse_options(options)]
98+
[HTTP::Timeout::PerOperation, HTTP::Timeout::PerOperation.normalize_options(options)]
9999
when :null then [HTTP::Timeout::Null, {}]
100100
else raise ArgumentError, "Use `.timeout(:null)`, " \
101101
"`.timeout(global_timeout_in_seconds)` or " \

lib/http/timeout/per_operation.rb

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,25 @@ class PerOperation < Null
1111
WRITE_TIMEOUT = 0.25
1212
READ_TIMEOUT = 0.25
1313

14-
SETTINGS = Set.new(%i[read_timeout write_timeout connect_timeout])
14+
KEYS = %i[read write connect].to_h { |k| [k, :"#{k}_timeout"] }.freeze
1515

1616
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
17+
def normalize_options(options)
18+
normalized = {}
19+
original = options.dup
3020

31-
private
21+
KEYS.each do |short, long|
22+
next if !original.key?(short) && !original.key?(long)
23+
raise ArgumentError, "can't pass both #{short} and #{long}" if original.key?(short) && original.key?(long)
3224

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
25+
normalized[long] = original.key?(long) ? original.delete(long) : original.delete(short)
26+
raise ArgumentError, "#{long} must be numeric" unless normalized[long].is_a?(Numeric)
4227
end
4328

44-
options
29+
raise ArgumentError, "unknown timeout options: #{original.keys.join(', ')}" if original.size.positive?
30+
raise ArgumentError, "no timeout options given" if normalized.empty?
31+
32+
normalized
4533
end
4634
end
4735

0 commit comments

Comments
 (0)