Email to Ruby script to google calendar

We receive notifications at the office from the iDEAL payment system in the Netherlands when there is planned maintenance or disturbances. It’s pretty difficult to visualise the downtime in the text form we get it in, so I thought it would be a good idea to try and get it into a calendar. It was surprisingly easy to accomplish, so I thought I would share a basic version of the script for anyone to use.

Setup

You will need Ruby (I was using 1.9.2-p180 via RVM) the googlecalendar gem the mail gem and optionally nokogiri if you want to parse a multipart html message.

You can install all with: gem install googlecalendar mail nokogiri

You will need to add to your /etc/aliases file (for postfix) for other MTA’s please RTFM:

scriptaddress: “| /path/to/script.rb”

Where script address is the address where the message should be emailed to.

The skeleton script is below:

#!/usr/local/bin/ruby
require 'rubygems'
require 'nokogiri'
require 'googlecalendar'
require 'time'
require 'mail'

include Googlecalendar

#read the email in
email = Mail.new(STDIN.read)

#if it's a multipart message then email.parts[1].body.to_s will give me the HTML
#I can then use Nokogiri to give me a HTML doc
#html_doc = Nokogiri::HTML(email.parts[1].body.to_s)

g = GData.new
#logon to my account and fetch a token
token = g.login('danny.hawkins@gmail.com', 'yourpassword')

event = {
  :title      => email.subject,
  :content    => email.body,
  :author     => "dannyhawkins",
  :email      => "danny.hawkins@gmail",
  :where      => "London",
  :startTime  => "2011-03-10T15:00:00.000+00:00", #+00:00 means GMT +01:00 would be CET etc
  :endTime    => "2011-03-10T16:00:00.000+00:00"
}
#post my event
response = g.new_event(event)
#post my event to a different calendar
#response = g.new_event(event, 'calendar name')
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • StumbleUpon
  • Twitter

About Danny

Primarily a Ruby On Rails developer, but I can do other cool stuff too.
This entry was posted in Development, Ruby / Rails. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>