Archive for the 'Rails' Category

Using Google Calendar API in Ruby on Rails

Update: I’ve got a new Ruby Gem that improves upon the functionality described below.  Check it out here.

Update 2: Rewrote this article to use GCal4Ruby.

A new project of mine involves project managment and milestones.  One feature I was interested in was natively integrating the Rails app with Google Calendar so that the milestone information would be available to anyone who wanted to subscribe to it.  I looked around and found a couple of google calendar integration libraries, but suprisingly there isn’t a fully implemented GData API that allow access to all the features of the Google Calendar API.

  • Google Calendar: a pretty simple and full featured google calendar library in ruby, with optional Rails plugin.  A good option for anyone who’s interested in accessing or exporting Google Calendars into HTML or Excel.  Though the Event support is excellent, my application needed finer grained control over multiple calendars under each user, and this library didn’t include the functions to determine calendars programmatically.
  • GCalAPI: also a good and simple API for Google Calendar, though it provided a better interface for finding calendars associated with a user account.  Unfortunately, the documentation is a little lacking, and there was no built in way to create a new calendar.  But, I ended up using this library despite the drawbacks.

These are two very good libraries, but haven’t been updated in a couple of years, and were missing some key pieces of functionality that I wanted.  So, I’ve created a new Ruby library (available as a Gem) called GCal4Ruby that implements all of the major features of the GCal API, including recurrence, attendees and reminders.  One of the main improvements in GCal4Ruby over the other two libraries is that you don’t have to remember the ‘feed’ urls for Google; all that is handled transparently for you.  This article describes how to use GCal4Ruby to interact with Google Calendars through Ruby on Rails.  Be sure to read the documentation for all available functions and features.

Setup
Setting up the GCal4Ruby gem is straightforward: just install it using sudo gem install gcal4ruby or by adding gem.config ‘gcal4ruby’ line to your Rails project environment.rb file. Then, all you need to do is run rake gems:install to install the gem through rails.

My application includes a ‘project’ model and a ‘milestone’ model.  Each project has a unique google calendar associated with it, though they all appear under one account.  You can either use a Google Gmail account for this, or you can your own Google Apps account if you have one.  You’ll probably want to add your username and password to the environment.rb file as static constants for reference later.

Creating a Calendar
The one thing that the GCalAPI library can’t do by default is create a calendar programmatically.  GCal4Ruby includes the ability to do this.  Here’s how to create a new public calendar from within a Calendar model in Rails:


#Step 1: Setup feed url and create account service using constants
service = GCal4Ruby::Service.new
service.authenticate(GOOGLE_LOGIN, GOOGLE_PASS)

#Step 2: Create a new calendar
calendar = GCal4Ruby::Calendar.new(service)
calendar.title = self.title # tile = 'A New Calendar'
calendar.summary = self.description
if calendar.save
 return true
else
 return false
end

#Step 3: Make the calendar publicly available
calendar.public = true

Adding an Event
Adding an event is straightforward.  Basically, its a three step process:

  1. Authenticate and load your Google Calendar Account
  2. Load the specific calendar you’d like to work with
  3. Construct and add your event to the selected calendar

I added this code to the milestone model which is within the save method.  Here’s the basic code for accomplishing the three steps outlined above:

#Step 1: Setup feed url and create account service using constants
service = GCal4Ruby::Service.new
service.authenticate(GOOGLE_LOGIN, GOOGLE_PASS)

#Step 2: Load calendar data for the calendar associated with the project
calendar = GCal4Ruby::Calendar.find(service, 'A Test Calendar', :first)

#Step 3: create a new event and save
event = GCal4Ruby::Event.new(calendar)
event.title = self.title
event.content = self.description
event.where = "N/A"
event.start = self.start_date
event.end = self.end_date || self.start_date
event.all_day = self.all_day_event
if !event.save
  return false
end