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:

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")

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