Class: FacebookGoogleCalendarSync::Synchroniser
Constant Summary
GoogleCalendarDescription::DESCRIPTION_MIDDLE, GoogleCalendarDescription::DESCRIPTION_PREFIX, GoogleCalendarDescription::DESCRIPTION_SUFFIX
Instance Method Summary
collapse
Methods included from Timezone
#with_timezone
#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 Logging
#logger
#create_description, #extract_last_modified_date
Constructor Details
#initialize(facebook_calendar, google_calendar) ⇒ Synchroniser
Returns a new instance of Synchroniser.
14
15
16
17
|
# 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
|
Instance Method Details
#add_new_event(facebook_event) ⇒ Object
84
85
86
|
# 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) ⇒ Object
32
33
34
|
# 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 ⇒ Object
115
116
117
|
# 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 ⇒ Object
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.
122
123
124
|
# 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) ⇒ Object
100
101
102
|
# 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) ⇒ Object
96
97
98
|
# 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 ⇒ Object
19
20
21
|
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 19
def events
@events
end
|
#google_calendar ⇒ Object
23
24
25
|
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 23
def google_calendar
@google_calendar
end
|
#handle_google_event_found(facebook_event, google_event) ⇒ Object
75
76
77
78
79
80
81
82
|
# 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) ⇒ Object
66
67
68
69
70
71
72
73
|
# 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) ⇒ Object
92
93
94
|
# 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 ⇒ Object
27
28
29
30
|
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 27
def synchronise
synchronise_events
update_last_known_event_update
end
|
#synchronise_event(facebook_event) ⇒ Object
57
58
59
60
61
62
63
64
|
# 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 ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# 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) ⇒ Object
88
89
90
|
# 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 ⇒ Object
104
105
106
107
108
109
110
111
112
113
|
# 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) ⇒ Object
36
37
38
|
# File 'lib/facebook_google_calendar_sync/synchroniser.rb', line 36
def with_google_calendar_timezone target
with_timezone(target, google_calendar.timezone)
end
|