class Feedbook::Helpers::TimeIntervalParser

Constants

INTERVAL_FORMAT

Public Class Methods

parse(value) click to toggle source

Parses given string with interval and converts into a amount of seconds. @param value [String] String with interval (e.g. ‘10m’, ‘100s’, ‘20h’, ‘10d’)

@return [Integer] amount of seconds that equals given interval value @raise [Feedbook::Errors::InvalidIntervalFormatError] if given string is not a valid format

# 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