Class: TimePup::AdverbParser

Inherits:
Object
  • Object
show all
Defined in:
lib/time_pup/adverb_parser.rb

Constant Summary collapse

STARTOFDAY =
8
ENDOFDAY =
17
DAYSOFWEEK =
[:monday, :tuesday, :wednesday,
:thursday, :friday, :saturday, :sunday]
MATCHERS =
{
  tomorrow: /tomorrow/,
  endofday: /endofday/,
  endofmonth: /endofmonth/,
  nextmonday: /nextmonday/,
  monday: /monday/,
  nexttuesday: /nexttuesday/,
  tuesday: /tuesday/,
  nextwednesday: /nextwednesday/,
  wednesday: /wednesday/,
  nextthursday: /nextthursday/,
  thursday: /thursday/,
  nextfriday: /nextfriday/,
  friday: /friday/,
  nextsaturday: /nextsaturday/,
  saturday: /saturday/,
  nextsunday: /nextsunday/,
  sunday: /sunday/,
  nextweek: /nextweek/
}

Class Method Summary collapse

Class Method Details

.date_of_next(day) ⇒ Object



88
89
90
91
92
# File 'lib/time_pup/adverb_parser.rb', line 88

def date_of_next(day)
  date  = DateTime.parse(day).to_time
  delta = date > Date.today ? 0 : 7
  (date + delta.days).change hour: STARTOFDAY
end

.endofdayObject



64
65
66
# File 'lib/time_pup/adverb_parser.rb', line 64

def endofday
  Time.now.utc.change hour: ENDOFDAY
end

.endofmonthObject



68
69
70
# File 'lib/time_pup/adverb_parser.rb', line 68

def endofmonth
  Time.now.end_of_month.utc.change hour: STARTOFDAY
end

.match_key_words(time) ⇒ Object



54
55
56
57
58
# File 'lib/time_pup/adverb_parser.rb', line 54

def match_key_words(time)
  MATCHERS.keys.inject([]) do
    |result, m| result << time.scan(MATCHERS[m])
  end.flatten
end

.nextweekObject



72
73
74
# File 'lib/time_pup/adverb_parser.rb', line 72

def nextweek
  Time.now.next_week.utc.change hour: STARTOFDAY
end

.parse(parsable, zone = 'UTC') ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/time_pup/adverb_parser.rb', line 29

def parse(parsable, zone = 'UTC')
  matches = match_key_words(parsable)
  unless matches.empty?
    time = parse_time(parsable, matches.first)
    if time
      hour = time.hour
      min = time.min
    end

    match = self.send(matches.first)
    return utc_offset(match, zone, hour, min)
  end
end

.parse_time(parsable, matcher) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/time_pup/adverb_parser.rb', line 43

def parse_time(parsable, matcher)
  parsable_parts = parsable.partition(matcher)
  time_part = nil
  [0, 2].each do |index|
    parsed_time = TimePup::TimeParser.parse(parsable_parts[index])
    time_part = parsed_time if parsed_time
  end

  time_part
end

.tomorrowObject



60
61
62
# File 'lib/time_pup/adverb_parser.rb', line 60

def tomorrow
  1.day.from_now.utc.change hour: STARTOFDAY
end