Skip to content

Instantly share code, notes, and snippets.

@narath
Last active May 13, 2024 15:06
Show Gist options
  • Save narath/ec0d35fe93e31e92a597ab9342fb0c8b to your computer and use it in GitHub Desktop.
Save narath/ec0d35fe93e31e92a597ab9342fb0c8b to your computer and use it in GitHub Desktop.
Use Google Calendar API in Jumpstart Pro Rails
require "google/apis/calendar_v3"
# To add the Google Calendar API to your project, you need to add the following line to your Gemfile:
# bundle add google-apis-calendar_v3
#
# Google Cloud Console Credential Settings:
# Authorized JavaScript origins: http://localhost:3000
# Authorized redirect URIs: http://localhost:3000/users/auth/google_oauth2/callback
# Update the above with your staging and production URLs
#
# In your credentials file, updated omniauth.google_oauth2 with your client id (public_key) and client secret (private_key)
# and the following scopes:
# scope: 'profile,calendar.events'
class GoogleCalendarService
Calendar = Google::Apis::CalendarV3
def initialize(user)
@user = user
@connected_account =
user.connected_accounts.find_by(provider: "google_oauth2")
if connected?
@calendar = Calendar::CalendarService.new
@calendar.authorization = @connected_account.token
end
end
def connected?
@connected_account.present?
end
def create(summary, start_time, end_time, description: nil, attendees: [])
raise "Not connected" unless connected?
event = Calendar::Event.new(
summary: summary,
start: { date_time: start_time.iso8601 },
end: { date_time: end_time.iso8601 },
description: description,
attendees: attendees.map { |email| { email: email } }
)
@calendar.insert_event("primary", event)
end
# list events from the user's primary calendar
# if time_min is not specified, it will show upcoming events
def list(time_min: nil, time_max: nil, limit: 1000) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
raise "Not connected" unless connected?
result = []
page_token = nil
time_min ||= Time.now
puts "time_min: #{time_min}"
puts "time_max: #{time_max}"
while limit.positive?
events =
@calendar.list_events(
"primary",
max_results: [limit, 100].min,
single_events: true,
order_by: "startTime",
time_min: time_min ? time_miniso8601 : nil,
time_max: time_max ? time_max.iso8601 : nil,
page_token: page_token,
fields: "items(id,summary,start,end,description,organizer,attendees,reminders,htmlLink),next_page_token"
)
result += events.items if events.items.present?
limit -= events.items.size
page_token = events.next_page_token
break unless page_token.present?
end
result
end
# gets all the events for today
def today
Time.use_zone(@user.time_zone) do
list(
time_min: Time.now.beginning_of_day,
time_max: Time.now.end_of_day
)
end
end
# gets all the events for tomorrow
def tomorrow
Time.use_zone(@user.time_zone) do
list(
time_min: Time.now.tomorrow.beginning_of_day,
time_max: Time.now.tomorrow.end_of_day
)
end
end
def this_week
Time.use_zone(@user.time_zone) do
list(
time_min: Time.now.beginning_of_week,
time_max: Time.now.end_of_week
)
end
end
def next_week
Time.use_zone(@user.time_zone) do
list(
time_min: Time.now.next_week.beginning_of_week,
time_max: Time.now.next_week.end_of_week
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment