class AwsLogs::Since

Constants

DEFAULT
FRIENDLY_REGEXP
ISO8601_REGEXP

Public Class Methods

new(str) click to toggle source
# File lib/aws_logs/since.rb, line 8
def initialize(str)
  @str = str
end

Public Instance Methods

find_match(regexp) click to toggle source
# File lib/aws_logs/since.rb, line 54
def find_match(regexp)
  md = @str.match(regexp)
  if md
    number, unit = md[1].to_i, md[2]
  end
  [number, unit]
end
friendly_format?() click to toggle source
# File lib/aws_logs/since.rb, line 34
def friendly_format?
  !!@str.match(FRIENDLY_REGEXP)
end
friendly_seconds() click to toggle source
# File lib/aws_logs/since.rb, line 38
def friendly_seconds
  number, unit = find_match(FRIENDLY_REGEXP)
  unless number && unit
    puts warning
    return DEFAULT
  end

  meth = shorthand(unit)
  if number.respond_to?(meth)
    number.send(meth).to_i
  else
    puts warning
    return DEFAULT
  end
end
iso8601_format?() click to toggle source
# File lib/aws_logs/since.rb, line 24
def iso8601_format?
  !!@str.match(ISO8601_REGEXP)
end
iso8601_seconds() click to toggle source
# File lib/aws_logs/since.rb, line 28
def iso8601_seconds
  # https://stackoverflow.com/questions/3775544/how-do-you-convert-an-iso-8601-date-to-a-unix-timestamp-in-ruby
  Time.iso8601(@str.sub(/ /,'T')).to_i
end
shorthand(k) click to toggle source

s - seconds m - minutes h - hours d - days w - weeks

# File lib/aws_logs/since.rb, line 71
def shorthand(k)
  map = {
    s: :seconds,
    m: :minutes,
    h: :hours,
    d: :days,
    w: :weeks,
  }
  map[k.to_sym] || k
end
to_i() click to toggle source
# File lib/aws_logs/since.rb, line 12
def to_i
  if iso8601_format?
    iso8601_seconds
  elsif friendly_format?
    friendly_seconds
  else
    puts warning
    return DEFAULT
  end
end
warning() click to toggle source
# File lib/aws_logs/since.rb, line 62
def warning
  "WARN: since is not in a supported format. Falling back to 10m".color(:yellow)
end