share-counter 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/share-counter.rb +58 -41
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28bd8134397ea367af88069e45b1b1b1ad9d54a7
4
- data.tar.gz: b85350b9dc8689272f9b4bf798ba766eb91465b6
3
+ metadata.gz: 0b570728de629567947a2bce071c6015d9719b3e
4
+ data.tar.gz: 2875c99c2fffb04afa5464ae82aa0fdaf40e9ba1
5
5
  SHA512:
6
- metadata.gz: dcc5841e2c3f56c367c20bc78946996de1b5d099392000e44d3e49662076dc85fa3aeb35648073936ca91765615ab6b78982b1ef6714a8513f9b786b6a0a96a6
7
- data.tar.gz: 60605f80f2bb9a91ad08448ad15105c1b07924f8bd040c417702a7995b6b228d21de4e96476c6a058063461a4be0404ee9d3158ac0a44a694a926bb183ba3884
6
+ metadata.gz: 86142d4d74f74d17fc02675e9b54e4138ede073002790e28bb51273728507db9bc60e8168837934bf375bd807dd3ff2e3a212590b3eabec039496b2146e6f680
7
+ data.tar.gz: 838cad34f741d50422a9c9d1f8cd4803c656762c6c51e203f00501d42a5582ba4c3bc08c63b139fe76631748bc4f145a7d271adb2464f674a8d90335c9814774
data/lib/share-counter.rb CHANGED
@@ -1,51 +1,68 @@
1
+ require 'share-counter/common'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+
1
5
  class ShareCounter
2
6
 
3
- # require 'share-counter/common'
4
-
5
- # require 'json'
6
- require 'rest-client'
7
- require 'nokogiri'
8
- require 'open-uri'
9
-
10
- # def self.twitter url, raise_exceptions = false
11
- # try("twitter", url, raise_exceptions) {
12
- # extract_count from_json( "http://urls.api.twitter.com/1/urls/count.json", :url => url),
13
- # :selector => "count"
14
- # }
15
- # end
16
-
17
- # def self.facebook url, raise_exceptions = false
18
- # try("facebook", url, raise_exceptions) {
19
- # extract_count from_json("http://api.facebook.com/restserver.php", :v => "1.0", :method => "links.getStats",
20
- # :urls => url, :callback => "fb_sharepro_render", :format => "json" ), :selector => "total_count"
21
- # }
22
- # end
23
-
24
- # def self.linkedin url, raise_exceptions = false
25
- # try("linkedin", url, raise_exceptions) {
26
- # extract_count from_json("http://www.linkedin.com/cws/share-count",
27
- # :url => url, :callback => "IN.Tags.Share.handleCount" ), :selector => "count"
28
- # }
29
- # end
7
+ # network lookups
30
8
 
31
- def self.googleplus url
32
- begin
33
- url = "https://plusone.google.com/_/+1/fastbutton?url=" + URI::encode(url)
34
- html = RestClient.get url
35
- return Nokogiri::HTML.parse(html).xpath('//*[@id="aggregateCount"]').text.to_i
36
- rescue Exception => e
37
- puts e
9
+ def self.reddit url
10
+ html = make_request "http://www.reddit.com/api/info.json", url: url
11
+ j = JSON.parse(html)
12
+
13
+ unless j['data']['children'].empty?
14
+ return j['data']['children']['data']['score']
15
+ else
16
+ return 0
38
17
  end
18
+ end
19
+
20
+ def self.twitter url
21
+ html = make_request "http://urls.api.twitter.com/1/urls/count.json", url: url
22
+ return JSON.parse(html)['count']
23
+ end
24
+
25
+ def self.facebook url
26
+ html = make_request "https://api.facebook.com/method/fql.query", format: "json", query: "select like_count from link_stat where url=\"#{url}\""
27
+ return JSON.parse(html)[0]['like_count']
28
+ end
29
+
30
+ def self.linkedin url
31
+ html = make_request "http://www.linkedin.com/countserv/count/share", url: url, callback: "IN.Tags.Share.handleCount"
32
+ return JSON.parse(html)['count']
33
+ end
39
34
 
35
+ def self.googleplus url
36
+ html = make_request "https://plusone.google.com/_/+1/fastbutton", url: url
37
+ return Nokogiri::HTML.parse(html).xpath('//*[@id="aggregateCount"]').text.to_i
40
38
  end
41
39
 
42
- # def self.all url
43
- # supported_networks.inject({}) { |r, c| r[c.to_sym] = ShareCounts.send(c, url); r }
44
- # end
40
+ # helpers - get all or selected networks
41
+
42
+ def self.supported_networks
43
+ %w(reddit twitter facebook linkedin googleplus)
44
+ end
45
+
46
+ def self.all url
47
+ supported_networks.inject({}) { |r, c| r[c.to_sym] = ShareCounter.send(c, url); r }
48
+ end
45
49
 
46
- # def self.selected url, selections
47
- # selections.map{|name| name.downcase}.select{|name| supported_networks.include? name.to_s}.inject({}) {
48
- # |r, c| r[c.to_sym] = ShareCounts.send(c, url); r }
49
- # end
50
+ def self.selected url, selections
51
+ selections.map{|name| name.downcase}.select{|name| supported_networks.include? name.to_s}.inject({}) {
52
+ |r, c| r[c.to_sym] = ShareCounter.send(c, url); r }
53
+ end
50
54
 
51
55
  end
56
+
57
+
58
+ # tests, lol. they all pass.
59
+
60
+ # url = "http://makeshift.io/"
61
+ # puts ShareCounter.all url
62
+ # puts ShareCounter.selected url, [:linkedin, :facebook]
63
+
64
+ # puts ShareCounter.linkedin url
65
+ # puts ShareCounter.googleplus url
66
+ # puts ShareCounter.twitter url
67
+ # puts ShareCounter.facebook url
68
+ # puts ShareCounter.reddit url
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: share-counter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ollie Glass
@@ -56,5 +56,5 @@ rubyforge_project:
56
56
  rubygems_version: 2.1.11
57
57
  signing_key:
58
58
  specification_version: 4
59
- summary: Share
59
+ summary: ShareCounter
60
60
  test_files: []