virility 0.1.0 → 0.1.1

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.
@@ -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
- # 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
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
@@ -1,5 +1,5 @@
1
1
  module Virility
2
2
 
3
- class UnknownStrategy < StandardError; end
3
+ class UnknownStrategy < StandardError; end
4
4
 
5
5
  end
@@ -1,104 +1,104 @@
1
1
  module Virility
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
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
- class Delicious < Strategy
2
+ class Delicious < Strategy
3
3
 
4
- def census
5
- self.class.get("http://feeds.delicious.com/v2/json/urlinfo/data?url=#{@url}")
6
- end
4
+ def census
5
+ self.class.get("http://feeds.delicious.com/v2/json/urlinfo/data?url=#{@url}")
6
+ end
7
7
 
8
- def count
9
- results["total_posts"] || 0
10
- end
8
+ def count
9
+ results["total_posts"] || 0
10
+ end
11
11
 
12
- end
12
+ end
13
13
  end
@@ -1,21 +1,24 @@
1
1
  module Virility
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"
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
- def poll
6
- @response = self.class.get("#{BASE_URL}%22#{@url}%22")
7
- @results = valid_response_test ? @response.parsed_response["fql_query_response"]["link_stat"] : {"total_count" => 0}
8
- end
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
- def count
11
- results["total_count"] || 0
12
- end
13
-
14
- private
15
-
16
- def valid_response_test
17
- @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?
18
- end
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
- end
23
+ end
21
24
  end
@@ -1,19 +1,19 @@
1
1
  module Virility
2
- class Pinterest < Strategy
2
+ class Pinterest < Strategy
3
3
 
4
- parser(
5
- Proc.new do |body, format|
6
- MultiJson.decode(body.scan(/(\{.+\})/).flatten.first)
7
- end
8
- )
4
+ parser(
5
+ Proc.new do |body, format|
6
+ MultiJson.decode(body.scan(/(\{.+\})/).flatten.first)
7
+ end
8
+ )
9
9
 
10
- def census
11
- self.class.get("http://api.pinterest.com/v1/urls/count.json?url=#{@url}")
12
- end
10
+ def census
11
+ self.class.get("http://api.pinterest.com/v1/urls/count.json?url=#{@url}")
12
+ end
13
13
 
14
- def count
15
- results["count"] || 0
16
- end
14
+ def count
15
+ results["count"] || 0
16
+ end
17
17
 
18
- end
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
- 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
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
- class StumbleUpon < Strategy
2
+ class StumbleUpon < Strategy
3
3
 
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
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