Parsing JSON Objects With Ruby Gem Typhoeus

6 Dec

I am learning a little Ruby right now by writing a few scripts to parse various data, so I thought I’d share.

In this example, I used the Ruby gem typhoeus to parse through publicly available  Facebook page data, which is available in a JSON object format. Here is an example URL for the public facebook fan page:

To install typhous simply type this into your terminal

$ gem install typhoeus

If you get an error due to your permissions (which I did), try

$ sudo gem install typhoeus

Once you have the gem installed, it is very easy to parse through the JSON Object provided by Facebook or any other api.

#this file goes through the facebook page statistics

require 'rubygems'
require 'json'
require 'typhoeus'

name = "facebook"

response = Typhoeus::Request.get("{name}")
startupStats = JSON.parse(response.body) #this is a hash
puts startupStats.fetch("name") #this prints out the fb page name
puts startupStats.fetch("link") #this prints out the fb page link
puts startupStats.fetch("likes") #this prints out the number of likes
puts startupStats.fetch("talking_about_count")

One Response to “Parsing JSON Objects With Ruby Gem Typhoeus”

  1. Lauren Proctor September 27, 2012 at 12:59 pm #

    Thanks for this. It’s by far the clearest look I’ve seen re: how Ruby plays with JSON and the Facebook API. Looking forward to reading more of your content.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s