Archive for category Ruby

Ruby Hacking Secrets From Barney Stinson

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’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, desperate, or horny.

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:

1
gem install fb_graph

Searching Facebook Using fb_graph

1
2
irb
require 'fb_graph'

Let’s search for the word ‘lonely’.

1
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.

1
2
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).

1
2
3
  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:

1
2
3
4
5
6
7
8
9
10
r = results.first
=> <too long, snipped>
user = FbGraph::User.fetch(r['from']['id'])
=> <too long, snipped>
 
user.name
=> "Lolita Bonita"
 
user.picture
=> "https://graph.facebook.com/800200356633259/picture"

And the actual status:

1
2
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:

1
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!

This article was also posted here. You can also view more articles there from my awesome colleagues at Exist.

,

No Comments