0.04
No commit activity in last 3 years
No release in over 3 years
The easiest way to check how many times a URL has been shared on Reddit, Digg, Twitter, Facebook, LinkedIn, GoogleBuzz and StumbleUpon!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Original blog post (January 31, 2011) vitobotta.com/share-counts-gem-social-networks/

Share Counts¶ ↑

This gem makes it super easy to check how many times a page/URL has been shared on social networks and aggregators. It also supports caching with Redis both to speed things up and to reduce the risk of problems with API rate limits.

Services currently supported:

  • Reddit

  • Digg

  • Twitter

  • Facebook

  • LinkedIn

  • Google Buzz

  • StumbleUpon

Installation¶ ↑

Just.. gem install share_counts

and then require/bundle it.

Basic Usage¶ ↑

You can check each service individually…

ruby-1.9.2-p0 :001 > require "share_counts"
=> true 

ruby-1.9.2-p0 :002 > url = "http://vitobotta.com/awesomeprint-similar-production/"
=> "http://vitobotta.com/awesomeprint-similar-production/" 

# Reddit
ruby-1.9.2-p0 :003 > ShareCounts.reddit url
=> 5 

# Digg
ruby-1.9.2-p0 :004 > ShareCounts.digg url
=> 1 

# Twitter
ruby-1.9.2-p0 :005 > ShareCounts.twitter url
=> 2 

# Facebook 
ruby-1.9.2-p0 :006 > ShareCounts.facebook url
=> 1 

# LinkedIn
ruby-1.9.2-p0 :008 > ShareCounts.linkedin url
=> 2 

# StumbleUpon
ruby-1.9.2-p0 :009 > ShareCounts.stumbleupon url
=> 0 

# Google Buzz
ruby-1.9.2-p0 :010 > ShareCounts.googlebuzz url
=> 0

or you can get ‘em all in one shot:

ruby-1.9.2-p0 :017 > ShareCounts.all "http://vitobotta.com/awesomeprint-similar-production/"
=> {:reddit=>5, :digg=>1, :twitter=>2, :facebook=>1, :linkedin=>2, :googlebuzz=>0, :stumbleupon=>0}

You can also specify which networks you want to query at once:

ruby-1.9.2-p0 :004 > ShareCounts.selected "http://vitobotta.com/awesomeprint-similar-production/", [:reddit, :twitter]
=> {:reddit=>5, :twitter=>2}

For Reddit, you can also

1) get both score and permalink for a given URL:

ruby-1.9.2-head :004 > ShareCounts.reddit_with_permalink "http://vitobotta.com/faster-internet-browsing-alternative-dns-servers-fast-local-cache-bind/"
Redis caching is disabled - Making request to reddit-details...
=> {"score"=>22, "permalink"=>"/r/apple/comments/fmkxz/faster_internet_browsing_with_alternative_dns/"}

2) get the share counts for all the known URLs from a given domain:

ruby-1.9.2-head :009 > ShareCounts::Reddit.by_domain "vitobotta.com"
Redis caching is disabled - Making request to reddit-domain...

Sometimes APIs may not be available or may be having issues (or there’s some rate limit and you are making too many requests in a short time). When something goes wrong while querying a service, even for a max of 3 attempts, the share count for that service is left as set to nil. This way you can easily know whether a share count could be obtained or updated for a given URL, but simply checking if the share count is nil.

ruby-1.9.2-p0 :002 > ShareCounts.selected “vitobotta.com/awesomeprint-similar-production/”, [:reddit, :twitter] Making request to reddit… Failed 1 attempt(s) Failed 2 attempt(s) Failed 3 attempt(s) Something went wrong with reddit: can’t convert nil into String Making request to twitter… => {:reddit=>nil, :twitter=>2}

Caching¶ ↑

Depending on how and in which kind of applications you may want to use this gem, if you make too many requests in a short time some APIs may fail because of rate limits. Plus, some API may respond too slowly at times or just be down (Digg seems to be the least reliable of the group so far!). So the gem also supports caching, at the moment with Redis only.

It’s very easy to enable and use the caching:

ruby-1.9.2-p0 :002 > require ‘benchmark’ => true

# Enabling caching ruby-1.9.2-p0 :003 > ShareCounts.use_cache => #<Redis client v2.1.1 connected to redis://127.0.0.1:6379/0 (Redis v2.0.3)>

# First run, values are not cached ruby-1.9.2-p0 :004 > Benchmark.realtime { ShareCounts.all “vitobotta.com/awesomeprint-similar-production/” } Making request to reddit… Making request to digg… Making request to twitter… Making request to facebook… Making request to linkedin… Making request to googlebuzz… Making request to stumbleupon… => 3.7037899494171143

# Now values are cached ruby-1.9.2-p0 :005 > Benchmark.realtime { ShareCounts.all “vitobotta.com/awesomeprint-similar-production/” } Loaded reddit count from cache Loaded digg count from cache Loaded twitter count from cache Loaded facebook count from cache Loaded linkedin count from cache Loaded googlebuzz count from cache Loaded stumbleupon count from cache => 0.003225088119506836

By default, the gem connects to the Redis store listening on 127.0.0.1:6379, but you can override these settings when you enable the caching:

ruby-1.9.2-p0 :002 > ShareCounts.use_cache :host => “192.168.10.85”, :port => 7500 => #<Redis client v2.1.1 connected to redis://192.168.10.85:7500/0 (Redis v2.0.3)>

Or, if you are already using the “redis” gem in your application and therefore already have initialised a connection to the Redis store, you can either pass the reference to that instance:

ruby-1.9.2-p0 :003 > ShareCounts.use_cache :redis_store => YOUR_REFERENCE_TO_REDIS_STORE => #<Redis client v2.1.1 connected to redis://127.0.0.1:6379/0 (Redis v2.0.3)>

or just set the global variable $share_counts_cache to that reference.

Similarly, by default cached share counts expire in two minutes, but you can override this by setting the global variable $share_counts_cache_expire (in seconds).

You can also get an hash having all the cached URLs with their share counts:

ruby-1.9.2-p0 :009 > ShareCounts.cached => {“vitobotta.com/awesomeprint-similar-production/”=>{:stumbleupon=>0, :linkedin=>2, :googlebuzz=>0, :facebook=>1, :twitter=>2, :digg=>1, :reddit=>5}}

And, if needed, your can clear the cached values:

ruby-1.9.2-p0 :010 > ShareCounts.clear_cache => [“ShareCounts||stumbleupon||vitobotta.com/awesomeprint-similar-production/”, “ShareCounts||linkedin||vitobotta.com/awesomeprint-similar-production/”, “ShareCounts||googlebuzz||vitobotta.com/awesomeprint-similar-production/”, “ShareCounts||facebook||vitobotta.com/awesomeprint-similar-production/”, “ShareCounts||twitter||vitobotta.com/awesomeprint-similar-production/”, “ShareCounts||digg||vitobotta.com/awesomeprint-similar-production/”, “ShareCounts||reddit||vitobotta.com/awesomeprint-similar-production/”] ruby-1.9.2-p0 :011 > ShareCounts.cached => {

However the gem will namespace all the keys as you can see, in case you also use Redis for something else in the same app.

Authors¶ ↑