Class: GCalendar

Inherits:
Object
  • Object
show all
Defined in:
lib/synchrograph/gcalendar.rb

Overview

Handles generating fetching the existing list of events in GCal and generating requests to correct this list

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client, gcal_identifier) ⇒ GCalendar

Returns a new instance of GCalendar.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/synchrograph/gcalendar.rb', line 10

def initialize(api_client, gcal_identifier)
  @client = api_client
  @gcal_id = gcal_identifier
  @pending_requests = []

  #categories = icalendar.first.events.map {|e| e.categories}.flatten.uniq { |u| u.to_s}
  #cat_to_col_mapping = {}
  #categories.each_with_index do |c, i|
  #  cat_to_col_mapping[c] = i%10
  #end

  @filters = [
    BaseFilter,
    CatToColFilter.new({
      "Protected time for Specific Task & Finish Work": 6,
      "Work Related Travel (mark as Out of Office)": 3,
      "Just for Information (mark as Free)": 8,
      "Authorised Leave (annual  maternity etc)": 5,
      "Formal Event or Workshop": 10
      })
  ]

end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/synchrograph/gcalendar.rb', line 8

def client
  @client
end

#gcal_idObject (readonly)

Returns the value of attribute gcal_id.



8
9
10
# File 'lib/synchrograph/gcalendar.rb', line 8

def gcal_id
  @gcal_id
end

Instance Method Details

#add_request_for_gcal_event_and_ical_event(gcal_event, ical_event) ⇒ Object



71
72
73
74
75
# File 'lib/synchrograph/gcalendar.rb', line 71

def add_request_for_gcal_event_and_ical_event(gcal_event, ical_event)
  @pending_requests << insert(ical_event) if gcal_event.nil?
  @pending_requests << delete(gcal_event) if ical_event.nil? and gcal_event['recurring_event_id'].nil? and (gcal_event['status'] != 'cancelled')
  @pending_requests << update(gcal_event, ical_event) if gcal_event and ical_event
end

#delete(gcal_event) ⇒ Object



96
97
98
99
100
101
# File 'lib/synchrograph/gcalendar.rb', line 96

def delete(gcal_event)
  {
    api_method: gcal_api.events.delete,
    parameters: {'calendarId' => gcal_id, 'eventId' => gcal_event['id']},
  }
end

#event_for_id(id) ⇒ Object



52
53
54
55
# File 'lib/synchrograph/gcalendar.rb', line 52

def event_for_id(id)
  result = client.execute(api_method:gcal_api.events.get, parameters: {'calendarId' => gcal_id, 'eventId' => id})
  return result.data
end

#eventsObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/synchrograph/gcalendar.rb', line 40

def events
  result = client.execute(api_method:gcal_api.events.list, parameters: {'calendarId' => gcal_id, 'showDeleted' => true})
  gcal_events = result.data.items

  while result.next_page_token
    result = client.execute(result.next_page)
    gcal_events += result.data.items
  end

  return gcal_events
end

#execute_requestsObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/synchrograph/gcalendar.rb', line 116

def execute_requests
  while @pending_requests.any?
    batch = Google::APIClient::BatchRequest.new
    
    @pending_requests.slice!(0,950).each do |r|
      batch.add r
    end
    
    @pending_requests = []
    return @client.execute(batch)
  end
end

#gcal_apiObject



34
35
36
# File 'lib/synchrograph/gcalendar.rb', line 34

def gcal_api
  @gcal_api ||= client.discovered_api('calendar', 'v3')
end

#google_calendar_representation_of_ical_event(ical_event) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/synchrograph/gcalendar.rb', line 77

def google_calendar_representation_of_ical_event(ical_event)
  representation = {}

  @filters.each do |f|
    representation = f.call(ical_event, representation)
  end
  
  return representation
end

#insert(ical_event) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/synchrograph/gcalendar.rb', line 87

def insert(ical_event)
  {
    api_method: gcal_api.events.insert,
    headers: {'Content-Type' => 'application/json'},
    parameters: {'calendarId' => gcal_id},
    body:JSON.dump(google_calendar_representation_of_ical_event(ical_event))
  }
end

#instances_for_event_id(event_id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/synchrograph/gcalendar.rb', line 57

def instances_for_event_id(event_id)
  result = client.execute(api_method:gcal_api.events.instances, parameters: {'calendarId' => gcal_id, 'eventId' => event_id})
  gcal_instances = result.data.items

  while result.next_page_token
    result = client.execute(result.next_page)
    gcal_instances += result.data.items
  end

  return gcal_instances
end

#update(gcal_event, ical_event) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/synchrograph/gcalendar.rb', line 103

def update(gcal_event, ical_event)
  new_rep = google_calendar_representation_of_ical_event(ical_event)

  {
    api_method: gcal_api.events.update,
    headers: {'Content-Type' => 'application/json'},
    parameters: {'calendarId' => gcal_id, 'eventId' => ical_event.google_calendar_id},
    body:JSON.dump(new_rep)
  }
end