Class: Calendar

Inherits:
Cogibara::OperatorBase show all
Defined in:
lib/cogibara/operators/calendar.rb

Instance Attribute Summary

Attributes inherited from Cogibara::OperatorBase

#clientID, #message_structure, #message_text, #operator_config

Instance Method Summary collapse

Methods inherited from Cogibara::OperatorBase

#confirm, #initialize, #process_file, #receive_message, #say

Constructor Details

This class inherits a constructor from Cogibara::OperatorBase

Instance Method Details

#initialize_operatorObject



5
6
7
# File 'lib/cogibara/operators/calendar.rb', line 5

def initialize_operator
  @calendar = Google::Calendar.new(username: self.operator_config["username"], password: self.operator_config["password"], app_name: 'cogibara-calendar-operator')
end

#process(message) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cogibara/operators/calendar.rb', line 9

def process(message)
  info = message.structure
  puts info
  case info[:action]
  when :CALENDAR_CREATE_EVENT
    # puts info
    title = info[:entities][:title]? info[:entities][:title][0] : "Untitled event"
    if info[:entities][:daterange]
      start_t = Time.parse(info[:entities][:daterange][0][:start] + " " + info[:entities][:timerange][0][:start])
      end_t = Time.parse(info[:entities][:daterange][0][:start] + " " + info[:entities][:timerange][0][:end]) # use same day because broken?
    else
      start_t = Time.parse(info[:entities][:timerange][0][:start])
      end_t = Time.parse(info[:entities][:timerange][0][:end]) # use same day because broken?
    end


    create_event = lambda do
      say "adding #{title} to your calendar"
      @calendar.create_event do |e|
        e.title = title
        e.start_time = start_t
        e.end_time = end_t
      end
    end
    # {code: :confirm, message: "create calendar event #{title} from #{start_t} to #{end_t}?", success: create_event}
    puts "confirming event"
    confirm("create calendar event #{title} from #{start_t} to #{end_t}?", create_event)
  else
    puts "unsupported calendar operation #{info[:action]}, returning nil"
    nil
  end
end