Class: Synchrograph

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

Overview

Glues together the API Client, the GCalendar and the ICalendar to create the right requests, in the right order and submit them

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ Synchrograph

Returns a new instance of Synchrograph.



11
12
13
# File 'lib/synchrograph.rb', line 11

def initialize(api_client)
  @client = api_client
end

Instance Method Details

#synchronise(gcalendar, icalendar) ⇒ Object



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/synchrograph.rb', line 16

def synchronise(gcalendar, icalendar)

  ical_events = icalendar.first.events.find_all {|ie| ie.recurrence_id.nil?}
  gcal_events = gcalendar.events

  response_body = ""

  #Insert or update
  ical_events.each do |ie|
    ge = gcal_events.find {|ge| ge['id'] == ie.google_calendar_id}

    gcalendar.add_request_for_gcal_event_and_ical_event ge, ie
    
  end

  (gcal_events.find_all {|ge| ge['recurring_event_id'].nil? and not ical_events.map {|ie| ie.google_calendar_id}.include?(ge['id'])}).each do |ge|
    gcalendar.add_request_for_gcal_event_and_ical_event ge, nil
  end

  response_body << gcalendar.execute_requests.body

  #Now deal with recurrence exceptions
  ical_events = icalendar.first.events.find_all {|ie| not ie.recurrence_id.nil?}

  ical_events.group_by {|ie| ie.google_calendar_id}.each_pair do |ge_id, re_e|
    gcal_instances = gcalendar.instances_for_event_id ge_id

    re_e.each do |ie|
      instance = gcal_instances.find {|i| i.start.date_time == ie.recurrence_id}
      next if instance.nil?

      ie.update_from_parent_component!

      ie.google_calendar_id = instance.id
      gcalendar.add_request_for_gcal_event_and_ical_event(instance, ie)
    end
  end

  er = gcalendar.execute_requests
  response_body << er.body if er

  return response_body
end