virility 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/virility.rb +29 -29
- data/lib/virility/exceptions.rb +1 -1
- data/lib/virility/excitation.rb +102 -102
- data/lib/virility/strategies/delicious.rb +8 -8
- data/lib/virility/strategies/facebook.rb +19 -16
- data/lib/virility/strategies/pinterest.rb +13 -13
- data/lib/virility/strategies/plus_one.rb +17 -17
- data/lib/virility/strategies/stumble_upon.rb +16 -16
- data/lib/virility/strategies/twitter.rb +11 -11
- data/lib/virility/strategy.rb +83 -83
- data/lib/virility/supporter.rb +53 -53
- data/lib/virility/version.rb +1 -1
- data/spec/excitation_spec.rb +111 -111
- data/spec/spec_helper.rb +11 -11
- data/spec/strategies/delicious_spec.rb +51 -51
- data/spec/strategies/facebook_spec.rb +93 -93
- data/spec/strategies/pinterest_spec.rb +51 -51
- data/spec/strategies/plus_one_spec.rb +51 -51
- data/spec/strategies/stumble_upon_spec.rb +51 -51
- data/spec/strategies/twitter_spec.rb +51 -51
- data/spec/strategy_spec.rb +98 -98
- data/spec/virility_spec.rb +64 -64
- data/virility.gemspec +1 -1
- metadata +3 -3
data/lib/virility.rb
CHANGED
@@ -11,33 +11,33 @@ Dir["#{File.dirname(__FILE__)}/virility/strategies/**/*.rb"].each {|f| require f
|
|
11
11
|
|
12
12
|
module Virility
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
14
|
+
#
|
15
|
+
# Public API
|
16
|
+
#
|
17
|
+
|
18
|
+
def self.counts(url)
|
19
|
+
Virility::Excitation.new(url).counts
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.total(url)
|
23
|
+
Virility::Excitation.new(url).total
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.poll(url)
|
27
|
+
Virility::Excitation.new(url).poll
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.url(url)
|
31
|
+
virility = Virility::Excitation.new(url)
|
32
|
+
virility.poll
|
33
|
+
virility
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# Factory
|
38
|
+
#
|
39
|
+
|
40
|
+
def self.factory(strategy, url)
|
41
|
+
Virility::Excitation.new(url).send(strategy)
|
42
|
+
end
|
43
43
|
end
|
data/lib/virility/exceptions.rb
CHANGED
data/lib/virility/excitation.rb
CHANGED
@@ -1,104 +1,104 @@
|
|
1
1
|
module Virility
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
2
|
+
class Excitation
|
3
|
+
include Virility::Supporter
|
4
|
+
|
5
|
+
attr_accessor :url, :results, :strategies, :counts
|
6
|
+
|
7
|
+
#
|
8
|
+
# Initialization
|
9
|
+
#
|
10
|
+
|
11
|
+
def initialize url
|
12
|
+
@url = url
|
13
|
+
@strategies = {}
|
14
|
+
@results = {}
|
15
|
+
@counts = {}
|
16
|
+
collect_strategies
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Get Virility from all of the Strategies
|
21
|
+
#
|
22
|
+
|
23
|
+
def poll
|
24
|
+
if @results.empty?
|
25
|
+
@strategies.each do |name, strategy|
|
26
|
+
begin
|
27
|
+
@results[symbolize_for_key(strategy)] = strategy.poll
|
28
|
+
rescue => e
|
29
|
+
puts "[virility#poll] #{strategy.class.to_s} => #{e}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
@results
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_response(strategy)
|
37
|
+
@strategies[strategy].response if @strategies[strategy]
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Return Collected Counts as a Hash
|
42
|
+
#
|
43
|
+
|
44
|
+
def counts
|
45
|
+
poll
|
46
|
+
if @counts.empty?
|
47
|
+
@strategies.each do |name, strategy|
|
48
|
+
begin
|
49
|
+
@counts[symbolize_for_key(strategy)] = strategy.count.to_i
|
50
|
+
rescue => e
|
51
|
+
puts "[virility] #{strategy.class.to_s} => #{e}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
@counts
|
56
|
+
end
|
57
|
+
|
58
|
+
def total_virility
|
59
|
+
counts.values.inject(0) { |result, count| result + count }
|
60
|
+
end
|
61
|
+
alias :total :total_virility
|
62
|
+
|
63
|
+
#
|
64
|
+
# Gather all of the Strategies
|
65
|
+
#
|
66
|
+
|
67
|
+
def collect_strategies
|
68
|
+
Dir["#{File.dirname(__FILE__)}/strategies/**/*.rb"].each { |klass| @strategies[get_class_string(klass).to_sym] = Virility.const_get(camelize(get_class_string(klass))).new(@url) }
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# Reflection
|
73
|
+
#
|
74
|
+
|
75
|
+
def attributes
|
76
|
+
{:url => @url, :available_strategies => @strategies.keys}
|
77
|
+
end
|
78
|
+
|
79
|
+
#
|
80
|
+
# Dynamic Methods
|
81
|
+
#
|
82
|
+
|
83
|
+
def get_strategy strategy
|
84
|
+
if strategy_exists?(strategy)
|
85
|
+
@strategies[strategy.to_sym]
|
86
|
+
else
|
87
|
+
raise UnknownStrategy, "#{strategy} Is Not A Known Strategy"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def strategy_exists? strategy
|
92
|
+
!@strategies[strategy.to_sym].nil?
|
93
|
+
end
|
94
|
+
|
95
|
+
def method_missing(name, *args, &block)
|
96
|
+
if strategy_exists?(name)
|
97
|
+
get_strategy(name)
|
98
|
+
else
|
99
|
+
raise UnknownStrategy, "#{name} Is Not A Known Strategy"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
104
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module Virility
|
2
|
-
|
2
|
+
class Delicious < Strategy
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
def census
|
5
|
+
self.class.get("http://feeds.delicious.com/v2/json/urlinfo/data?url=#{@url}")
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def count
|
9
|
+
results["total_posts"] || 0
|
10
|
+
end
|
11
11
|
|
12
|
-
|
12
|
+
end
|
13
13
|
end
|
@@ -1,21 +1,24 @@
|
|
1
1
|
module Virility
|
2
|
-
|
3
|
-
|
2
|
+
class Facebook < Strategy
|
3
|
+
BASE_URL = "https://api.facebook.com/method/fql.query?query=SELECT+share_count%2C+like_count%2C+comment_count%2C+total_count%2C+commentsbox_count%2C+click_count+FROM+link_stat+WHERE+url%3D"
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def census
|
6
|
+
self.class.get("#{BASE_URL}%22#{@url}%22")
|
7
|
+
end
|
8
|
+
|
9
|
+
def outcome
|
10
|
+
@response.parsed_response["fql_query_response"]["link_stat"]
|
11
|
+
end
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
def count
|
14
|
+
results["total_count"] || 0
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def valid_response_test
|
20
|
+
@response.respond_to?(:parsed_response) and @response.parsed_response.is_a?(Hash) and !@response.parsed_response["fql_query_response"].nil? and !@response.parsed_response["fql_query_response"]["link_stat"].nil?
|
21
|
+
end
|
19
22
|
|
20
|
-
|
23
|
+
end
|
21
24
|
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
module Virility
|
2
|
-
|
2
|
+
class Pinterest < Strategy
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
parser(
|
5
|
+
Proc.new do |body, format|
|
6
|
+
MultiJson.decode(body.scan(/(\{.+\})/).flatten.first)
|
7
|
+
end
|
8
|
+
)
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def census
|
11
|
+
self.class.get("http://api.pinterest.com/v1/urls/count.json?url=#{@url}")
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def count
|
15
|
+
results["count"] || 0
|
16
|
+
end
|
17
17
|
|
18
|
-
|
18
|
+
end
|
19
19
|
end
|
@@ -1,20 +1,20 @@
|
|
1
1
|
# http://stackoverflow.com/questions/7403553/how-do-i-get-the-counter-of-a-google-plus-1-button
|
2
2
|
module Virility
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
3
|
+
class PlusOne < Strategy
|
4
|
+
|
5
|
+
parser(
|
6
|
+
Proc.new do |body, format|
|
7
|
+
{'shares' => body.scan(/c: (\d+)/).flatten.first}
|
8
|
+
end
|
9
|
+
)
|
10
|
+
|
11
|
+
def census
|
12
|
+
self.class.get("https://plusone.google.com/_/+1/fastbutton?url=#{@url}")
|
13
|
+
end
|
14
|
+
|
15
|
+
def count
|
16
|
+
results["shares"].to_i || 0
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
20
|
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
module Virility
|
2
|
-
|
2
|
+
class StumbleUpon < Strategy
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
4
|
+
parser(
|
5
|
+
Proc.new do |body, format|
|
6
|
+
MultiJson.decode(body)["result"]
|
7
|
+
end
|
8
|
+
)
|
9
|
+
|
10
|
+
def census
|
11
|
+
self.class.get("http://www.stumbleupon.com/services/1.01/badge.getinfo?url=#{@url}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def count
|
15
|
+
results["views"] || 0
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
19
|
end
|