<?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>jimdrewes.blog &#187; code</title>
	<atom:link href="http://blog.jimdrewes.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jimdrewes.com</link>
	<description>The thoughts of Jim Drewes - all in one place</description>
	<lastBuildDate>Sat, 21 Mar 2009 04:39:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Easy daily twitter poster with twitter4r and rake</title>
		<link>http://blog.jimdrewes.com/2009/03/easy-daily-twitter-poster-with-twitter4r-and-rake/</link>
		<comments>http://blog.jimdrewes.com/2009/03/easy-daily-twitter-poster-with-twitter4r-and-rake/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 06:35:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://blog.jimdrewes.com/?p=29</guid>
		<description><![CDATA[So for TodaysCigar.com I wanted to tweet the day&#8217;s cigar each night, just as Woot does.  This way anyone following the @todayscigar twitter account could instantly see the daily posting, and look back at past days&#8217; cigars.  Fortunately, some members of the Ruby community have released Twitter gems &#8211; namely the Ruby Twitter Gem and [...]]]></description>
			<content:encoded><![CDATA[<p>So for <a title="TodaysCigar.com" href="http://www.todayscigar.com">TodaysCigar.com</a> I wanted to tweet the day&#8217;s cigar each night, just as Woot does.  This way anyone following the <a title="@todayscigar" href="http://twitter.com/todayscigar">@todayscigar</a> twitter account could instantly see the daily posting, and look back at past days&#8217; cigars.  Fortunately, some members of the Ruby community have released Twitter gems &#8211; namely the <a title="Ruby Twitter Gem" href="http://twitter.rubyforge.org/">Ruby Twitter Gem</a> and the <a title="Twitter4R" href="http://twitter4r.rubyforge.org/">Twitter4R</a> gem.  Both are extremely easy to use, but I opted for the Twitter4R gem.  The Ruby Twitter Gem is excellent, but it&#8217;ll track multiple twitter accounts and it wants you to run some migrations on your database.  I didn&#8217;t want to update my DB schema, so I went with twitter4r.  Once the twitter4r gem was installed, all I had to do was write up a quick rake task, then setup a cron job to fire it off every night after midnight.</p>
<p>So first was the installation of twitter4r&#8230;</p>
<blockquote><p>sudo gem install twitter4r</p></blockquote>
<p>Next, I created a new rake task in lib/tasks called twitter_poster.rake.</p>
<blockquote><p>desc &#8220;Posts the current days&#8217; cigar to the twitter account.&#8221;<br />
task :twitter_poster =&gt; :environment do<br />
end</p></blockquote>
<p>Next, I added a couple of lines to my various environment files (development.rb, etc) to indicate my login/password for my dev and production twitter accounts.  This way my passwords won&#8217;t be floating around in the code, and it&#8217;s easy to test on multiple environments.</p>
<blockquote><p>ENV["TWITTER_ACCOUNT"] = &#8220;login_name&#8221;<br />
ENV["TWITTER_PASSWORD"] = &#8220;password&#8221;</p></blockquote>
<p>Going back to the twitter_poster.rake file, I included the twitter4r gem, and was able to set up a new Twitter client and post my first status.</p>
<blockquote><p>desc &#8220;Posts the current days&#8217; cigar to the twitter account.&#8221;<br />
task :twitter_poster =&gt; :environment do<br />
require &#8216;twitter&#8217;</p>
<p>client = Twitter::Client.new(:login =&gt; ENV['TWITTER_ACCOUNT'], :password =&gt; ENV['TWITTER_PASSWORD'])</p>
<p>new_message = client.status(:post, &#8220;Welcome to the TodaysCigar.com Twitter feed.&#8221;)<br />
end</p></blockquote>
<p>Throwing in some code to actually pull the current cigar, and I&#8217;ve got a workable rake task.</p>
<blockquote><p>desc &#8220;Posts the current days&#8217; cigar to the twitter account.&#8221;<br />
task :twitter_poster =&gt; :environment do<br />
require &#8216;twitter&#8217;</p>
<p>@product = Product.todays_item<br />
if (@product.nil?)<br />
@product = Product.default_item<br />
end</p>
<p>client = Twitter::Client.new(:login =&gt; ENV['TWITTER_ACCOUNT'], :password =&gt; ENV['TWITTER_PASSWORD'])</p>
<p>new_message = client.status(:post, &#8220;Today&#8217;s cigar is the #{@product.title}, for only $#{@product.price}.  http://www.todayscigar.com&#8221;)</p>
<p>puts &#8220;Twitter posting complete.&#8221;<br />
end</p></blockquote>
<p>Then all I had to do was add an entry to /etc/crontab to kick off the rake task each night.</p>
<p>Thank you, Twitter4R!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jimdrewes.com/2009/03/easy-daily-twitter-poster-with-twitter4r-and-rake/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

