Where's the share?

Ever wondered how you can get the shares made for your url?  Here's a ruby library that might just save and put some color to your day!

require 'open-uri'

module SocialUtils

  def self.get_facebook_shares(url)
    f = open("http://graph.facebook.com/?id=#{url}")
    response = f.read()
    shares = JSON.parse(response)['shares']
    return shares.nil? ? 0 : JSON.parse(response)['shares']
  end

  def self.get_twitter_shares(url)
    f = open("http://cdn.api.twitter.com/1/urls/count.json?url=#{url}")
    return JSON.parse(f.read())['count']
  end

  def self.get_pinterest_shares(url)
    f = open("http://api.pinterest.com/v1/urls/count.json?url=#{url}")
    return JSON.parse(f.read().gsub('receiveCount(','').gsub(')',''))['count']
  end

  def self.get_linkedin_shares(url)
    f = open("http://www.linkedin.com/countserv/count/share?url=#{url}&format=json")
    return JSON.parse(f.read())['count']
  end

  def self.get_stumbleupon_shares(url)
    f = open("http://www.stumbleupon.com/services/1.01/badge.getinfo?url=#{url}")
    response = f.read()
    views = JSON.parse(response)['result']['views']
    return views.blank? ? 0 : JSON.parse(response)['result']['views']
  end

  def self.get_googleplus_shares(url)
    f = open("https://plusone.google.com/_/+1/fastbutton?url=#{URI::encode(url)}")
    response = f.read()
    shares = response[/window.__SSR = {c\: \d+.\d+/]
    return shares.nil? ? 0 : shares[/\d+.\d+/]
  end
end

See original gist here: https://gist.github.com/maricris-sn/7082858

Maricris Nonato

I turn tech ideas into websites using Ruby. I'm a budding Buddhist practitioner, passionate about health & fitness, and a cat fanatic!