Social shares is intended to easily check social sharings of an url.
You can track how many times the url was shared in various social networks, such as facebook, twitter, etc. It can be useful for some kind landings with social mechanics or for tracking network activity of your content pages.
International:
Russian:
- vkontakte
- mail.ru(aka moi mir)
- odnoklassniki
- yandex (currently on beta, most of time returns 0)
Chinese:
:000 > require 'social_shares'
=> true
:000 > url = 'http://www.apple.com/'
=> "http://www.apple.com/"
:000 > SocialShares.facebook url
=> 394927
:000 > SocialShares.google url
=> 28289
:000 > SocialShares.twitter url
=> 1164675
In case of exception:
:000 > SocialShares.twitter url
=> nil
:000 > SocialShares.twitter! url
=> RestClient::RequestTimeout: Request Timeout
List of all supported networks:
:000 > SocialShares.supported_networks
=> [:vkontakte, :facebook, :google, :twitter, :mail_ru, :odnoklassniki, :reddit, :linkedin, :pinterest, :stumbleupon, :buffer]
Fetch all shares by one method (#all, #all!):
# in case of exception it will return nil
:000 > SocialShares.all url
=> {:vkontakte=>nil, :facebook=>399027, :google=>28346, :twitter=>1836, :mail_ru=>37, :odnoklassniki=>1, :reddit=>2361, :linkedin=>33, :pinterest=>21011, :stumbleupon=>43035, :weibo=>12760, :buffer=>1662, :yandex=>0}
# and this will raise it
:000 > SocialShares.all! url
=> RestClient::RequestTimeout: Request Timeout
Fetch shares of selected networks(#selected, #selected!):
:000 > SocialShares.selected url, %w(facebook google linkedin)
=> {:facebook=>394927, :google=>28289, :linkedin=>nil}
# same here
:000 > SocialShares.selected! url, %w(facebook google linkedin)
=> RestClient::RequestTimeout: Request Timeout
Total sum of sharings in selected networks:
:000 > SocialShares.total url, %w(facebook google)
=> 423216
# Second arg is optional, by default it takes all networks
:000 > SocialShares.total url
=> 1631102
Does any network have at least one link?
:000 > SocialShares.has_any? url, %w(facebook google)
=> true
# Second arg is optional, by default it takes all networks
:000 > SocialShares.has_any? url
=> true
Note that #has_any? is faster than (#total > 0), because it stops on first network that has at least 1 sharing
Include the gem in your Gemfile:
gem 'social_shares'
- Create provider class in lib/social_shares/foo.rb with method #shares!, that return Integer value. #checked_url is accessed attribute.
module SocialShares
class Foo < Base
def shares!
response = RestClient.get(url)
JSON.parse(response)["shares"] || 0
end
private
def url
"http://foo.com/?url=#{checked_url}"
end
end
end
- Add it to lib/social_shares.rb
require 'social_shares/foo'
SUPPORTED_NETWORKS = [:foo, :vkontakte, :facebook]
- Update README: add link to list, possible answer in #all method, etc.