<?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>Mundo Ruby &#187; twitter</title>
	<atom:link href="http://www.mundoruby.com.ar/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mundoruby.com.ar</link>
	<description>Ruby Artists, Hackers y otras yerbas ...</description>
	<lastBuildDate>Wed, 12 Aug 2009 23:02:13 +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>rails twitter search using search.atom</title>
		<link>http://www.mundoruby.com.ar/2009/07/23/rails-twitter-search-using-search-atom/</link>
		<comments>http://www.mundoruby.com.ar/2009/07/23/rails-twitter-search-using-search-atom/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 00:08:00 +0000</pubDate>
		<dc:creator>FreedomCoder</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.mundoruby.com.ar/?p=155</guid>
		<description><![CDATA[
Recently, I had to create a twitter live feed for a work I&#8217;m doing&#8230;. so I found the gems for searching in twitter, but all of them were working with the search.json api, but since my client wanted the same results that he saw in twitter.search.com, I created a lib for that project (following the [...]]]></description>
			<content:encoded><![CDATA[<p>
<p>Recently, I had to create a twitter live feed for a work I&#8217;m doing&#8230;. so I found the gems for searching in twitter, but all of them were working with the search.json api, but since my client wanted the same results that he saw in <a href="http://twitter.search.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://twitter.search.com');" target="_blank">twitter.search.com</a>, I created a lib for that project (following the gem example code), in order to use the search.atom api and parse the xml results with hpricot.</p>
<p>If you deal with the same problem, here you have the code</p>
<p>require &#8216;rubygems&#8217;<br />
require &#8216;net/http&#8217;<br />
require &#8216;cgi&#8217;</p>
<p>class Twitter<br />
TWITTER_SEARCH_API_URL = &#8216;http://search.twitter.com/search.atom&#8217;<br />
DEFAULT_TIMEOUT = 5<br />
HEADERS = { &#8220;Content-Type&#8221; =&gt; &#8216;application/rss+xml&#8217;,<br />
&#8220;User-Agent&#8221;   =&gt; &#8216;twitter-search&#8217; }</p>
<p>def self.search(opts)<br />
search_for = &#8216;#twitter&#8217;</p>
<p>url = URI.parse(TWITTER_SEARCH_API_URL)<br />
url.query = sanitize_query(opts)</p>
<p>ensure_no_location_operators(url.query)</p>
<p>req  = Net::HTTP::Get.new(url.path)<br />
http = Net::HTTP.new(url.host, url.port)<br />
http.read_timeout = DEFAULT_TIMEOUT</p>
<p>res = http.start { |h| h.get(&#8221;#{url.path}?#{url.query}&#8221;, HEADERS) }</p>
<p>if res.code == &#8216;404&#8242;<br />
raise &#8220;Twitter responded with a 404 for your query&#8221;<br />
end</p>
<p>self.parse_search(res.body)<br />
end</p>
<p>def self.sanitize_query(opts)<br />
if opts.is_a? String<br />
&#8220;q=#{CGI.escape(opts)}&#8221;<br />
elsif opts.is_a? Hash<br />
&#8220;#{sanitize_query_hash(opts)}&#8221;<br />
else<br />
raise &#8220;sanitique_query expects a String or a Hash&#8221;<br />
end<br />
end</p>
<p>def self.sanitize_query_hash(query_hash)<br />
query_hash.collect{|key, value| &#8220;#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}&#8221; }.join(&#8217;&amp;&#8217;)<br />
end</p>
<p>def self.ensure_no_location_operators(query_string)<br />
if query_string.include?(&#8221;near%3A&#8221;) ||<br />
query_string.include?(&#8221;within%3A&#8221;)<br />
raise &#8220;near: and within: are available from the Twitter Search web interface, but not the API. The API requires the geocode parameter. See dancroak/twitter-search README.&#8221;<br />
end<br />
end</p>
<p>def self.parse_search(body)<br />
doc = Hpricot.parse(body)</p>
<p>entries = []</p>
<p>items = (doc/:entry)<br />
items.each do |raw_item|<br />
entry = {}</p>
<p>entry</p>
</p>
<p>(Via <a href="http://www.rorra.com.ar">Rorra&#8217;s blog</a>.) Original Link: <a href="http://www.rorra.com.ar/2009/07/22/rails-twitter-search-using-searchatomrails-busquedas-en-twitter-usando-searchatom/#comments">rails twitter search using search.atom</a> </p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7949681675937032";
google_ad_slot = "0874687580";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mundoruby.com.ar/2009/07/23/rails-twitter-search-using-search-atom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

