class LetsCert::ValidTime

Class used to process validation time from String. @author Sylvain Daubert

Constants

SECONDS_IN_ONE_DAY

Number of seconds in one day

SECONDS_IN_ONE_HOUR

Number of seconds in one hour

SECONDS_IN_ONE_MINUTE

Number of seconds in one minute

Public Class Methods

new(str) click to toggle source

@param [String] str time string. May be:

* an integer -> time in seconds
* an integer plus a letter:
  * 30m: 30 minutes,
  * 30h: 30 hours,
  * 30d: 30 days.
# File lib/letscert/valid_time.rb, line 56
def initialize(str)
  m = str.match(/^(\d+)([mhd])?$/)
  if m
    @seconds = case m[2]
               when nil
                 m[1].to_i
               when 'm'
                 m[1].to_i * SECONDS_IN_ONE_MINUTE
               when 'h'
                 m[1].to_i * SECONDS_IN_ONE_HOUR
               when 'd'
                 m[1].to_i * SECONDS_IN_ONE_DAY
               end
  else
    raise OptionParser::InvalidArgument,
          "invalid argument: valid-min #{str}"
  end
  @string = str
end
time_in_words(seconds) click to toggle source

Get time in words (e.g. xx days or xx hours) @param [Integer] seconds @return [String]

# File lib/letscert/valid_time.rb, line 38
def self.time_in_words(seconds)
  if seconds < SECONDS_IN_ONE_MINUTE
    '%u seconds' % seconds
  elsif seconds < SECONDS_IN_ONE_HOUR
    'about %u minutes' % (seconds / SECONDS_IN_ONE_MINUTE)
  elsif seconds < SECONDS_IN_ONE_DAY
    'about %u hours' % (seconds / SECONDS_IN_ONE_HOUR)
  else
    'about %u days' % (seconds / SECONDS_IN_ONE_DAY)
  end
end

Public Instance Methods

to_s() click to toggle source

Get time as string @return [String]

# File lib/letscert/valid_time.rb, line 84
def to_s
  @string
end
to_seconds() click to toggle source

Get time in seconds @return [Integer]

# File lib/letscert/valid_time.rb, line 78
def to_seconds
  @seconds
end