<?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>Perplexed Labs &#187; twitter</title>
	<atom:link href="http://blog.perplexedlabs.com/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.perplexedlabs.com</link>
	<description>web development war stories from the frontlines to the backend</description>
	<lastBuildDate>Mon, 16 May 2011 14:19:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Twitter and SD News</title>
		<link>http://blog.perplexedlabs.com/2009/07/31/twitter-and-sd-news/</link>
		<comments>http://blog.perplexedlabs.com/2009/07/31/twitter-and-sd-news/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 17:24:28 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[hacker news]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=336</guid>
		<description><![CDATA[Motivation: SD News is a "social news site" (basically a Hacker News clone), written in Rails, that I work on as part of my efforts with a Christian publishing company I run with some friends.  As part of the administrative backend, I wanted to be able to send posts to our Twitter profile.  The site [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/02/26/flickr-rss-and-ruby/' rel='bookmark' title='Permanent Link: Flickr, RSS, and Ruby'>Flickr, RSS, and Ruby</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/06/01/be-language-agnostic-solve-the-problem/' rel='bookmark' title='Permanent Link: Be Language Agnostic &#8211; Solve the Problem!'>Be Language Agnostic &#8211; Solve the Problem!</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/08/deployment-using-capistrano-and-webistrano-via-rails-and-phusion-passenger/' rel='bookmark' title='Permanent Link: Deployment Using Capistrano / Webistrano via Rails / Phusion Passenger'>Deployment Using Capistrano / Webistrano via Rails / Phusion Passenger</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>Motivation:</strong> <a href="http://news.sensusdivinitatis.com">SD News</a> is a "social news site" (basically a Hacker News clone), written in Rails, that I work on as part of my efforts with a Christian publishing company I run with some friends.  As part of the administrative backend, I wanted to be able to send posts to our <a href="http://twitter.com/sdpub">Twitter profile</a>.  The site is still young, and the community still growing, so I wanted the admins to have complete control over what gets sent to Twitter.  I had thought of automating this process based on which items have the most votes in a given time period, but trust is easy to lose and all it would take is 1 or 2 irrelevant, or irreverent. posts to lose that trust.</p>
<p><strong>Methodology:</strong> I would first need a good Twitter gem for Ruby, and I'd need to decide which URL shortening service I'd use.  <a href="http://twitter.rubyforge.org/">Ruby Twitter</a> seemed to be the simplest gem for Twitter.  For the URL shortening I chose bit.ly, because the <a href="http://github.com/philnash/bitly/tree/master">bitly gem</a> seemed like the easiest, and the documentation was good.  My plan of attack was:</p>
<ol>
<li>Grab one item from the queue, that has not been Twittered</li>
<li>Shorten the URL via bitly</li>
<li>Send the item's title and shortened URL to Twitter</li>
<li>Save the shortened URL in the database so I could retrieve stats later</li>
</ol>
<p>The script that did this would be run every hour.<br />
<strong><br />
Implementation: </strong>The two gems made this an almost trivial implementation.</p>
<pre class="brush: ruby; title: ;">
require 'twitter'
require 'bitly'

@item = Item.find(:first, :conditions =&gt; &quot;send_to_twitter = 1 and twitterd = 0&quot;, :order =&gt; &quot;posted_on desc&quot;)

if !@item.nil?
   b = Bitly.new(username, password)
   @url = b.shorten(&quot;http://news.sensusdivinitatis.com/item/#{@item.id}&quot;).short_url

   httpauth = Twitter::HTTPAuth.new(username, password)
   base = Twitter::Base.new(httpauth)
   base.update(&quot;#{@item.title[0...110]} - #{@url}&quot;) #shorten the title if it's too long

   Item.update(@item.id, :twitterd =&gt; 1, :bitly_url =&gt; @url) #save the bit.ly url
end
</pre>
<p>That's pretty much it.  Incidentally, the bitly gem makes it very easy to grab the stats for any URL.  For instance, if you wanted to see how many clicks a given URL has received:</p>
<pre class="brush: ruby; title: ;">
require 'twitter'
require 'bitly'

@item = Item.find(id)
b = Bitly.new(username, password)
@clicks = b.stats(i.bitly_url).stats[&quot;clicks&quot;]
</pre>
<p>Done.</p>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/02/26/flickr-rss-and-ruby/' rel='bookmark' title='Permanent Link: Flickr, RSS, and Ruby'>Flickr, RSS, and Ruby</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/06/01/be-language-agnostic-solve-the-problem/' rel='bookmark' title='Permanent Link: Be Language Agnostic &#8211; Solve the Problem!'>Be Language Agnostic &#8211; Solve the Problem!</a></li>
<li><a href='http://blog.perplexedlabs.com/2010/02/08/deployment-using-capistrano-and-webistrano-via-rails-and-phusion-passenger/' rel='bookmark' title='Permanent Link: Deployment Using Capistrano / Webistrano via Rails / Phusion Passenger'>Deployment Using Capistrano / Webistrano via Rails / Phusion Passenger</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2009/07/31/twitter-and-sd-news/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

