virility 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,13 @@
1
1
  module Virility
2
- class Twitter < Strategy
3
-
4
- def census
5
- self.class.get("http://urls.api.twitter.com/1/urls/count.json?url=#{@url}")
6
- end
7
-
8
- def count
9
- results["count"] || 0
10
- end
11
-
12
- end
2
+ class Twitter < Strategy
3
+
4
+ def census
5
+ self.class.get("http://urls.api.twitter.com/1/urls/count.json?url=#{@url}")
6
+ end
7
+
8
+ def count
9
+ results["count"] || 0
10
+ end
11
+
12
+ end
13
13
  end
@@ -1,98 +1,98 @@
1
1
  module Virility
2
- class Strategy
3
- include HTTParty
4
- include Virility::Supporter
2
+ class Strategy
3
+ include HTTParty
4
+ include Virility::Supporter
5
5
 
6
- attr_accessor :url, :response, :results
6
+ attr_accessor :url, :response, :results
7
7
 
8
- def initialize url
9
- @url = encode url
10
- @results = {}
11
- end
12
-
13
- #
14
- # Abstract Methods - Delete eventually
15
- #
8
+ def initialize url
9
+ @url = encode url
10
+ @results = {}
11
+ end
12
+
13
+ #
14
+ # Abstract Methods - Delete eventually
15
+ #
16
16
 
17
- def census
18
- raise "Abstract Method census called on #{self.class} - Please define this method"
19
- end
17
+ def census
18
+ raise "Abstract Method census called on #{self.class} - Please define this method"
19
+ end
20
20
 
21
- def count
22
- raise "Abstract Method count called on #{self.class} - Please define this method"
23
- end
24
-
25
- #
26
- # Poll
27
- #
28
-
29
- def poll
30
- call_strategy
31
- collect_results
32
- end
33
-
34
- #
35
- # Call Strategy
36
- #
37
-
38
- def call_strategy
39
- @response = census
40
- end
21
+ def count
22
+ raise "Abstract Method count called on #{self.class} - Please define this method"
23
+ end
24
+
25
+ #
26
+ # Poll
27
+ #
28
+
29
+ def poll
30
+ call_strategy
31
+ collect_results
32
+ end
33
+
34
+ #
35
+ # Call Strategy
36
+ #
37
+
38
+ def call_strategy
39
+ @response = census
40
+ end
41
41
 
42
- #
43
- # Results
44
- #
45
-
46
- def collect_results
47
- if respond_to?(:outcome)
48
- @results = valid_response_test ? outcome : {}
49
- else
50
- @results = valid_response_test ? @response.parsed_response : {}
51
- end
52
- end
42
+ #
43
+ # Results
44
+ #
45
+
46
+ def collect_results
47
+ if respond_to?(:outcome)
48
+ @results = valid_response_test ? outcome : {}
49
+ else
50
+ @results = valid_response_test ? @response.parsed_response : {}
51
+ end
52
+ end
53
53
 
54
- def results
55
- if @results.empty?
56
- begin
57
- poll
58
- rescue => e
59
- puts "[virility#poll] #{self.class.to_s} => #{e}"
60
- end
61
- end
62
- @results
63
- end
54
+ def results
55
+ if @results.empty?
56
+ begin
57
+ poll
58
+ rescue => e
59
+ puts "[virility#poll] #{self.class.to_s} => #{e}"
60
+ end
61
+ end
62
+ @results
63
+ end
64
64
 
65
- #
66
- # Dynamic Methods
67
- #
65
+ #
66
+ # Dynamic Methods
67
+ #
68
68
 
69
- def get_result key
70
- if result_exists?(key)
71
- results[key.to_s]
72
- else
73
- 0
74
- end
75
- end
69
+ def get_result key
70
+ if result_exists?(key)
71
+ results[key.to_s]
72
+ else
73
+ 0
74
+ end
75
+ end
76
76
 
77
- def result_exists? key
78
- !results[key.to_s].nil?
79
- end
77
+ def result_exists? key
78
+ !results[key.to_s].nil?
79
+ end
80
80
 
81
- def method_missing(name, *args, &block)
82
- if result_exists?(name)
83
- get_result(name)
84
- else
85
- 0
86
- end
87
- end
81
+ def method_missing(name, *args, &block)
82
+ if result_exists?(name)
83
+ get_result(name)
84
+ else
85
+ 0
86
+ end
87
+ end
88
88
 
89
- #
90
- # Parsed Response Test - Overwrite if needed
91
- #
89
+ #
90
+ # Parsed Response Test - Overwrite if needed
91
+ #
92
92
 
93
- def valid_response_test
94
- @response.respond_to?(:parsed_response) and @response.parsed_response.is_a?(Hash)
95
- end
93
+ def valid_response_test
94
+ @response.respond_to?(:parsed_response) and @response.parsed_response.is_a?(Hash)
95
+ end
96
96
 
97
- end
97
+ end
98
98
  end
@@ -1,55 +1,55 @@
1
1
  module Virility
2
- module Supporter
3
-
4
- #
5
- # URL Encoding / Decoding Methods
6
- #
7
-
8
- def encode url
9
- CGI.escape url
10
- end
11
-
12
- def url
13
- CGI.unescape @url
14
- end
15
-
16
- def escaped_url
17
- @url
18
- end
19
-
20
- #
21
- # Camelize / Underscore
22
- #
23
-
24
- def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
25
- if first_letter_in_uppercase
26
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
27
- else
28
- lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
29
- end
30
- end
31
-
32
- def underscore(camel_cased_word)
33
- word = camel_cased_word.to_s.dup
34
- word.gsub!(/::/, '/')
35
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
36
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
37
- word.tr!("-", "_")
38
- word.downcase!
39
- word
40
- end
41
-
42
- #
43
- # Convert Class Name To Appropriate Key Symbol
44
- #
45
-
46
- def symbolize_for_key(klass)
47
- underscore(klass.class.to_s.gsub(/Virility::/, '')).to_sym
48
- end
49
-
50
- def get_class_string(klass)
51
- File.basename(klass).gsub(/\.rb/,'')
52
- end
53
-
54
- end
2
+ module Supporter
3
+
4
+ #
5
+ # URL Encoding / Decoding Methods
6
+ #
7
+
8
+ def encode url
9
+ CGI.escape url
10
+ end
11
+
12
+ def url
13
+ CGI.unescape @url
14
+ end
15
+
16
+ def escaped_url
17
+ @url
18
+ end
19
+
20
+ #
21
+ # Camelize / Underscore
22
+ #
23
+
24
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
25
+ if first_letter_in_uppercase
26
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
27
+ else
28
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
29
+ end
30
+ end
31
+
32
+ def underscore(camel_cased_word)
33
+ word = camel_cased_word.to_s.dup
34
+ word.gsub!(/::/, '/')
35
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
36
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
37
+ word.tr!("-", "_")
38
+ word.downcase!
39
+ word
40
+ end
41
+
42
+ #
43
+ # Convert Class Name To Appropriate Key Symbol
44
+ #
45
+
46
+ def symbolize_for_key(klass)
47
+ underscore(klass.class.to_s.gsub(/Virility::/, '')).to_sym
48
+ end
49
+
50
+ def get_class_string(klass)
51
+ File.basename(klass).gsub(/\.rb/,'')
52
+ end
53
+
54
+ end
55
55
  end
@@ -1,3 +1,3 @@
1
1
  module Virility
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,116 +1,116 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Excitation" do
4
- before(:each) do
5
- @url = "http://creativeallies.com"
6
- end
7
-
8
- #
9
- # Initialization
10
- #
11
-
12
- context "initialization" do
13
- it "should raise an error if a URL is not set" do
14
- lambda {Virility::Excitation.new}.should raise_error
15
- end
16
- end
17
-
18
- #
19
- # Get Virility
20
- #
21
-
22
- # context "poll" do
23
- # it "should not raise an error" do
24
- # lambda {Virility::Excitation.new(@url).poll}.should_not raise_error
25
- # end
26
- # end
27
-
28
- #
29
- # Collect Strategies
30
- #
31
-
32
- context "collect_strategies" do
33
- it "should assign a hash to the strategies variable" do
34
- Virility::Excitation.new(@url).strategies.should be_a_kind_of Hash
35
- end
36
-
37
- it "strategies should be inherited from the Strategy" do
38
- Virility::Excitation.new(@url).strategies.first.last.should be_a_kind_of Virility::Strategy
39
- end
40
-
41
- it "should load all of the strategies" do
42
- Virility::Excitation.new(@url).strategies.count.should == Dir[File.join('lib', 'virility', 'strategies', '**', '*')].count { |file| File.file?(file) }
43
- end
44
- end
45
-
46
- #
47
- # Encode
48
- #
49
-
50
- context "encode" do
51
- it "should encode the url" do
52
- v = Virility::Excitation.new(@url)
53
- v.encode(@url).should == "http%3A%2F%2Fcreativeallies.com"
54
- end
55
- end
56
-
57
- #
58
- # Symbolize For Key
59
- #
60
-
61
- context "symbolize_for_key" do
62
- it "should return a symbol with the name of the class" do
63
- Virility::Excitation.new(@url).symbolize_for_key(Virility::Excitation.new(@url)).should == :excitation
64
- end
65
- end
66
-
67
- #
68
- # Dynamic Methods
69
- #
70
-
71
- describe "dynamic methods" do
72
- context "overall testing" do
73
- Virility::TESTING_STRATEGIES.each do |method, klass|
74
- it "should return a #{klass} object when the method #{method} is called" do
75
- Virility::Excitation.new(@url).send(method).should be_a_kind_of klass
76
- end
77
- end
78
-
79
- Virility::FAKE_TESTING_STRATEGIES.each do |method|
80
- it "should raise an error if the strategy (#{method}) does not exist" do
81
- lambda { Virility::Excitation.new(@url).send(method) }.should raise_error(Virility::UnknownStrategy, "#{method} Is Not A Known Strategy")
82
- end
83
- end
84
- end
85
-
86
- context "strategy_exists?" do
87
- Virility::TESTING_STRATEGIES.keys.each do |strategy|
88
- it "should return true for #{strategy}" do
89
- Virility::Excitation.new(@url).strategy_exists?(strategy).should be true
90
- end
91
- end
92
-
93
- Virility::FAKE_TESTING_STRATEGIES.each do |strategy|
94
- it "should return false for #{strategy}" do
95
- Virility::Excitation.new(@url).strategy_exists?(strategy).should be false
96
- end
97
- end
98
- end
99
-
100
- context "get_strategy" do
101
- Virility::TESTING_STRATEGIES.each do |method, klass|
102
- it "should return a #{klass} object when get_strategy is called with #{method}" do
103
- Virility::Excitation.new(@url).get_strategy(method).should be_a_kind_of klass
104
- end
105
- end
106
-
107
- Virility::FAKE_TESTING_STRATEGIES.each do |method|
108
- it "should raise an error if the strategy (#{method}) does not exist" do
109
- lambda { Virility::Excitation.new(@url).get_strategy(method) }.should raise_error(Virility::UnknownStrategy, "#{method} Is Not A Known Strategy")
110
- end
111
- end
112
- end
113
-
114
- end
4
+ before(:each) do
5
+ @url = "http://creativeallies.com"
6
+ end
7
+
8
+ #
9
+ # Initialization
10
+ #
11
+
12
+ context "initialization" do
13
+ it "should raise an error if a URL is not set" do
14
+ lambda {Virility::Excitation.new}.should raise_error
15
+ end
16
+ end
17
+
18
+ #
19
+ # Get Virility
20
+ #
21
+
22
+ # context "poll" do
23
+ # it "should not raise an error" do
24
+ # lambda {Virility::Excitation.new(@url).poll}.should_not raise_error
25
+ # end
26
+ # end
27
+
28
+ #
29
+ # Collect Strategies
30
+ #
31
+
32
+ context "collect_strategies" do
33
+ it "should assign a hash to the strategies variable" do
34
+ Virility::Excitation.new(@url).strategies.should be_a_kind_of Hash
35
+ end
36
+
37
+ it "strategies should be inherited from the Strategy" do
38
+ Virility::Excitation.new(@url).strategies.first.last.should be_a_kind_of Virility::Strategy
39
+ end
40
+
41
+ it "should load all of the strategies" do
42
+ Virility::Excitation.new(@url).strategies.count.should == Dir[File.join('lib', 'virility', 'strategies', '**', '*')].count { |file| File.file?(file) }
43
+ end
44
+ end
45
+
46
+ #
47
+ # Encode
48
+ #
49
+
50
+ context "encode" do
51
+ it "should encode the url" do
52
+ v = Virility::Excitation.new(@url)
53
+ v.encode(@url).should == "http%3A%2F%2Fcreativeallies.com"
54
+ end
55
+ end
56
+
57
+ #
58
+ # Symbolize For Key
59
+ #
60
+
61
+ context "symbolize_for_key" do
62
+ it "should return a symbol with the name of the class" do
63
+ Virility::Excitation.new(@url).symbolize_for_key(Virility::Excitation.new(@url)).should == :excitation
64
+ end
65
+ end
66
+
67
+ #
68
+ # Dynamic Methods
69
+ #
70
+
71
+ describe "dynamic methods" do
72
+ context "overall testing" do
73
+ Virility::TESTING_STRATEGIES.each do |method, klass|
74
+ it "should return a #{klass} object when the method #{method} is called" do
75
+ Virility::Excitation.new(@url).send(method).should be_a_kind_of klass
76
+ end
77
+ end
78
+
79
+ Virility::FAKE_TESTING_STRATEGIES.each do |method|
80
+ it "should raise an error if the strategy (#{method}) does not exist" do
81
+ lambda { Virility::Excitation.new(@url).send(method) }.should raise_error(Virility::UnknownStrategy, "#{method} Is Not A Known Strategy")
82
+ end
83
+ end
84
+ end
85
+
86
+ context "strategy_exists?" do
87
+ Virility::TESTING_STRATEGIES.keys.each do |strategy|
88
+ it "should return true for #{strategy}" do
89
+ Virility::Excitation.new(@url).strategy_exists?(strategy).should be true
90
+ end
91
+ end
92
+
93
+ Virility::FAKE_TESTING_STRATEGIES.each do |strategy|
94
+ it "should return false for #{strategy}" do
95
+ Virility::Excitation.new(@url).strategy_exists?(strategy).should be false
96
+ end
97
+ end
98
+ end
99
+
100
+ context "get_strategy" do
101
+ Virility::TESTING_STRATEGIES.each do |method, klass|
102
+ it "should return a #{klass} object when get_strategy is called with #{method}" do
103
+ Virility::Excitation.new(@url).get_strategy(method).should be_a_kind_of klass
104
+ end
105
+ end
106
+
107
+ Virility::FAKE_TESTING_STRATEGIES.each do |method|
108
+ it "should raise an error if the strategy (#{method}) does not exist" do
109
+ lambda { Virility::Excitation.new(@url).get_strategy(method) }.should raise_error(Virility::UnknownStrategy, "#{method} Is Not A Known Strategy")
110
+ end
111
+ end
112
+ end
113
+
114
+ end
115
115
 
116
116
  end