Skip to content

Instantly share code, notes, and snippets.

@imouaddine
Last active July 19, 2018 18:05
Show Gist options
  • Save imouaddine/12a8d276a16ae05bc407013d20b12803 to your computer and use it in GitHub Desktop.
Save imouaddine/12a8d276a16ae05bc407013d20b12803 to your computer and use it in GitHub Desktop.
require 'google/apis/calendar_v3'
class GoogleCalendarController < ApplicationController
def redirect
client = Signet::OAuth2::Client.new(client_options)
redirect_to client.authorization_uri.to_s
end
def callback
client = Signet::OAuth2::Client.new(client_options)
client.code = params[:code]
response = client.fetch_access_token!
session[:authorization] = response
redirect_to calendars_url
end
def index
client = Signet::OAuth2::Client.new(client_options)
client.update!(session[:authorization])
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client
@calendar_list = service.list_calendar_lists
end
def events
client = Signet::OAuth2::Client.new(client_options)
client.update!(session[:authorization])
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client
existing_event = service.get_event(params[:calendar_id], "f2cep271crog3if11tg58adnns_20180829T160000Z")
existing_event.summary = "Google Calendar IO 2018"
service.update_event(params[:calendar_id], "f2cep271crog3if11tg58adnns_20180829T160000Z", existing_event)
@event_list = service.list_events(
params[:calendar_id],
max_results: 120,
single_events: true,
order_by: 'startTime',
time_min: 10.days.from_now.iso8601,
)
end
def update_event
service.update_event("f2cep271crog3if11tg58adnns_20180829T160000Z", event)
end
def create_event
service.insert_event(params[:calendar_id], event)
end
def event
Google::Apis::CalendarV3::Event.new({
summary: 'Google I/O 2018',
location: '800 Howard St., San Francisco, CA 94103',
description: 'A chance to hear more about Google\'s developer products.',
start: {
date_time: '2018-08-10T09:00:00-07:00',
time_zone: 'America/Los_Angeles',
},
end: {
date_time: '2018-08-10T17:00:00-07:00',
time_zone: 'America/Los_Angeles',
},
recurrence: [
'RRULE:FREQ=DAILY;COUNT=2'
],
attendees: [
{ email: 'lpage@example.com' },
{ email: 'sbrin@example.com' },
],
reminders: {
use_default: true,
},
})
end
private
def client_options
{
client_id: ENV.fetch("LC_GOOGLEOAUTH_CLIENT_ID"),
client_secret: ENV.fetch("LC_GOOGLEOAUTH_CLIENT_SECRET"),
authorization_uri: 'https://accounts.google.com/o/oauth2/auth',
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
scope: Google::Apis::CalendarV3::AUTH_CALENDAR,
redirect_uri: calendar_callback_url,
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment