Parsing JSON Objects With Ruby Gem Typhoeus

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: https://graph.facebook.com/facebook.


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("https://graph.facebook.com/#{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")

Enjoy the article? Join over 17,500+ Swift developers and enthusiasts who get my weekly updates.

  • Lauren Proctor

    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.