1
votes

To create a monitoring tool with Dashing, I want to display the number of LIKES of Facebook page in a widget. I retrieve the JSON with the necessary information :

http://graph.facebook.com/fql?q=SELECT%20share_count,%20like_count,%20comment_count,%20total_count%20FROM%20link_stat%20WHERE%20url=%22http://www.google.fr%22&format=json

{
   "data": [
      {
         "share_count": 242039,
         "like_count": 63648,
         "comment_count": 52304,
         "total_count": 357991
      }
   ]
}

to view the number of like in a widget as in this example : https://github.com/Ephigenia/foobugs-dashboard#default-dashboard

How to display only the number of like in Ruby ?

I found something similar but uses Nokogiri https://github.com/Ephigenia/foobugs-dashboard/blob/master/jobs/twitter_user.rb

2
You just want to parse the json and retain "like_count" as text?Laurens
Yes, I just want the value.Romain Schneider

2 Answers

1
votes

That would look like this:

    require 'json'

    # make the GET request
    resp = Net::HTTP.get(URI("http://graph.facebook.com/fql?q=SELECT%20share_count,%20like_count,%20comment_count,%20total_count%20FROM%20link_stat%20WHERE%20url=%22http://www.google.fr%22&format=json"))

    # parse the JSON into a ruby hash
    json = JSON.parse(resp)

    # pull the like_count value out of the response
    puts json["data"][0]["like_count"]

    => 63648
1
votes

You can parse json with the json gem:

gem install json

In your code:

require 'rubygems'
require 'json'

Then parse the json like this (the response from facebook):

parsed_json = JSON.parse(string)

For the like count:

parsed_json["data"][0]["like_count"]