Class: AwsLogs::Since
- Inherits:
-
Object
- Object
- AwsLogs::Since
- Defined in:
- lib/aws_logs/since.rb
Constant Summary collapse
- DEFAULT =
10.minutes.to_i
- ISO8601_REGEXP =
/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/
- FRIENDLY_REGEXP =
/(\d+)(\w+)/
Instance Method Summary collapse
- #find_match(regexp) ⇒ Object
- #friendly_format? ⇒ Boolean
- #friendly_seconds ⇒ Object
-
#initialize(str) ⇒ Since
constructor
A new instance of Since.
- #iso8601_format? ⇒ Boolean
- #iso8601_seconds ⇒ Object
-
#shorthand(k) ⇒ Object
s - seconds m - minutes h - hours d - days w - weeks.
- #to_i ⇒ Object
- #warning ⇒ Object
Constructor Details
#initialize(str) ⇒ Since
Returns a new instance of Since.
9 10 11 |
# File 'lib/aws_logs/since.rb', line 9 def initialize(str) @str = str end |
Instance Method Details
#find_match(regexp) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/aws_logs/since.rb', line 55 def find_match(regexp) md = @str.match(regexp) if md number, unit = md[1].to_i, md[2] end [number, unit] end |
#friendly_format? ⇒ Boolean
35 36 37 |
# File 'lib/aws_logs/since.rb', line 35 def friendly_format? !!@str.match(FRIENDLY_REGEXP) end |
#friendly_seconds ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aws_logs/since.rb', line 39 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? ⇒ Boolean
25 26 27 |
# File 'lib/aws_logs/since.rb', line 25 def iso8601_format? !!@str.match(ISO8601_REGEXP) end |
#iso8601_seconds ⇒ Object
29 30 31 32 |
# File 'lib/aws_logs/since.rb', line 29 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) ⇒ Object
s - seconds m - minutes h - hours d - days w - weeks
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aws_logs/since.rb', line 72 def shorthand(k) map = { s: :seconds, m: :minutes, h: :hours, d: :days, w: :weeks, } map[k.to_sym] || k end |
#to_i ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/aws_logs/since.rb', line 13 def to_i if iso8601_format? iso8601_seconds elsif friendly_format? friendly_seconds else puts warning return DEFAULT end end |
#warning ⇒ Object
63 64 65 |
# File 'lib/aws_logs/since.rb', line 63 def warning "WARN: since is not in a supported format. Falling back to 10m".color(:yellow) end |