Fetching and caching Google Calendar data in Rails

Estimated reading time: 3 minutes

Table of contents

Recently we had a requirement to show Google Calendar data for a client on a Rails intranet project. The requirements were a bit outside what’s offered by the embedded iFrame option so we needed a better solution to retrieve Google calendar data.

Options for getting data from Google Calendar

The simplest option is just to use the iFrame that Google provide that is accessible from your Google Calendar under Settings > Calendars > Name of your Calendar > Embed This Calendar. You don’t really get to customise this though so what you see is what you get

If you want the raw data so you can do things with it Google publish the gdata-ruby-util Ruby library which provides an interface into Google’s Data APIs. This is a very useful gem, providing full access to lots of data across Google services. From a Ruby perspective it seemed like you need to write a lot of code for a simple API call.

Better for our requirments was the GCal4Ruby gem that provides a clean interface to Google Calendar data. It works for both Google and Hosted Accounts and with just six lines of ruby you can get event data for a particular calendar:

Caching

The API call takes a while to come back so we don’t really want the user experience to be impacted by slow load times. We can use Rails caching to store the response and only fetch it when we need to. We put this into a rake task so that we could call it using a cron script to keep the calendar up to date

Tags

Recent Posts

Using HashiCorp Vault with LDAPHow to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users

Linux and Unix xargs command tutorial with examplesTutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.

Copy a file in GoHow to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.

About the author

George Ornbo is a hacker, futurist, blogger and Dad based in Buckinghamshire, England.