Sha256: 814c19b0cff792d4948d8ff6233cb8e827cc92cc0e056cd288e59200e5448f39

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

class SocialUrlStats::Counter
  attr_reader :url

  # Initialize with a URL, then use the methods below to get SUS.
  def initialize(url)
    @url = url
  end

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

  def fb_likes
    f = open("http://graph.facebook.com/?id=#{@url}")
    response = f.read()
    likes = JSON.parse(response)['likes']
    return likes.nil? ? 0 : JSON.parse(response)['likes'].to_i
  end
 
  def tweets
    f = open("https://cdn.api.twitter.com/1/urls/count.json?url=#{@url}")
    return JSON.parse(f.read())['count'].to_i
  end
 
  # Pinterest pins
  def pins
    f = open("http://api.pinterest.com/v1/urls/count.json?url=#{@url}")
    return JSON.parse(f.read().gsub('receiveCount(','').gsub(')',''))['count'].to_i
  end
 
  def linkedin_shares
    f = open("http://www.linkedin.com/countserv/count/share?url=#{@url}&format=json")
    return JSON.parse(f.read())['count'].to_i
  end
 
  # StumbleUpon shares
  def stumbles
    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'].to_i
  end
 
  # Google+
  def gplus_ones
    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+/].to_i
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
social_url_stats-0.0.1 lib/social_url_stats/counter.rb