Class: FacebookGoogleCalendarSync::Synchroniser

Inherits:
Object
  • Object
show all
Includes:
GoogleCalendarClient, GoogleCalendarDescription, Logging
Defined in:
lib/facebook_google_calendar_sync/synchroniser.rb

Constant Summary

Constants included from GoogleCalendarDescription

GoogleCalendarDescription::DESCRIPTION_MIDDLE, GoogleCalendarDescription::DESCRIPTION_PREFIX, GoogleCalendarDescription::DESCRIPTION_SUFFIX

Instance Method Summary collapse

Methods included from GoogleCalendarClient

#add_event, configure, #create_calendar, #find_calendar_details_by_summary, #find_primary_calendar_details, #get_calendar, #get_calendar_list, #get_calendar_metadata, #update_calendar, #update_event

Methods included from GoogleCalendarDescription

#create_description, #extract_last_modified_date

Methods included from Logging

#logger

Constructor Details

#initialize(facebook_calendar, google_calendar) ⇒ Synchroniser

Returns a new instance of Synchroniser.



12
13
14
15
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 12

def initialize(facebook_calendar, google_calendar)
  @facebook_calendar = facebook_calendar
  @google_calendar = google_calendar
end

Instance Method Details

#current_time_in_google_calendar_timezoneObject



89
90
91
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 89

def current_time_in_google_calendar_timezone
  to_local(DateTime.now)
end

#date_of_most_recent_event_updateObject



97
98
99
100
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 97

def date_of_most_recent_event_update
  most_recently_modified_event = facebook_calendar.events.max{ | event_a, event_b | event_a.last_modified <=> event_b.last_modified }
  most_recently_modified_event.last_modified
end

#event_created_since_calendar_last_modified(facebook_event) ⇒ Object



74
75
76
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 74

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) ⇒ Object



70
71
72
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 70

def event_updated_since_calendar_last_modified facebook_event
  facebook_event.last_modified > google_calendar.last_known_event_update
end

#facebook_calendarObject



17
18
19
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 17

def facebook_calendar
  @facebook_calendar
end

#google_calendarObject



21
22
23
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 21

def google_calendar
  @google_calendar
end

#handle_google_event_found(facebook_event, google_event) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 61

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_event google_calendar.id, google_event.id, facebook_event.to_hash.merge(google_event)
  else
    logger.info "Not updating '#{facebook_event.summary}' in #{google_calendar.summary} as #{facebook_event.last_modified} is not later than #{to_local(google_event.updated)}"
  end
end

#handle_google_event_not_found(facebook_event) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 52

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_event google_calendar.id, facebook_event.to_hash
  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

#synchroniseObject



25
26
27
28
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 25

def synchronise
  synchronise_events
  update_last_known_event_update
end

#synchronise_event(facebook_event) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 43

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, google_event
  end
end

#synchronise_eventsObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 30

def synchronise_events
  facebook_calendar.events.each do | facebook_event |
    begin
      synchronise_event EventConverter.new(facebook_event, google_calendar.id, google_calendar.timezone)
    rescue StandardError => e
      logger.error e
      logger.error "Error synchronising event. Please note that if this was a new event, it will not have been added to your calendar."
      logger.error facebook_event.summary
      raise e
    end
  end
end

#to_local(date_or_time) ⇒ Object



93
94
95
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 93

def to_local date_or_time
  date_or_time.convert_time_zone(google_calendar.timezone)
end

#update_last_known_event_updateObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 78

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 #{to_local(google_calendar.last_known_event_update)}."
  end
end