<?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>Radamanthus Batnag &#187; Ruby</title>
	<atom:link href="http://rad.batnag.org/category/ruby-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://rad.batnag.org</link>
	<description>Random thoughts on web and indie game development.</description>
	<lastBuildDate>Sun, 31 Jul 2011 09:22:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Ruby Hacking Secrets From Barney Stinson</title>
		<link>http://rad.batnag.org/2011/04/ruby-hacking-secrets-from-barney-stinson/</link>
		<comments>http://rad.batnag.org/2011/04/ruby-hacking-secrets-from-barney-stinson/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 15:39:27 +0000</pubDate>
		<dc:creator>rad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://rad.batnag.org/?p=162</guid>
		<description><![CDATA[Introduction Yup, that guy from How I Met Your Mother. Ever wonder how he manages to hook up with pretty girls so easily? Is it the suit? The money? The good looks? Actually, his secret is his Ruby ninja skillz. I&#8217;m sure you&#8217;re familiar with the adage &#8220;Success is being in the right place at [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>Yup, that guy from How I Met Your Mother. Ever wonder how he manages to hook up with pretty girls so easily? Is it the suit? The money? The good looks? Actually, his secret is his Ruby ninja skillz.</p>
<p>I&#8217;m sure you&#8217;re familiar with the adage &#8220;Success is being in the right place at the right time&#8221;. For the purposes of this discussion, success means getting to hook up with a pretty girl. The right place is right beside the target girl, whoever she is. The right time is when she&#8217;s lonely, desperate, or horny. </p>
<p>How does Barney find the right place and the right time? You guessed it right, by stalking them on Facebook.</p>
<p>But Barney doesn&#8217;t use the lame Facebook search form. That&#8217;s too inefficient for him. He uses Ruby, the Facebook API, and the awesome <a href="https://github.com/nov/fb_graph" onclick="pageTracker._trackPageview('/outgoing/github.com/nov/fb_graph?referer=');">fb_graph</a> gem. In this article, I&#8217;ll give you a peek at the awesome Facebook stalking and searching you can do using these three tools.</p>
<p>SETUP</p>
<p>I&#8217;m assuming you already have Ruby 1.9.2 installed. MRI, <a href="http://www.macruby.org/" onclick="pageTracker._trackPageview('/outgoing/www.macruby.org/?referer=');">MacRuby</a>, whatever, as long as it implements 1.9.2 and you run it using <a href="https://rvm.beginrescueend.com/" onclick="pageTracker._trackPageview('/outgoing/rvm.beginrescueend.com/?referer=');">RVM</a>.</p>
<p>After that, all you really need is the fb_graph gem:</p>
<div class="code">
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> fb_graph</pre></td></tr></table></div>

<p></code>
</div>
<p><strong>Searching Facebook Using fb_graph</strong></p>
<div class="code">
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">irb
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'fb_graph'</span></pre></td></tr></table></div>

<p></code>
</div>
<p>Let&#8217;s search for the word &#8216;lonely&#8217;.</p>
<div class="code">
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">results = <span style="color:#6666ff; font-weight:bold;">FbGraph::Searchable</span>.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'lonely'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p></code>
</div>
<p>Yep, it&#8217;s that easy! What you get is an Array of Hashes, each hash containing a search result. </p>
<p>After taking a closer look at the results, you probably don&#8217;t like most of what you got &#8211; most are people who &#8220;Liked&#8221; Justin Bieber&#8217;s recent song, &#8220;One Less Lonely Girl&#8221;.</p>
<p>So let&#8217;s refine our search a little. Let&#8217;s get only search for Facebook statuses. Yep, that&#8217;s most likely what we want &#8211; girls who have out &#8220;I&#8217;m lonely&#8221; in their status.</p>
<div class="code">
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">results = FbGraph::Searchable.search<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'lonely'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.
              <span style="color: #000000; font-weight: bold;">select</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000; font-weight: bold;">|</span>r<span style="color: #000000; font-weight: bold;">|</span> r<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'type'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #ff0000;">'status'</span> == r<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'type'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p></code>
</div>
<p>Now all the results are just <a href="http://developers.facebook.com/docs/reference/api/status/" onclick="pageTracker._trackPageview('/outgoing/developers.facebook.com/docs/reference/api/status/?referer=');">status messages</a>.</p>
<p>We can refine this further, though. We want to get only those that point to a <a href="http://developers.facebook.com/docs/reference/api/user/" onclick="pageTracker._trackPageview('/outgoing/developers.facebook.com/docs/reference/api/user/?referer=');">User</a> object, because that&#8217;s what we&#8217;re really after (evil grin).</p>
<div class="code">
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">  results = FbGraph::Searchable.search<span style="color: #7a0874; font-weight: bold;">&#40;</span>search_string<span style="color: #7a0874; font-weight: bold;">&#41;</span>.
    <span style="color: #000000; font-weight: bold;">select</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000; font-weight: bold;">|</span>r<span style="color: #000000; font-weight: bold;">|</span> r<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'type'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #ff0000;">'status'</span> == r<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'type'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>.
    <span style="color: #000000; font-weight: bold;">select</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000; font-weight: bold;">|</span>r<span style="color: #000000; font-weight: bold;">|</span> r<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'from'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> r<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'from'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p></code>
</div>
<p>You might want to take a peek at the users you got from your search:</p>
<div class="code">
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">r = results.first
=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span>too long, snipped<span style="color: #000000; font-weight: bold;">&gt;</span>
user = FbGraph::User.fetch<span style="color: #7a0874; font-weight: bold;">&#40;</span>r<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'from'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span>too long, snipped<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
user.name
=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;Lolita Bonita&quot;</span>
&nbsp;
user.picture
=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;https://graph.facebook.com/800200356633259/picture&quot;</span></pre></td></tr></table></div>

<p></code>
</div>
<p>And the actual status:</p>
<div class="code">
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">r<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'message'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;I'm so lonely. I really need a guy beside me.&quot;</span></pre></td></tr></table></div>

<p></code>
</div>
<p>Now we&#8217;re on to something!</p>
<p>A little bit more scripting, and we can automate the download of those user pics!</p>
<p><strong>For The tl;dr Crowd</strong></p>
<p>If you&#8217;re too lazy to do all that Ruby, you can download <a href="https://github.com/radamanthus/barney_facebook_search" onclick="pageTracker._trackPageview('/outgoing/github.com/radamanthus/barney_facebook_search?referer=');">my project on github</a> and just run it:</p>
<div class="code">
<code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">ruby run.rb lonely results.html</pre></td></tr></table></div>

<p></code>
</div>
<p>This will run a Facebook search for &#8216;lonely&#8217;, download the profile pics of the matching users, and put their name, picture and status (with link to their facebook page) in an HTML file, results.html.</p>
<p>This is&#8230;Legendary!</p>
<p><em>This article was also posted <a href="http://info.exist.com/blogs/bid/47233/Ruby-Hacking-Secrets-from-Barney-Stinson" onclick="pageTracker._trackPageview('/outgoing/info.exist.com/blogs/bid/47233/Ruby-Hacking-Secrets-from-Barney-Stinson?referer=');">here</a></em>. You can also view more articles there from my awesome colleagues at <a href="http://www.exist.com/" onclick="pageTracker._trackPageview('/outgoing/www.exist.com/?referer=');">Exist</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://rad.batnag.org/2011/04/ruby-hacking-secrets-from-barney-stinson/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

