diff --git a/lib/google/calendar.rb b/lib/google/calendar.rb index 83cb51b..14e28ab 100644 --- a/lib/google/calendar.rb +++ b/lib/google/calendar.rb @@ -222,6 +222,39 @@ def events event_lookup() end + # + # Find all of the events associated with this calendar with the intention + # of future incremental syncing. + # + # This method is not useful if you do not store the returned next_sync_token. + # + # Returns: + # a hash with a next_sync_token plus an empty [:events] array if nothing found. + # a hash with a next_sync_token plus an array with one element if only one event found. + # a hash with a next_sync_token plus an array of events if many found. + # + def initial_sync + sync_event_lookup('', nil, nil) + end + + # + # Find all new and updated events associated with this calendar using a + # sync_token. Use page tokens if the returned :events are paginated. + # + # This method is not useful if you do not store the returned + # next_sync_token if it does not equal the provided sync_token. + # + # Returns: + # a hash with a next_sync_token plus an empty [:events] array if nothing found. + # a hash with a next_sync_token plus an array with one element if only one event found. + # a hash with a next_sync_token plus an array of events if many found. + # a hash with a next_sync_token and page_token, plus an array of events if many found. + # + def incremental_sync(sync_token: '', page_token: nil) + page_token_string = page_token == nil ? '' : "&pageToken=#{page_token}" + sync_event_lookup("?syncToken=#{sync_token}#{page_token_string}", sync_token, page_token) + end + # # This is equivalent to running a search in the Google calendar web application. # Google does not provide a way to specify what attributes you would like to @@ -476,6 +509,19 @@ def event_lookup(query_string = '') #:nodoc: end end + # + # Utility method used to centralize sync event lookup. + # + def sync_event_lookup(query_string = '', next_sync_token, next_page_token) #:nodoc: + begin + response = send_events_request(query_string, :get) + parsed_json = JSON.parse(response.body) + sync_events = Synchronize.synchronize_hash(parsed_json, self) || {} + rescue Google::HTTPNotFound + return { events: [], next_sync_token: next_sync_token, next_page_token: next_page_token } + end + end + # # Utility method used to centralize event setup # diff --git a/lib/google/synchronize.rb b/lib/google/synchronize.rb new file mode 100644 index 0000000..19d32be --- /dev/null +++ b/lib/google/synchronize.rb @@ -0,0 +1,19 @@ +module Google + class Synchronize + # + # Convenience method used to build a hash for incremental sync. + # Includes events from a Google feed plus necessary synchronization tokens. + # + def self.synchronize_hash(response, calendar) + events = Event.build_from_google_feed(response, calendar) + unless events.empty? + events = events.length > 1 ? events : [events[0]] + end + result = { events: events } + result[:next_sync_token] = response['nextSyncToken'] if response['nextSyncToken'] + result[:next_page_token] = response['nextPageToken'] if response['nextPageToken'] + + return result + end + end +end \ No newline at end of file diff --git a/lib/google_calendar.rb b/lib/google_calendar.rb index feea625..50ce17c 100644 --- a/lib/google_calendar.rb +++ b/lib/google_calendar.rb @@ -6,4 +6,5 @@ module Google require 'google/connection' require 'google/event' require 'google/freebusy' + require 'google/synchronize' end