【问题标题】:Facebook Like count in widget Dashing with RubyFacebook 喜欢计数在小部件中使用 Ruby 冲刺
【发布时间】:2016-02-02 19:44:34
【问题描述】:

要使用 Dashing 创建一个监控工具,我想在一个小部件中显示 Facebook 页面的 LIKES 数。我用必要的信息检索 JSON:

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
      }
   ]
}

查看小部件中的点赞数,如本例所示:https://github.com/Ephigenia/foobugs-dashboard#default-dashboard

如何在 Ruby 中只显示点赞数?

我发现了类似的东西,但使用的是 Nokogiri https://github.com/Ephigenia/foobugs-dashboard/blob/master/jobs/twitter_user.rb

【问题讨论】:

  • 你只是想解析json并将“like_count”保留为文本?
  • 是的,我只想要价值。

标签: ruby json facebook widget dashing


【解决方案1】:

看起来像这样:

    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

【讨论】:

  • 与我的小部件完美配合
【解决方案2】:

您可以使用 json gem 解析 json:

gem install json

在您的代码中:

require 'rubygems'
require 'json'

然后像这样解析json(来自facebook的响应):

parsed_json = JSON.parse(string)

点赞数:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-12
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    相关资源
    最近更新 更多