Ā¶ ā
Google CalendarA fast lightweight and minimalist wrapper around the Google Calendar api.
<img src=āhttps://badge.fury.io/rb/google_calendar.svgā alt=āGem Versionā /> <img src=āhttps://travis-ci.org/northworld/google_calendar.png?branch=masterā alt=āBuild Statusā /> <img src=āhttps://codeclimate.com/github/northworld/google_calendar/badges/gpa.svgā /> <img src=āhttps://codeclimate.com/github/northworld/google_calendar/badges/coverage.svgā />
Ā¶ ā
Install[sudo] gem install 'google_calendar'
Ā¶ ā
SetupImportant 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
-
Go to the Google Developers Console.
-
Select a project, or create a new one (at the top of the page).
-
In the sidebar on the left, select Library.
-
Type in āGoogle Calendarā in the search box and click on āGoogle Calendar APIā in the results.
-
Click on the āEnableā link at the top of the page.
-
In the sidebar on the left, select Credentials.
-
If you havenāt done so already, create your āOAuth client IDā by clicking āCreate Credentials -> OAuth client IDā. Choose Web Application as the type.
-
You must also provide an āAuthorized redirect URIsā which is a publically reachable URL where google will redirect to after the OAuth approval. Google will append the paramater ācode=<your auth code>ā to this URL during redirection which will include the necessary code to authenticate the usage of this application.
-
Take note of the Client ID and Client Secret as youāll need to add it to your code later.
-
In the sidebar on the left, select āOAuth consent screenā. You must setup your Application Name, and add āTest usersā if this is for personal use (i.e. you are setting up links to at limited set of known usersā calendars), or āPublishā your app if this is for public use.
Find your calendar ID
-
Visit Google Calendar in your web browser.
-
In the calendar list on the left, click the three vertical dots next to the appropriate calendar, then select āSettings and sharingā.
-
From the left toolbar, choose āIntegrate Calendarā.
-
In the Integrate Calendar section, locate the Calendar ID at the top of the section.
-
Copy the Calendar ID.
Ā¶ ā
Usage (readme_code.rb)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.
Ā¶ ā
Ruby SupportThe current google_calendar gem supports Ruby 2.1 and higher ā because of the json gem dependency. We maintain support for Ruby 1.8.7, 1.9.3 and 2.0 on different branches.
Ā¶ ā
Notes-
This is not a complete implementation of the calendar api, it just includes the features we needed to support our internal calendar integration. Feel free to add additional features and we will happily integrate them.
-
Did you get an SSL exception? If so take a look at this: gist.github.com/fnichol/867550
Ā¶ ā
Contributing to google_calendar-
Check out the latest master to make sure the feature hasnāt been implemented or the bug hasnāt been fixed yet
-
Check out the issue tracker to make sure someone already hasnāt requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I donāt break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Ā¶ ā
Running TestsThe 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
.
Ā¶ ā
CopyrightCopyright Ā© 2010-2022 Northworld, LLC. See LICENSE.txt for further details.