class FacebookGoogleCalendarSync::Synchroniser

Public Class Methods

new(facebook_calendar, google_calendar) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 14
def initialize(facebook_calendar, google_calendar)
  @google_calendar = with_timezone(google_calendar, google_calendar.timezone)
  @events = facebook_calendar.events.collect{ | facebook_event | with_google_calendar_timezone(facebook_event) }
end

Public Instance Methods

add_new_event(facebook_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 84
def add_new_event facebook_event
  add_event google_calendar.id, facebook_event.to_hash
end
convert(facebook_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 32
def convert facebook_event
  EventConverter.new(facebook_event, google_calendar.id)
end
current_time_in_google_calendar_timezone() click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 115
def current_time_in_google_calendar_timezone
  DateTime.now.convert_time_zone(google_calendar.timezone)
end
date_of_most_recent_event_update() click to toggle source

Use the date of the most recent event update as our 'line in the sand' rather than the Google calendar's updated property, because of the slight differences in the clocks between Facebook and Google calendar and the fact that this script takes a non-zero amount of time to run, which could lead to inconsitencies in the synchronisation logic.

# File lib/facebook_google_calendar_sync/synchroniser.rb, line 122
def date_of_most_recent_event_update
  events.max{ | event_a, event_b | event_a.last_modified <=> event_b.last_modified }.last_modified
end
event_created_since_calendar_last_modified(facebook_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 100
def event_created_since_calendar_last_modified facebook_event
  facebook_event.created > google_calendar.last_known_event_update
end
event_updated_since_calendar_last_modified(facebook_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 96
def event_updated_since_calendar_last_modified facebook_event
  facebook_event.last_modified > google_calendar.last_known_event_update
end
events() click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 19
def events
  @events
end
google_calendar() click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 23
def google_calendar
  @google_calendar
end
handle_google_event_found(facebook_event, google_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 75
def handle_google_event_found facebook_event, google_event
  if event_updated_since_calendar_last_modified facebook_event
    logger.info "Updating '#{facebook_event.summary}' in #{google_calendar.summary}"
    update_existing_event facebook_event, google_event
  else
    logger.info "Not updating '#{facebook_event.summary}' in #{google_calendar.summary} as #{facebook_event.last_modified} is not later than #{google_event.updated}"
  end
end
handle_google_event_not_found(facebook_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 66
def handle_google_event_not_found facebook_event
  if event_created_since_calendar_last_modified facebook_event
    logger.info "Adding '#{facebook_event.summary}' to #{google_calendar.summary}"
    add_new_event facebook_event
  else
    logger.info "Not updating '#{facebook_event.summary}' as it has been deleted from the target calendar since #{google_calendar.last_known_event_update}."
  end
end
merge_events(facebook_event, google_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 92
def merge_events facebook_event, google_event
  google_event.to_hash.merge(facebook_event.to_hash)
end
synchronise() click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 27
def synchronise
  synchronise_events
  update_last_known_event_update
end
synchronise_event(facebook_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 57
def synchronise_event facebook_event
  google_event = google_calendar.find_event_by_uid facebook_event.uid
  if google_event == nil
    handle_google_event_not_found facebook_event
  else
    handle_google_event_found facebook_event, with_google_calendar_timezone(google_event)
  end
end
synchronise_events() click to toggle source

TODO: Fix this method!

# File lib/facebook_google_calendar_sync/synchroniser.rb, line 41
def synchronise_events
  errors = []
  events.each do | facebook_event |
    converted_event = nil
    begin
      converted_event = convert(facebook_event)
      synchronise_event converted_event
    rescue StandardError => e
      logger.error e
      logger.error "Error synchronising event. #{converted_event.to_hash}" rescue nil
      errors << e
    end
  end
  raise "Errors synchronising calendar" if errors.any?
end
update_existing_event(facebook_event, google_event) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 88
def update_existing_event facebook_event, google_event
  update_event google_calendar.id, google_event.id, merge_events(facebook_event, google_event)
end
update_last_known_event_update() click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 104
def update_last_known_event_update
  last_modified = date_of_most_recent_event_update
  if last_modified != google_calendar.last_known_event_update
    logger.info "Updating description of '#{google_calendar.summary}' to include the time of the last known update, #{last_modified}"
    details = google_calendar.details.to_hash.merge({'description' => create_description(date_of_most_recent_event_update, current_time_in_google_calendar_timezone)})
    update_calendar google_calendar.id, details
  else
    logger.info "Not updating description of '#{google_calendar.summary}' as the date of the most recent update has not changed from #{google_calendar.last_known_event_update}."
  end
end
with_google_calendar_timezone(target) click to toggle source
# File lib/facebook_google_calendar_sync/synchroniser.rb, line 36
def with_google_calendar_timezone target
  with_timezone(target, google_calendar.timezone)
end