class SocialUrlStats::Counter
Constants
- HANDLED_OPEN_URI_EXCEPTIONS
Attributes
url[R]
Public Class Methods
new(url)
click to toggle source
Initialize with a URL, then use the methods below to get SUS.
# File lib/social_url_stats/counter.rb, line 12 def initialize(url) @url = url end
Public Instance Methods
fb_likes()
click to toggle source
# File lib/social_url_stats/counter.rb, line 22 def fb_likes r = get_fb_graph return nil unless r JSON.parse(r)['likes'].to_i end
gplus_ones()
click to toggle source
Google+
# File lib/social_url_stats/counter.rb, line 55 def gplus_ones r = get_response("https://plusone.google.com/_/+1/fastbutton?url=#{URI::encode(url)}") return nil unless r shares = r[/window.__SSR = {c\: \d+.\d+/] shares[/\d+.\d+/].to_i end
pins()
click to toggle source
Pinterest pins
# File lib/social_url_stats/counter.rb, line 35 def pins r = get_response("http://api.pinterest.com/v1/urls/count.json?url=#{@url}") return nil unless r JSON.parse(r.gsub('receiveCount(','').gsub(')',''))['count'].to_i end
stumbles()
click to toggle source
StumbleUpon shares
# File lib/social_url_stats/counter.rb, line 48 def stumbles r = get_response("http://www.stumbleupon.com/services/1.01/badge.getinfo?url=#{@url}") return nil unless r JSON.parse(r)['result']['views'].to_i end
tweets()
click to toggle source
# File lib/social_url_stats/counter.rb, line 28 def tweets r = get_response("https://cdn.api.twitter.com/1/urls/count.json?url=#{@url}") return nil unless r JSON.parse(r)['count'].to_i end
Protected Instance Methods
get_fb_graph()
click to toggle source
Store fb graph response so we don't make 2 hits if getting shares & likes
# File lib/social_url_stats/counter.rb, line 73 def get_fb_graph @fb_response ||= begin get_response("http://graph.facebook.com/?id=#{@url}") end end
get_response(full_url)
click to toggle source
# File lib/social_url_stats/counter.rb, line 64 def get_response(full_url) f = Timeout::timeout(10) { open(full_url) } response = f.read() rescue *HANDLED_OPEN_URI_EXCEPTIONS return nil end