Class: Blertr::TimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/blertr/time_parser.rb

Constant Summary collapse

SECONDS_IN_MIN =
60
SECONDS_IN_HOUR =
3600
SECONDS_IN_DAY =
86400

Class Method Summary collapse

Class Method Details

.convert_word(input_word) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blertr/time_parser.rb', line 23

def self.convert_word input_word
  multiplier = 1
  if input_word =~ /^s/
    multiplier = 1
  elsif input_word =~ /^m/
    multiplier = SECONDS_IN_MIN
  elsif input_word =~ /^(h|hour|hr)/
    multiplier = SECONDS_IN_HOUR
  elsif input_word =~ /^d/
    multiplier = SECONDS_IN_DAY
  end
  multiplier
end

.to_seconds(time_string) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/blertr/time_parser.rb', line 7

def self.to_seconds time_string
  time_seconds = 0
  if time_string =~ /^(\d+)$/
    # only digits. assume seconds
    time_seconds = $1.to_i
  elsif time_string =~ /^(\d+)\s+(\w+)$/
    # digits and words
    digits = $1.to_i
    mult = convert_word $2
    time_seconds = digits * mult
  end
  time_seconds = time_seconds > 0 ? time_seconds : nil

  time_seconds
end