class RedZone::Lifetime

Simple time parser for TTL/SOA lifetimes

The simple time format format is a number followed by a time unit.

ie: ‘<Number> [Unit]`

Where the ‘Unit` is one of

If the units are missing, it is assumed to be in seconds

Public Class Methods

new(str) click to toggle source

Constructs a lifetime object from a string @param [String] str time stirng

# File lib/redzone/lifetime.rb, line 20
def initialize(str)
  if str.upcase =~ /([0-9]+)\s*([HDWM]?)/
    i    = $1.to_i
    pl   = "s" if i > 1
    pl ||= ""
    time = case $2
      when 'H' then [i * 3600,"#{i} Hour#{pl}"]
      when 'D' then [i * 86400,"#{i} Day#{pl}"]
      when 'M' then [i * 60,"#{i} Minute#{pl}"]
      when 'W' then [i * 604800,"#{i} Week#{pl}"]
      else [i,"#{i} Second#{pl}"]
    end
    @time = time.first
    @str  = time.last
  end
end

Public Instance Methods

seconds() click to toggle source

Returns the lifetime as seconds @return [Integer] seconds

# File lib/redzone/lifetime.rb, line 38
def seconds
  @time
end
to_s() click to toggle source

Returns the string representaton of the lifetime

Examples:

  • 300 Seconds

  • 1 Minute

  • 2 Hours

  • 1 Day

  • 2 Weeks

@return [String]

# File lib/redzone/lifetime.rb, line 51
def to_s
  @str
end