Posts Tagged ruby
Ruby Hacking Secrets From Barney Stinson
Posted by rad in Programming, Ruby on April 13, 2011
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.
XMPP Development in Ruby
Recently I wasted a day and half in debugging an XMPP-based service I’m working on. As is usual, the real reason I lost so much time was because I was totally unfamiliar with the terrain. I should have spent an hour or two on XMPP basics before I waded into this unfamiliar territory.
If you’re just starting on XMPP development with Ruby, here are some tips and resources that you might find useful.
One client at a time
For each account, there should only be one XMPP client logged in at any time. If client A is already logged in, and then client B logs in, client A will be disconnected and will stop receiving notifications. Just like in Yahoo! Messenger: when you login from a second client, the first client will be disconnected. It’s so basic, but I managed to waste the better part of a day on debugging perfectly working code before I realized that this was what’s going on in my system.
Use switchboard for testing
When debugging network applications, you want to make sure that the application is indeed the problem and not the network. For web applications, you use curl for testing. switchboard aims to be the curl for XMPP development. curl is a command-line (hence scriptable!) http client; XMPP is a command-line XMPP client.
github page: http://github.com/mojodna/switchboard/tree
command-line usage: http://mojodna.net/2009/07/16/switchboard-curl-for-xmpp.html
Useful Resources
If you’re going to do XMPP development in plain Ruby, there’s a peepcode screencast that you might find useful. It might be a bit dated, though, and most likely there are higher-level libraries that you can use.
In my case, I was working on harvesting blog entries from Superfeedr, so I use the Superfeedr gem, superfeedr-ruby. An alternative is the more streamlined and highly opinionated superfeedr-rb.
Even if you’re not doing Superfeedr work, it’s worth spending the time to study superfeedr-ruby, if only to study how it uses Skates. Skates (formerly Babylon) is a framework for building XMPP applications in Ruby, using EventMachine for handling connections.
work.sh
Posted by rad in Programming on June 10, 2010
From a cold boot, to start working on one of my Rails projects, I need to do the following:
That’s a lot of keystrokes and mouse actions. No wonder I end up playing games most of the time, because Age of Empires III takes just one click.
I can configure MySQL and PostgreSQL to run on startup, but that’s wasteful. That’ll slow down the machine’s startup time, and sometimes I use just MySQL, on other projects I use PostgreSQL, and on other projects I use neither but instead use MongoDB. And that will just take away one step in this 5-step process to get from cold bootup to work bliss.
Ideally, there’s a one-click work button, a sideproject1 button, a sideproject2 button, etc. Press the right button and the Mac will be transformed into a mode optimized for the work to be done: run the needed apps, close unneeded apps, play music from the “work playlist”, change the desktop background, etc.
I have decided to learn AppleScript to build those magic buttons. Here’s what I’ve come up with so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/sh
# From: http://stackoverflow.com/questions/1589114/opening-a-new-terminal-tab-in-osxsnow-leopard-with-the-opening-terminal-windows/1590818#1590818
# (See answer by i0n that says "courtesy of Dan Benjamin" http://twitter.com/danbenjamin)
# Sets up my Infinitely terminal windows
# 1. Run MongoDB
# 2. Run redis in a new terminal tab
# 3. cd to the project
# 4. Launch Evernote
# 5. Launch Firefox
# 6. Launch GitX
# 1. Run MongoDB in a new terminal
/usr/bin/osascript <<mongo
activate application "Terminal"
tell application "System Events"
keystroke "t" using {command down}
end tell
tell application "Terminal"
repeat with win in windows
try
if get frontmost of win is true then
do script "cd ~; ./mongo.sh" in (selected tab of win)
end if
end try
end repeat
end tell
mongo
# 2. Run redis in a new terminal
/usr/bin/osascript <<redis
activate application "Terminal"
tell application "System Events"
keystroke "t" using {command down}
end tell
tell application "Terminal"
repeat with win in windows
try
if get frontmost of win is true then
do script "cd ~; ./redis.sh" in (selected tab of win)
end if
end try
end repeat
end tell
redis
# 3. cd to the workers project
/usr/bin/osascript <<workers
activate application "Terminal"
tell application "System Events"
keystroke "t" using {command down}
end tell
tell application "Terminal"
repeat with win in windows
try
if get frontmost of win is true then
do script "cd ~/Documents/projects/infinitely/workers; mate ." in (selected tab of win)
end if
end try
end repeat
end tell
workers
sleep 1
# 4. Launch Evernote
open /Applications/Evernote.app
# 5. Launch Firefox
open /Applications/Firefox.app
clear
There’s still no “Open iTunes and play songs in my ‘work’ playlist” section, but I’m working on it. And here’s something to make it more interesting: AppleScripting with Ruby!
This script borrowed heavily from the ideas of the people in this Stackoverflow thread.