Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sync functionality #296

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/google/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
#
Expand Down
19 changes: 19 additions & 0 deletions lib/google/synchronize.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/google_calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module Google
require 'google/connection'
require 'google/event'
require 'google/freebusy'
require 'google/synchronize'
end