share-counter 0.0.8 → 0.0.9
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.
- checksums.yaml +4 -4
- data/lib/share-counter.rb +48 -24
- metadata +1 -2
- data/lib/share-counter/common.rb +0 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70066de4974656c2b044075db5f3305f8602bf5d
|
4
|
+
data.tar.gz: 1469e90870f8b14ba7c619c7d7c4762faad215fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcd6fcf78ac2f18c953d19811e490db9291e824e42038597e4daaa159544a870f80915d2f273ded2ca49e26b830229a2b434aecb141faaa63c8922fb24352a69
|
7
|
+
data.tar.gz: 1b9cfb9d8bd6a23f6bcfad6409eab0a938f9a37f31e8a76c32e2ddde7ca82bf519996d8d7d7b5bc5fbd5674a7bb59a6483f19eb80a594dc9820c4501c4b629f3
|
data/lib/share-counter.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
require 'share-counter/common'
|
2
1
|
require 'nokogiri'
|
3
2
|
require 'open-uri'
|
4
3
|
require 'json'
|
4
|
+
require 'rest-client'
|
5
|
+
require 'digest/md5'
|
5
6
|
|
6
7
|
class ShareCounter
|
7
8
|
|
@@ -24,8 +25,8 @@ class ShareCounter
|
|
24
25
|
end
|
25
26
|
|
26
27
|
def self.facebook url
|
27
|
-
html = make_request "https://api.facebook.com/method/fql.query", format: "json", query: "select
|
28
|
-
return JSON.parse(html)[0]['
|
28
|
+
html = make_request "https://api.facebook.com/method/fql.query", format: "json", query: "select share_count from link_stat where url=\"#{url}\""
|
29
|
+
return JSON.parse(html)[0]['share_count']
|
29
30
|
end
|
30
31
|
|
31
32
|
def self.linkedin url
|
@@ -39,13 +40,9 @@ class ShareCounter
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def self.delicious url
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
return -1
|
46
|
-
else
|
47
|
-
return JSON.parse(html)[0]['total_posts']
|
48
|
-
end
|
43
|
+
url_digest = Digest::MD5.hexdigest(url)
|
44
|
+
html = make_request "https://avosapi.delicious.com/api/v1/posts/md5/#{url_digest}"
|
45
|
+
JSON.parse(html)["pkg"][0]["num_saves"].to_i rescue 0
|
49
46
|
end
|
50
47
|
|
51
48
|
def self.stumbleupon url
|
@@ -75,21 +72,48 @@ class ShareCounter
|
|
75
72
|
selections.map{|name| name.downcase}.select{|name| supported_networks.include? name.to_s}.inject({}) {
|
76
73
|
|r, c| r[c.to_sym] = ShareCounter.send(c, url); r }
|
77
74
|
end
|
75
|
+
private
|
76
|
+
|
77
|
+
#
|
78
|
+
#
|
79
|
+
# Performs an HTTP request to the given API URL with the specified params
|
80
|
+
# and within 2 seconds, and max 3 attempts
|
81
|
+
#
|
82
|
+
# If a :callback param is also specified, then it is assumed that the API
|
83
|
+
# returns a JSON text wrapped in a call to a method by that callback name,
|
84
|
+
# therefore in this case it manipulates the response to extract only
|
85
|
+
# the JSON data required.
|
86
|
+
#
|
87
|
+
def self.make_request *args
|
88
|
+
result = nil
|
89
|
+
attempts = 1
|
90
|
+
url = args.shift
|
91
|
+
params = args.inject({}) { |r, c| r.merge! c }
|
92
|
+
|
93
|
+
begin
|
94
|
+
response = RestClient.get url, { :params => params, :timeout => 5 }
|
95
|
+
|
96
|
+
# if a callback is specified, the expected response is in the format "callback_name(JSON data)";
|
97
|
+
# with the response ending with ";" and, in some cases, "\n"
|
98
|
+
#Strip off the preceeding /**/
|
99
|
+
result = params.keys.include?(:callback) \
|
100
|
+
? response.gsub(/\A\/\*\*\/\s+/, "").gsub(/^(.*);+\n*$/, "\\1").gsub(/^#{params[:callback]}\((.*)\)$/, "\\1") \
|
101
|
+
: response
|
102
|
+
|
103
|
+
rescue Exception => e
|
104
|
+
puts "Failed #{attempts} attempt(s) - #{e}"
|
105
|
+
attempts += 1
|
106
|
+
if attempts <= 3
|
107
|
+
sleep 2
|
108
|
+
retry
|
109
|
+
else
|
110
|
+
raise Exception
|
111
|
+
end
|
112
|
+
end
|
78
113
|
|
79
|
-
|
80
|
-
|
114
|
+
result
|
115
|
+
end
|
81
116
|
|
82
|
-
# tests, lol. they all pass.
|
83
117
|
|
84
|
-
|
85
|
-
# puts ShareCounter.all url
|
86
|
-
# puts ShareCounter.selected url, [:linkedin, :facebook]
|
118
|
+
end
|
87
119
|
|
88
|
-
# puts ShareCounter.linkedin url
|
89
|
-
# puts ShareCounter.googleplus url
|
90
|
-
# puts ShareCounter.twitter url
|
91
|
-
# puts ShareCounter.facebook url
|
92
|
-
# puts ShareCounter.reddit url
|
93
|
-
# puts ShareCounter.delicious url
|
94
|
-
# puts ShareCounter.stumbleupon url
|
95
|
-
# puts ShareCounter.pinterest 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.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ollie Glass
|
@@ -59,7 +59,6 @@ extensions: []
|
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
61
|
- lib/share-counter.rb
|
62
|
-
- lib/share-counter/common.rb
|
63
62
|
homepage: http://rubygems.org/gems/share-counter
|
64
63
|
licenses:
|
65
64
|
- MIT
|
data/lib/share-counter/common.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'rest-client'
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# Performs an HTTP request to the given API URL with the specified params
|
6
|
-
# and within 2 seconds, and max 3 attempts
|
7
|
-
#
|
8
|
-
# If a :callback param is also specified, then it is assumed that the API
|
9
|
-
# returns a JSON text wrapped in a call to a method by that callback name,
|
10
|
-
# therefore in this case it manipulates the response to extract only
|
11
|
-
# the JSON data required.
|
12
|
-
#
|
13
|
-
def make_request *args
|
14
|
-
result = nil
|
15
|
-
attempts = 1
|
16
|
-
url = args.shift
|
17
|
-
params = args.inject({}) { |r, c| r.merge! c }
|
18
|
-
|
19
|
-
begin
|
20
|
-
response = RestClient.get url, { :params => params, :timeout => 5 }
|
21
|
-
|
22
|
-
# if a callback is specified, the expected response is in the format "callback_name(JSON data)";
|
23
|
-
# with the response ending with ";" and, in some cases, "\n"
|
24
|
-
#Strip off the preceeding /**/
|
25
|
-
result = params.keys.include?(:callback) \
|
26
|
-
? response.gsub(/\A\/\*\*\/\s+/, "").gsub(/^(.*);+\n*$/, "\\1").gsub(/^#{params[:callback]}\((.*)\)$/, "\\1") \
|
27
|
-
: response
|
28
|
-
|
29
|
-
rescue Exception => e
|
30
|
-
puts "Failed #{attempts} attempt(s) - #{e}"
|
31
|
-
attempts += 1
|
32
|
-
if attempts <= 3
|
33
|
-
retry
|
34
|
-
else
|
35
|
-
raise Exception
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
result
|
40
|
-
end
|