Class: Feedbook::Helpers::TimeIntervalParser

Inherits:
Object
  • Object
show all
Defined in:
lib/feedbook/helpers/time_interval_parser.rb

Constant Summary collapse

INTERVAL_FORMAT =
/\A(\d+)(s|m|h|d)\z/

Class Method Summary collapse

Class Method Details

.parse(value) ⇒ Integer

Parses given string with interval and converts into a amount of seconds.

Parameters:

  • value (String)

    String with interval (e.g. ‘10m’, ‘100s’, ‘20h’, ‘10d’)

Returns:

  • (Integer)

    amount of seconds that equals given interval value

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/feedbook/helpers/time_interval_parser.rb', line 15

def self.parse(value)
  if value.strip =~ INTERVAL_FORMAT
    number, type = INTERVAL_FORMAT.match(value).captures
    case type
    when 's'
      Integer(number).seconds
    when 'm'
      Integer(number).minutes
    when 'h'
      Integer(number).hours
    when 'd'
      Integer(number).days
    end
  else
    raise ArgmumentError.new
  end

rescue
  raise Errors::InvalidIntervalFormatError.new
end