Current Articles | RSS Feed RSS Feed

Ruby Hacking Secrets from Barney Stinson

  
  
  
  
  

Legendary!

by Radamanthus Batnag

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'm sure you're familiar with the adage "Success is being in the right place at the right time." 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's lonely or desperate.

How does Barney find the right place and the right time? You guessed it right, by stalking them on Facebook.

But Barney doesn't use the lame Facebook search form. That's too inefficient for him. He uses Ruby, the Facebook API, and the awesome fb_graph gem. In this article, I'll give you a peek at the awesome Facebook stalking and searching you can do using these three tools.

SETUP

I'm assuming you already have Ruby 1.9.2 installed. MRI, MacRuby, whatever, as long as it implements 1.9.2 and you run it using RVM.

After that, all you really need is the fb_graph gem:

gem install fb_graph

Searching Facebook Using fb_graph

irb
require 'fb_graph'

Let's search for the word 'lonely'.

results = FbGraph::Searchable.search('lonely')
Yep, it's that easy! What you get is an Array of Hashes, each hash containing a search result.

After taking a closer look at the results, you probably don't like most of what you got - most are people who "Liked" Justin Bieber's recent song, "One Less Lonely Girl."

So let's refine our search a little. Let's get only search for Facebook statuses. Yep, that's most likely what we want - girls who have out "I'm lonely" in their status.

results = FbGraph::Searchable.search('lonely').
              select{|r| r['type'] && 'status' == r['type']}
Now all the results are just status messages.
We can refine this further, though. We want to get only those that point to a User object, because that's what we're really after (evil grin).
  results = FbGraph::Searchable.search(search_string).
    select{|r| r['type'] && 'status' == r['type']}.
    select{|r| r['from'] && r['from']['id']}
You might want to take a peek at the users you got from your search:
r = results.first
=> 
user = FbGraph::User.fetch(r['from']['id'])
=> 

user.name
=> "Lolita Bonita"

user.picture
=> "https://graph.facebook.com/800200356633259/picture"
And the actual status:
r['message']
=> "I'm so lonely. I really need a guy beside me."

Now we're on to something!

A little bit more scripting, and we can automate the download of those user pics!

For The tl;dr Crowd

If you're too lazy to do all that Ruby, you can download my project on github and just run it:

ruby run.rb lonely results.html

This will run a Facebook search for 'lonely', 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.

This is...Legendary!

The above article was originally posted in Rad's blog and before that, demoed in Exist's recruitment party in Manila held at the TechBar last 08 April 2011. Rad Batnag had built websites on Java, Oracle and SQL Server. He now works as a Ruby programmer for Exist during the day, and is an amateur iPhone programmer during the night.

Comments

wow! cool. thanks for this post!!! even though im not a ruby programmer.
Posted @ Thursday, April 14, 2011 12:26 AM by cute programmer
wow neil become rich because of ruby
Posted @ Monday, April 25, 2011 8:52 AM by Bon Jovi
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics

cta

Subscribe by Email

Your email:

About Exist

Technology innovators with open source roots. Founded 2001. Java, Ruby on Rails, and dedicated interactive capabilities. Software development operations in Manila and Cebu, Philippines.

Download Exist Fact Sheet

Wanted: Java, Rails developers

Philippines Java Rails developers careers

Categories