From 648610c4cddfebfe08c6fbc27a123e2851115bd1 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 26 Jun 2023 15:38:18 +0200 Subject: [PATCH] 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 --- lib/http/chainable.rb | 2 +- lib/http/timeout/per_operation.rb | 38 +++++++++++-------------------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/lib/http/chainable.rb b/lib/http/chainable.rb index 2a8c3e65..42115c41 100644 --- a/lib/http/chainable.rb +++ b/lib/http/chainable.rb @@ -95,7 +95,7 @@ def timeout(options) klass, options = case options when Numeric then [HTTP::Timeout::Global, {:global_timeout => options}] when Hash - [HTTP::Timeout::PerOperation, HTTP::Timeout::PerOperation.parse_options(options)] + [HTTP::Timeout::PerOperation, HTTP::Timeout::PerOperation.normalize_options(options)] when :null then [HTTP::Timeout::Null, {}] else raise ArgumentError, "Use `.timeout(:null)`, " \ "`.timeout(global_timeout_in_seconds)` or " \ diff --git a/lib/http/timeout/per_operation.rb b/lib/http/timeout/per_operation.rb index 589a9c8a..a98d391a 100644 --- a/lib/http/timeout/per_operation.rb +++ b/lib/http/timeout/per_operation.rb @@ -11,37 +11,25 @@ class PerOperation < Null WRITE_TIMEOUT = 0.25 READ_TIMEOUT = 0.25 - SETTINGS = Set.new(%i[read_timeout write_timeout connect_timeout]) + KEYS = %i[read write connect].to_h { |k| [k, :"#{k}_timeout"] }.freeze class << self - def parse_options(options) - options = options.dup.then { |opts| expand_names(opts) } - options.each do |key, value| - unless SETTINGS.member?(key) && value.is_a?(Numeric) - raise ArgumentError, "invalid option #{key.inspect}, must be numeric " \ - "`.timeout(connect: x, write: y, read: z)`." - end - end - - raise ArgumentError, "at least one option" if options.empty? - - options - end + def normalize_options(options) + normalized = {} + original = options.dup - private + KEYS.each do |short, long| + next if !original.key?(short) && !original.key?(long) + raise ArgumentError, "can't pass both #{short} and #{long}" if original.key?(short) && original.key?(long) - def expand_names(options) - %i[read write connect].each do |k| - next unless options.key? k - - if options.key?("#{k}_timeout".to_sym) - raise ArgumentError, "can't pass both #{k} and #{"#{k}_timeout".to_sym}" - end - - options["#{k}_timeout".to_sym] = options.delete k + normalized[long] = original.key?(long) ? original.delete(long) : original.delete(short) + raise ArgumentError, "#{long} must be numeric" unless normalized[long].is_a?(Numeric) end - options + raise ArgumentError, "unknown timeout options: #{original.keys.join(', ')}" if original.size.positive? + raise ArgumentError, "no timeout options given" if normalized.empty? + + normalized end end