Module: DeepL::Utils::NumberParser

Extended by:
NumberParser
Included in:
NumberParser
Defined in:
lib/deepl/utils/number_parser.rb

Overview

Reads numbers from API responses without failing on unexpected values.

Instance Method Summary collapse

Instance Method Details

#count(value) ⇒ Integer

Returns The value if it is an Integer, otherwise 0.

Returns:

  • (Integer) —

    The value if it is an Integer, otherwise 0.



14
15
16
# File 'lib/deepl/utils/number_parser.rb', line 14

def count(value)
  value.is_a?(Integer) ? value : 0
end

#minutes(value) ⇒ Float

Returns The value as a Float if it is numeric, otherwise 0.0.

Returns:

  • (Float) —

    The value as a Float if it is numeric, otherwise 0.0.



19
20
21
# File 'lib/deepl/utils/number_parser.rb', line 19

def minutes(value)
  value.is_a?(Numeric) ? value.to_f : 0.0
end

#optional_count(value) ⇒ Integer?

Returns The value if it is an Integer, otherwise nil.

Returns:

  • (Integer, nil) —

    The value if it is an Integer, otherwise nil.



29
30
31
# File 'lib/deepl/utils/number_parser.rb', line 29

def optional_count(value)
  value.is_a?(Integer) ? value : nil
end

#optional_minutes(value) ⇒ Float?

Returns The value as a Float if it is numeric, otherwise nil.

Returns:

  • (Float, nil) —

    The value as a Float if it is numeric, otherwise nil.



24
25
26
# File 'lib/deepl/utils/number_parser.rb', line 24

def optional_minutes(value)
  value.is_a?(Numeric) ? value.to_f : nil
end