Fast, Lightweight and Minimalist
A fast lightweight and minimalist wrapper around the Google Calendar API.
[sudo] gem install 'google_calendar'
Important Changes: Google no longer supports the 'urn:ietf:wg:oauth:2.0:oob' out-of-band OAuth method. You must setup your OAuth Credentials with a publically accessible URL where you can grab your code from the URL paramaters after approving the OAuth request. If your product is a Web Application, you can automate this set up by pointing the redirect_url to the appropriate method of your application.
Obtain a Client ID and Secret
Find your calendar ID
require 'rubygems'
require 'google_calendar'
# Create an instance of the calendar.
cal = Google::Calendar.new(:client_id => YOUR_CLIENT_ID,
:client_secret => YOUR_SECRET,
:calendar => YOUR_CALENDAR_ID,
:redirect_url => YOUR_REDIRECT_URL # This must match a url you permitted in your OAuth setting
)
puts "Do you already have a refresh token? (y/n)"
has_token = $stdin.gets.chomp
if has_token.downcase != 'y'
# A user needs to approve access in order to work with their calendars.
puts "Visit the following web page in your browser and approve access."
puts cal.authorize_url
puts "\nCopy the code out of the paramters after Google redirects you to your provided redirect_url"
# Pass the ONE TIME USE access code here to login and get a refresh token that you can use for access from now on.
refresh_token = cal.login_with_auth_code( $stdin.gets.chomp )
puts "\nMake sure you SAVE YOUR REFRESH TOKEN so you don't have to prompt the user to approve access again."
puts "your refresh token is:\n\t#{refresh_token}\n"
puts "Press return to continue"
$stdin.gets.chomp
else
puts "Enter your refresh token"
refresh_token = $stdin.gets.chomp
cal.login_with_refresh_token(refresh_token)
# Note: You can also pass your refresh_token to the constructor and it will login at that time.
end
event = cal.create_event do |e|
e.title = 'A Cool Event'
e.start_time = Time.now
e.end_time = Time.now + (60 * 60) # seconds * min
end
puts event
event = cal.find_or_create_event_by_id(event.id) do |e|
e.title = 'An Updated Cool Event'
e.end_time = Time.now + (60 * 60 * 2) # seconds * min * hours
end
puts event
# All events
puts cal.events
# Query events
puts cal.find_events('your search string')
This sample code is located in readme_code.rb in the root folder.
The current google_calendar gem supports Ruby 3.2 and higher — the signet OAuth2 dependency requires it. Older Ruby versions (1.8.7 through 2.7) are maintained on different branches.
The first time you run rake test Rake will copy over .env.test to .env for use by Dotenv. You can also use .env.default as your own starting point, just remember to copy it over to .env before running tests.
You can modify .env with your own credentials and don't worry about accidentally committing to the repo as .env is in the .gitignore.
Copyright (c) 2010-2022 Northworld, LLC. See LICENSE.txt for further details.