module FacebookGoogleCalendarSync::GoogleCalendarClient

Public Class Methods

configure() { |config| ... } click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 17
def self.configure
  @@config = OpenStruct.new
  yield @@config

  oauth_yaml = YAML.load_file(@@config.google_api_config_file)
  @@client = Google::APIClient.new({:application_name => "Facebook to Google Calendar Sync", :application_version => FacebookGoogleCalendarSync::VERSION})
  @@client.authorization.client_id = oauth_yaml["client_id"]
  @@client.authorization.client_secret = oauth_yaml["client_secret"]
  @@client.authorization.scope = oauth_yaml["scope"]
  @@client.authorization.refresh_token = oauth_yaml["refresh_token"]
  @@client.authorization.access_token = oauth_yaml["access_token"]

  if @@client.authorization.refresh_token && @@client.authorization.expired?
    @@client.authorization.fetch_access_token!
  end

  @@calendar_service = @@client.discovered_api('calendar', 'v3')
end

Public Instance Methods

add_event(calendar_id, event) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 56
def add_event calendar_id, event
  make_call :api_method => calendar_service.events.import,
    :parameters => {'calendarId' => calendar_id},
    :body_object => event
end
create_calendar(calendar_details) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 68
def create_calendar calendar_details
  make_call :api_method => calendar_service.calendars.insert,
    :parameters => {},
    :body_object => calendar_details
end
find_calendar_details_by_summary(calendar_summary) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 40
def find_calendar_details_by_summary calendar_summary
  get_calendar_list.items.find { | calendar | calendar.summary == calendar_summary && calendar.accessRole == 'owner'}
end
find_primary_calendar_details() click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 44
def find_primary_calendar_details
  get_calendar_list.items.find { | calendar | calendar.primary }
end
get_calendar(calendar_id) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 52
def get_calendar calendar_id
  make_call :api_method => calendar_service.events.list, :parameters => {'calendarId' => calendar_id}
end
get_calendar_list() click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 48
def get_calendar_list
  make_call :api_method => calendar_service.calendar_list.list
end
get_calendar_metadata(calendar_id) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 36
def get_calendar_metadata calendar_id
  make_call :api_method => calendar_service.calendars.get, :parameters => {'calendarId' => calendar_id}
end
update_calendar(calendar_id, calendar_details) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 74
def update_calendar calendar_id, calendar_details
  make_call :api_method => calendar_service.calendars.update,
    :parameters => {'calendarId' => calendar_id},
    :body_object => calendar_details
end
update_event(calendar_id, event_id, event) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 62
def update_event calendar_id, event_id, event
  make_call :api_method => calendar_service.events.update,
    :parameters => {'calendarId' => calendar_id, 'eventId' => event_id},
    :body_object => event
end

Private Instance Methods

calendar_service() click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 94
def calendar_service
  @@calendar_service
end
check_for_success(result, params) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 98
def check_for_success result, params
  raise SyncException.new(result.status.to_s + "\nResponse:" + result.body + "\nRequest: #{params}") unless result.status == 200
end
client() click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 90
def client
  @@client
end
make_call(params) click to toggle source
# File lib/facebook_google_calendar_sync/google_calendar_client.rb, line 82
def make_call params
  request = params.merge(:headers => {'Content-Type' => 'application/json'})
  logger.debug("Calling Google Calendar API with request #{request}")
  result = client.execute(request)
  check_for_success result, params
  result.data
end