Skip to content

Commit

Permalink
normalize_options instead of parse_options
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stoivo authored and ixti committed Aug 29, 2024
1 parent 875201a commit 648610c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/http/chainable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 " \
Expand Down
38 changes: 13 additions & 25 deletions lib/http/timeout/per_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 648610c

Please sign in to comment.