Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a library that pulls in shares from Facebook, Twitter, Google Plus, Pinterest, LinkedIn and StumbleUpon. If you need a more elaborate breakdown for Facebook related data, please look at: http://stackoverflow.com/questions/6137414/how-to-fetch-facebook-likes-share-comments-count-from-an-article/7707702#7707702 Inspired by https://gist.git…
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("https://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
## Your social_utils_spec.rb
## uses vcr gem: https://github.com/vcr/vcr
## expected count is the count at the time of first run
require 'open-uri'
require 'spec_helper'
require "#{Rails.root}/lib/social_utils.rb"
describe SocialUtils do
let(:url) { 'http://maricrisnonato.com' }
it "get_facebook_shares", :vcr do
VCR.use_cassette('social_utils', :record => :new_episodes) do
count = SocialUtils.get_facebook_shares(url)
expect(count).eql?('0')
end
end
it "get_twitter_shares", :vcr do
VCR.use_cassette('social_utils', :record => :new_episodes) do
count = SocialUtils.get_twitter_shares(url)
expect(count).eql?('1')
end
end
it "get_pinterest_shares", :vcr do
VCR.use_cassette('social_utils', :record => :new_episodes) do
count = SocialUtils.get_pinterest_shares(url)
expect(count).eql?('0')
end
end
it "get_linkedin_shares", :vcr do
VCR.use_cassette('social_utils', :record => :new_episodes) do
count = SocialUtils.get_linkedin_shares(url)
expect(count).eql?('0')
end
end
it "get_stumbleupon_shares", :vcr do
VCR.use_cassette('social_utils', :record => :new_episodes) do
count = SocialUtils.get_stumbleupon_shares(url)
expect(count).eql?('0')
end
end
it "get_googleplus_shares", :vcr do
VCR.use_cassette('social_utils', :record => :new_episodes) do
count = SocialUtils.get_googleplus_shares(url)
expect(count).eql?('0')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.