0

我需要计算 Rails 3 应用程序中特定 URL 的 Facebook Likes、Google +1、Diggs、StumbleUpon 和 Tweets。

Rails 中是否有任何教程、API 或演示?

4

1 回答 1

0

对于facebook likes count,google +1 count

我写了以下帮助程序来根据 url 查找计数

  #Ref:- http://developers.facebook.com/docs/reference/plugins/like/
  def facebook_like_count(url, id)
    js = <<-JAVASCRIPT
      <div id="#{id}">
        <img src='/images/facebook_login.png'/>
      </div>
      <script>
        jQuery(window).load(function() {
          var query = FB.Data.query('select like_count from link_stat where url="#{url}"');
          query.wait(function(rows){
            jQuery("##{id}").append((typeof(rows[0]) === "undefined") ? "<br/>0" : "<br/>"+rows[0].like_count)
          });
        });
      </script>
      JAVASCRIPT
      js.html_safe
  end

  def tweets_count(url, id)
    js = <<-JAVASCRIPT
      <div id="#{id}">
        <img src="/images/share-twitter.png" />
      </div>
      <script>
        jQuery(window).load(function() {
          jQuery.getJSON('http://feeds.delicious.com/v2/json/urlinfo/data?url='+"#{url}"+'&amp;callback=?',
            function(data) {
              jQuery('##{id}').append((typeof(data[0]) === "undefined") ? "<br/>0" : "<br/>"+data[0].total_posts);
          });
        });
      </script>
      JAVASCRIPT
      js.html_safe
  end

  #Ref:- https://developers.google.com/+/plugins/+1button/#plusonetag-parameters
  def google_plus_one_count(url, id)
    js = <<-JAVASCRIPT
      <div id="#{id}">
        <g:plusone href="#{url}" annotation="bubble"></g:plusone>
      </div>
      <script type="text/javascript">
        jQuery(window).load(function() {
        window.___gcfg = {
          lang: 'en-US'
        };

        (function() {
          var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
          po.src = 'https://apis.google.com/js/plusone.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
        })();
        });
      </script>
      JAVASCRIPT
      js.html_safe
  end
于 2012-12-12T05:50:05.267 回答