class GCalendar

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

Attributes

client[R]
gcal_id[R]

Public Class Methods

new(api_client, gcal_identifier) click to toggle source
# 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

Public Instance Methods

add_request_for_gcal_event_and_ical_event(gcal_event, ical_event) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
events() click to toggle source
# 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_requests() click to toggle source
# 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_api() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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