Thursday 16 October 2008

Outputting a calendar without a gem

Had some fun yesterday with an application I've been working on to track employee absences. One of the requirements we identified was to publish a calendar that could be viewed in Outlook, iCal, etc, showing who was off and when.

As I was using Ruby and Rails, my first thought was to use a gem. I found this one iCalendar — Internet calendaring, Ruby style and was soon publishing the calendar. Marvelous, well not quite.

After giving up trying to setup an internal Linux server to host this application I've been trying Mor.ph. As this was a test project I didn't want to take resources away from the our Ops team, big mistake.

Morph seems really easy to use, I love the batched release process with capistrano, however there was a problem. They don't have the iCalendar gem.

Again this is as much a test project as anything so it seemed over the top to try and get them to host it. It occurred to me that I could use an ActionView template to achieve the same thing.

Here it is:

BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
<% @absences.each do |absence| %>BEGIN:VEVENT
DTSTART:<%= absence.starts_at.to_datetime.strftime '%Y%m%dT%H%M%S' %>
DTEND:<%= absence.ends_at.to_datetime.strftime '%Y%m%dT%H%M%S' %>
SUMMARY:<%= absence.employee.name + ' - ' + absence.absence_type %>
END:VEVENT
<% end %>END:VCALENDAR

The code to render the view is dead simple:

@absences = Absence.find(:all)
headers["Content-Type"] = "text/calendar"
render

And being Ruby on Rails, I have a nice functional test that confirms the output.

Only tinge to the cloud is that I'm having a bit of a problem getting the timezone to appear in a format that Outlook recognises, any thoughts welcome.

No comments: