Module: ShareCounts

Extended by:
Caching, Common
Defined in:
lib/share_counts.rb,
lib/share_counts/common.rb,
lib/share_counts/reddit.rb,
lib/share_counts/caching.rb

Defined Under Namespace

Modules: Caching, Common, Reddit

Class Method Summary collapse

Methods included from Common

to_merged_hash

Methods included from Caching

cache_enabled?, cached, clear_cache, use_cache

Class Method Details

.all(url) ⇒ Object



71
72
73
# File 'lib/share_counts.rb', line 71

def self.all url
  supported_networks.inject({}) { |r, c| r[c.to_sym] = ShareCounts.send(c, url); r }
end

.digg(url, raise_exceptions = false) ⇒ Object



28
29
30
31
32
33
# File 'lib/share_counts.rb', line 28

def self.digg url, raise_exceptions = false
  try("digg", url, raise_exceptions) {
    extract_count from_json( "http://services.digg.com/2.0/story.getInfo", :links => url ), 
      :selector => "stories/diggs"
  }
end

.extract_count(*args) ⇒ Object



9
10
11
# File 'lib/share_counts.rb', line 9

def self.extract_count *args
  extract_info *args
end

.facebook(url, raise_exceptions = false) ⇒ Object



42
43
44
45
46
47
# File 'lib/share_counts.rb', line 42

def self.facebook url, raise_exceptions = false
  try("facebook", url, raise_exceptions) {
    extract_count from_json("http://api.facebook.com/restserver.php", :v => "1.0", :method => "links.getStats",  
     :urls => url, :callback => "fb_sharepro_render", :format => "json" ), :selector => "total_count"
  }
end

.googlebuzz(url, raise_exceptions = false) ⇒ Object



56
57
58
59
60
61
# File 'lib/share_counts.rb', line 56

def self.googlebuzz url, raise_exceptions = false 
  try("googlebuzz", url, raise_exceptions) {
    from_json("http://www.google.com/buzz/api/buzzThis/buzzCounter", 
      :url => url, :callback => "google_buzz_set_count" )[url]
  }
end

.linkedin(url, raise_exceptions = false) ⇒ Object



49
50
51
52
53
54
# File 'lib/share_counts.rb', line 49

def self.linkedin url, raise_exceptions = false 
  try("linkedin", url, raise_exceptions) {
    extract_count from_json("http://www.linkedin.com/cws/share-count", 
      :url => url, :callback => "IN.Tags.Share.handleCount" ), :selector => "count"
  }
end

.reddit(url, raise_exceptions = false) ⇒ Object



17
18
19
20
21
22
# File 'lib/share_counts.rb', line 17

def self.reddit url, raise_exceptions = false
  try("reddit", url, raise_exceptions) {
    extract_count from_json( "http://www.reddit.com/api/info.json", :url => url ), 
      :selector => "data/children/data/score" 
  }
end


24
25
26
# File 'lib/share_counts.rb', line 24

def self.reddit_with_permalink url, raise_exceptions = false
  ShareCounts::Reddit.info_for url, raise_exceptions
end

.selected(url, selections) ⇒ Object



75
76
77
78
# File 'lib/share_counts.rb', line 75

def self.selected url, selections
  selections.map{|name| name.downcase}.select{|name| supported_networks.include? name.to_s}.inject({}) {
     |r, c| r[c.to_sym] = ShareCounts.send(c, url); r }
end

.stumbleupon(url, raise_exceptions = false) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/share_counts.rb', line 63

def self.stumbleupon url, raise_exceptions = false 
  try("stumbleupon", url, raise_exceptions) {
    Nokogiri::HTML.parse( 
        make_request("http://www.stumbleupon.com/badge/embed/5/", :url => url ) 
      ).xpath( "//body/div/ul/li[2]/a/span").text.to_i
  }
end

.supported_networksObject



13
14
15
# File 'lib/share_counts.rb', line 13

def self.supported_networks
  %w(reddit digg twitter facebook linkedin stumbleupon googlebuzz)
end

.twitter(url, raise_exceptions = false) ⇒ Object



35
36
37
38
39
40
# File 'lib/share_counts.rb', line 35

def self.twitter url, raise_exceptions = false
  try("twitter", url, raise_exceptions) {
    extract_count from_json( "http://urls.api.twitter.com/1/urls/count.json", :url => url), 
      :selector => "count"
  }
end