<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rel=me &#187; ics</title>
	<atom:link href="http://rel.me/t/ics/feed/" rel="self" type="application/rss+xml" />
	<link>http://rel.me</link>
	<description>programming, objective-c, cocoa, iphone, c</description>
	<lastBuildDate>Mon, 02 Aug 2010 20:09:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Creating iCalendar files in Ruby</title>
		<link>http://rel.me/2007/02/16/creating-icalendar-files-in-ruby/</link>
		<comments>http://rel.me/2007/02/16/creating-icalendar-files-in-ruby/#comments</comments>
		<pubDate>Fri, 16 Feb 2007 06:51:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[greader]]></category>
		<category><![CDATA[ical]]></category>
		<category><![CDATA[icalendar]]></category>
		<category><![CDATA[ics]]></category>

		<guid isPermaLink="false">/2007/09/03/creating-icalendar-files-in-ruby</guid>
		<description><![CDATA[Using the iCalendar ruby library (gem install icalendar): From the Show model, I added a method to create an event: 1 2 3 4 5 6 7 8 9 10 11 class Show &#60; ActiveRecord::Base ... def to_ical_event event = Icalendar::Event.new event.dtstart = time ? time.to_datetime : date event.dtstamp = created_at.to_datetime event.summary = &#34;The title&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>Using the <a href="http://icalendar.rubyforge.org/">iCalendar</a> ruby library (gem install icalendar):<br/><br />
From the Show model, I added a method to create an event:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">class</span> <span class="cl">Show</span> &lt; <span class="co">ActiveRecord</span>::<span class="co">Base</span><tt>
</tt>...<tt>
</tt>  <span class="r">def</span> <span class="fu">to_ical_event</span><tt>
</tt>    event = <span class="co">Icalendar</span>::<span class="co">Event</span>.new    <tt>
</tt>    event.dtstart = time ? time.to_datetime : date<tt>
</tt>    event.dtstamp = created_at.to_datetime<tt>
</tt>    event.summary = <span class="s"><span class="dl">&quot;</span><span class="k">The title</span><span class="dl">&quot;</span></span><tt>
</tt>    event.description = <span class="s"><span class="dl">&quot;</span><span class="k">The description</span><span class="dl">&quot;</span></span><tt>
</tt>    event.uid = <span class="s"><span class="dl">&quot;</span><span class="k">show-</span><span class="il"><span class="idl">#{</span>id<span class="idl">}</span></span><span class="dl">&quot;</span></span>   <tt>
</tt>  <span class="r">end</span><tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<ul>
<li><code>dtstart</code> takes a Date or DateTime object (see Ruby cookbook recipe chapter 3.9 for to_datetime method).
</ul>
<p>From the Venue model, I added a method to create the calendar and add all the show events:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">class</span> <span class="cl">Venue</span> &lt; <span class="co">ActiveRecord</span>::<span class="co">Base</span><tt>
</tt>  has_many <span class="sy">:shows</span>  <tt>
</tt>...<tt>
</tt>  <span class="r">def</span> <span class="fu">to_ical</span><tt>
</tt>    ical = <span class="co">Icalendar</span>::<span class="co">Calendar</span>.new<tt>
</tt>    ical.product_id = <span class="s"><span class="dl">&quot;</span><span class="k">-//dclicio.us//iCal 1.0//EN</span><span class="dl">&quot;</span></span><tt>
</tt>    ical.custom_property(<span class="s"><span class="dl">&quot;</span><span class="k">X-WR-CALNAME;VALUE=TEXT</span><span class="dl">&quot;</span></span>, name)<tt>
</tt>    ical.custom_property(<span class="s"><span class="dl">&quot;</span><span class="k">X-WR-TIMEZONE;VALUE=TEXT</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">US/Eastern</span><span class="dl">&quot;</span></span>)<tt>
</tt>    shows.each <span class="r">do</span> |show|<tt>
</tt>      event = show.to_ical_event<tt>
</tt>      ical.add_event(event)<tt>
</tt>    <span class="r">end</span><tt>
</tt>    ical<tt>
</tt>  <span class="r">end</span><tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<ul>
<li>The <code>X-WR</code> custom properties are read by iCal and by google calendar.
</ul>
<p>Then a task to iterate through the venues creating the ics files:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="co">Venue</span>.find(<span class="sy">:all</span>, <span class="sy">:include</span> =&gt; [ <span class="sy">:shows</span> ]).each <span class="r">do</span> |venue|<tt>
</tt>  ical = venue.to_ical       <tt>
</tt>  file = <span class="co">File</span>.new(<span class="s"><span class="dl">&quot;</span><span class="k">/tmp/</span><span class="il"><span class="idl">#{</span>venue.name<span class="idl">}</span></span><span class="k">.ics</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">w</span><span class="dl">&quot;</span></span>)<tt>
</tt>  file.write(ical.to_ical) <span class="c"># Allow myself to ... introduce ... myself :/</span><tt>
</tt>  file.close<tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>An ics file looks like:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">BEGIN:VCALENDAR<tt>
</tt>X-WR-CALNAME;VALUE=TEXT:Black cat<tt>
</tt>VERSION:2.0<tt>
</tt>CALSCALE:GREGORIAN<tt>
</tt>PRODID:-//dclicio.us//iCal 1.0//EN<tt>
</tt>X-WR-TIMEZONE;VALUE=TEXT:US/Eastern<tt>
</tt>BEGIN:VEVENT<tt>
</tt>UID:dclicio.us-243<tt>
</tt>DESCRIPTION:with \nhttp://dclicio.us<tt>
</tt>SUMMARY:<tt>
</tt>DTSTART:20070117<tt>
</tt>DTSTAMP:040425<tt>
</tt>SEQ:0<tt>
</tt>END:VEVENT<tt>
</tt>END:VCALENDAR</pre>
</td>
</tr>
</table>
<p>I then surface these files in public/ical/ and add a <code>ProxyPass /ical !</code> so apache can serve em up straight without hitting mongrel.<br/><br />
Check out <a href="http://dclicio.us">dclicio.us</a> to see it in action.</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/02/16/creating-icalendar-files-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
