Module: CalendarAssistant::CLI::Helpers

Defined in:
lib/calendar_assistant/cli/helpers.rb

Defined Under Namespace

Classes: ChronicParseException

Class Method Summary collapse

Class Method Details

.find_av_uri(ca, timespec) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/calendar_assistant/cli/helpers.rb', line 34

def self.find_av_uri(ca, timespec)
  time = Chronic.parse timespec
  range = time..(time + 5.minutes)
  event_set = ca.find_events range

  [CalendarAssistant::Event::Response::ACCEPTED,
   CalendarAssistant::Event::Response::TENTATIVE,
   CalendarAssistant::Event::Response::NEEDS_ACTION].each do |response|
    event_set.events.reverse.select do |event|
      event.response_status == response
    end.each do |event|
      return [event_set.new(event), event.av_uri] if event.av_uri
    end
  end

  event_set.new(nil)
end

.nowObject



26
27
28
29
30
31
32
# File 'lib/calendar_assistant/cli/helpers.rb', line 26

def self.now
  CalendarAssistant::Event.new(
    Google::Apis::CalendarV3::Event.new(start: Google::Apis::CalendarV3::EventDateTime.new(date_time: Time.now),
                                        end: Google::Apis::CalendarV3::EventDateTime.new(date_time: Time.now),
                                        summary: Rainbow("          now          ").inverse.faint)
  )
end

.parse_datespec(userspec) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/calendar_assistant/cli/helpers.rb', line 8

def self.parse_datespec(userspec)
  start_userspec, end_userspec = userspec.split(/ ?\.\.\.? ?/)

  if end_userspec.nil?
    time = Chronic.parse(userspec) || raise(ChronicParseException, "could not parse '#{userspec}'")
    return time.beginning_of_day..time.end_of_day
  end

  start_time = Chronic.parse(start_userspec) || raise(ChronicParseException, "could not parse '#{start_userspec}'")
  end_time = Chronic.parse(end_userspec) || raise(ChronicParseException, "could not parse '#{end_userspec}'")

  if start_time.to_date == end_time.to_date
    start_time..end_time
  else
    start_time.beginning_of_day..end_time.end_of_day
  end
end