share_counts 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -169,11 +169,6 @@ And, if needed, your can clear the cached values:
169
169
 
170
170
  However the gem will namespace all the keys as you can see, in case you also use Redis for something else in the same app.
171
171
 
172
- == TODO
173
-
174
- * specs
175
-
176
-
177
172
  == Authors
178
173
 
179
174
  * Vito Botta ( http://vitobotta.com )
@@ -1,5 +1,5 @@
1
1
  %w( rubygems rest_client json nokogiri redis ).each{ |lib| require lib }
2
- %w( array caching common reddit ).each{ |file| load File.expand_path( File.join( File.dirname( __FILE__ ), "share_counts", "#{file}.rb" ) ) } # TODO: replace load with require
2
+ %w( caching common reddit ).each{ |file| load File.expand_path( File.join( File.dirname( __FILE__ ), "share_counts", "#{file}.rb" ) ) } # TODO: replace load with require
3
3
 
4
4
  module ShareCounts
5
5
 
@@ -1,6 +1,10 @@
1
1
  module ShareCounts
2
2
  module Common
3
3
 
4
+ def to_merged_hash array
5
+ array.inject({}){|r, c| r.merge!(c); r }
6
+ end
7
+
4
8
  private
5
9
 
6
10
  #
@@ -6,7 +6,7 @@ module ShareCounts
6
6
  def self.info_for url, raise_exceptions = false
7
7
  try("reddit-details", url, raise_exceptions) {
8
8
  data = extract_info from_json( "http://www.reddit.com/api/info.json", :url => url ), :selector => "data/children/data"
9
- data.select{|k, v| ["permalink", "score"].include? k }.map{|x| { x[0] => x[1] } }.to_hash
9
+ ShareCounts.to_merged_hash(data.select{|k, v| ["permalink", "score"].include? k }.map{|x| { x[0] => x[1] } })
10
10
  }
11
11
  end
12
12
 
@@ -4,7 +4,7 @@ $:.push File.expand_path("../lib/share_counts", __FILE__)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "share_counts"
7
- s.version = "0.1.1"
7
+ s.version = "0.1.2"
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Vito Botta"]
10
10
  s.email = ["vito@botta.name"]
@@ -19,6 +19,10 @@ class ActiveSupport::TestCase
19
19
  $stdin = @stdin = STDIN
20
20
  $stdout = @stdout = STDOUT
21
21
  end
22
+
23
+ def to_merged_hash array
24
+ ShareCounts.to_merged_hash array
25
+ end
22
26
  end
23
27
 
24
28
  class Reddit
@@ -2,12 +2,12 @@ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
2
2
 
3
3
  class RedditModuleTest < ActiveSupport::TestCase
4
4
  test ".info_for should return a hash with score and permalink for the given url" do
5
- stub_request(:get, Reddit.api).with(:query => Reddit.params.to_hash).to_return(:body => Reddit.url_info_json)
5
+ stub_request(:get, Reddit.api).with(:query => to_merged_hash(Reddit.params)).to_return(:body => Reddit.url_info_json)
6
6
  assert_equal({ "permalink" => "/r/ruby/comments/ffik5/geeky_cv_d/", "score" => 30}, ShareCounts::Reddit.info_for(SOME_URL))
7
7
  end
8
8
 
9
9
  test ".info_for with raise_exceptions=true should raise exception" do
10
- stub_request(:get, Reddit.api).with(:query => Reddit.params.to_hash).to_raise(Exception)
10
+ stub_request(:get, Reddit.api).with(:query => to_merged_hash(Reddit.params)).to_raise(Exception)
11
11
  assert_raise(Exception) { ShareCounts::Reddit.info_for(SOME_URL, true) }
12
12
  end
13
13
 
@@ -1,15 +1,14 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
2
2
 
3
3
  class ShareCountsTest < ActiveSupport::TestCase
4
-
5
4
  def stub_all
6
- stub_request(:get, Reddit.api).with(:query => Reddit.params.to_hash).to_return(:body => Reddit.json)
7
- stub_request(:get, Digg.api).with(:query => Digg.params.to_hash).to_return(:body => Digg.json)
8
- stub_request(:get, Facebook.api).with(:query => Facebook.params.to_hash).to_return(:body => Facebook.json)
9
- stub_request(:get, Twitter.api).with(:query => Twitter.params.to_hash).to_return(:body => Twitter.json)
10
- stub_request(:get, Linkedin.api).with(:query => Linkedin.params.to_hash).to_return(:body => Linkedin.json)
11
- stub_request(:get, GoogleBuzz.api).with(:query => GoogleBuzz.params.to_hash).to_return(:body => GoogleBuzz.json)
12
- stub_request(:get, StumbleUpon.api).with(:query => StumbleUpon.params.to_hash).to_return(:body => StumbleUpon.html)
5
+ stub_request(:get, Reddit.api).with(:query => to_merged_hash(Reddit.params)).to_return(:body => Reddit.json)
6
+ stub_request(:get, Digg.api).with(:query => to_merged_hash(Digg.params)).to_return(:body => Digg.json)
7
+ stub_request(:get, Facebook.api).with(:query => to_merged_hash(Facebook.params)).to_return(:body => Facebook.json)
8
+ stub_request(:get, Twitter.api).with(:query => to_merged_hash(Twitter.params)).to_return(:body => Twitter.json)
9
+ stub_request(:get, Linkedin.api).with(:query => to_merged_hash(Linkedin.params)).to_return(:body => Linkedin.json)
10
+ stub_request(:get, GoogleBuzz.api).with(:query => to_merged_hash(GoogleBuzz.params)).to_return(:body => GoogleBuzz.json)
11
+ stub_request(:get, StumbleUpon.api).with(:query => to_merged_hash(StumbleUpon.params)).to_return(:body => StumbleUpon.html)
13
12
  end
14
13
 
15
14
  test ".supported_networks returns the supported networks" do
@@ -17,7 +16,7 @@ class ShareCountsTest < ActiveSupport::TestCase
17
16
  end
18
17
 
19
18
  test ".make_request makes a request to a remove service and returns the response" do
20
- stub_request(:get, SOME_URL).with(:query => SOME_PARAMS.to_hash).to_return(:body => "---RESPONSE---")
19
+ stub_request(:get, SOME_URL).with(:query => to_merged_hash(SOME_PARAMS)).to_return(:body => "---RESPONSE---")
21
20
 
22
21
  assert_equal("---RESPONSE---", ShareCounts.send(:make_request, SOME_URL, *SOME_PARAMS ))
23
22
  assert_equal(0, @stdout.string.split("\n").size)
@@ -56,7 +55,7 @@ class ShareCountsTest < ActiveSupport::TestCase
56
55
  end
57
56
 
58
57
  test ".make_request should strip the callback call from the JSON response if a callback has been specified" do
59
- stub_request(:get, SOME_URL).with(:query => SOME_PARAMS.to_hash).
58
+ stub_request(:get, SOME_URL).with(:query => to_merged_hash(SOME_PARAMS)).
60
59
  to_return(:body => "myCallback(JSON_DATA);").then.
61
60
  to_return(:body => "myCallback(JSON_DATA)")
62
61
 
@@ -68,7 +67,7 @@ class ShareCountsTest < ActiveSupport::TestCase
68
67
 
69
68
  test ".from_json parses the JSON response returned by a remote service" do
70
69
  stub_request(:get, SOME_URL).to_return(:body => "{\"a\":1,\"b\":2}").then.to_return(:body => "[\"a\", \"b\", 1, 2]")
71
- stub_request(:get, SOME_URL).with(:query => SOME_PARAMS.to_hash).to_return(:body => "myCallback({\"a\":1,\"b\":2})")
70
+ stub_request(:get, SOME_URL).with(:query => to_merged_hash(SOME_PARAMS)).to_return(:body => "myCallback({\"a\":1,\"b\":2})")
72
71
 
73
72
  assert_equal({ "a" => 1, "b" => 2 }, ShareCounts.send(:from_json, SOME_URL))
74
73
  assert_equal(["a", "b", 1, 2], ShareCounts.send(:from_json, SOME_URL))
@@ -89,82 +88,82 @@ class ShareCountsTest < ActiveSupport::TestCase
89
88
  end
90
89
 
91
90
  test ".reddit should return the reddit score" do
92
- stub_request(:get, Reddit.api).with(:query => Reddit.params.to_hash).to_return(:body => Reddit.json)
91
+ stub_request(:get, Reddit.api).with(:query => to_merged_hash(Reddit.params)).to_return(:body => Reddit.json)
93
92
  assert_equal(31, ShareCounts.reddit(SOME_URL))
94
93
  end
95
94
 
96
95
  test ".reddit with raise_exceptions=true should raise exception" do
97
- stub_request(:get, Reddit.api).with(:query => Reddit.params.to_hash).to_raise(Exception)
96
+ stub_request(:get, Reddit.api).with(:query => to_merged_hash(Reddit.params)).to_raise(Exception)
98
97
  assert_raise(Exception) { ShareCounts.reddit(SOME_URL, true) }
99
98
  end
100
99
 
101
100
  test ".digg should return the digg score" do
102
- stub_request(:get, Digg.api).with(:query => Digg.params.to_hash).to_return(:body => Digg.json)
101
+ stub_request(:get, Digg.api).with(:query => to_merged_hash(Digg.params)).to_return(:body => Digg.json)
103
102
  assert_equal(1, ShareCounts.digg(SOME_URL))
104
103
  end
105
104
 
106
105
  test ".digg with raise_exceptions=true should raise exception" do
107
- stub_request(:get, Digg.api).with(:query => Digg.params.to_hash).to_raise(Exception)
106
+ stub_request(:get, Digg.api).with(:query => to_merged_hash(Digg.params)).to_raise(Exception)
108
107
  assert_raise(Exception) { ShareCounts.digg(SOME_URL, true) }
109
108
  end
110
109
 
111
110
  test ".twitter should return the twitter score" do
112
- stub_request(:get, Twitter.api).with(:query => Twitter.params.to_hash).to_return(:body => Twitter.json)
111
+ stub_request(:get, Twitter.api).with(:query => to_merged_hash(Twitter.params)).to_return(:body => Twitter.json)
113
112
  assert_equal(35, ShareCounts.twitter(SOME_URL))
114
113
  end
115
114
 
116
115
  test ".twitter with raise_exceptions=true should raise exception" do
117
- stub_request(:get, Twitter.api).with(:query => Twitter.params.to_hash).to_raise(Exception)
116
+ stub_request(:get, Twitter.api).with(:query => to_merged_hash(Twitter.params)).to_raise(Exception)
118
117
  assert_raise(Exception) { ShareCounts.twitter(SOME_URL, true) }
119
118
  end
120
119
 
121
120
  test ".facebook should return the facebook score" do
122
- stub_request(:get, Facebook.api).with(:query => Facebook.params.to_hash).to_return(:body => Facebook.json)
121
+ stub_request(:get, Facebook.api).with(:query => to_merged_hash(Facebook.params)).to_return(:body => Facebook.json)
123
122
  assert_equal(23, ShareCounts.facebook(SOME_URL))
124
123
  end
125
124
 
126
125
  test ".facebook with raise_exceptions=true should raise exception" do
127
- stub_request(:get, Facebook.api).with(:query => Facebook.params.to_hash).to_raise(Exception)
126
+ stub_request(:get, Facebook.api).with(:query => to_merged_hash(Facebook.params)).to_raise(Exception)
128
127
  assert_raise(Exception) { ShareCounts.facebook(SOME_URL, true) }
129
128
  end
130
129
 
131
130
  test ".linkedin should return the linkedin score" do
132
- stub_request(:get, Linkedin.api).with(:query => Linkedin.params.to_hash).to_return(:body => Linkedin.json)
131
+ stub_request(:get, Linkedin.api).with(:query => to_merged_hash(Linkedin.params)).to_return(:body => Linkedin.json)
133
132
  assert_equal(23, ShareCounts.linkedin(SOME_URL))
134
133
  end
135
134
 
136
135
  test ".linkedin with raise_exceptions=true should raise exception" do
137
- stub_request(:get, Linkedin.api).with(:query => Linkedin.params.to_hash).to_raise(Exception)
136
+ stub_request(:get, Linkedin.api).with(:query => to_merged_hash(Linkedin.params)).to_raise(Exception)
138
137
  assert_raise(Exception) { ShareCounts.linkedin(SOME_URL, true) }
139
138
  end
140
139
 
141
140
  test ".googlebuzz should return the googlebuzz score" do
142
- stub_request(:get, GoogleBuzz.api).with(:query => GoogleBuzz.params.to_hash).to_return(:body => GoogleBuzz.json)
141
+ stub_request(:get, GoogleBuzz.api).with(:query => to_merged_hash(GoogleBuzz.params)).to_return(:body => GoogleBuzz.json)
143
142
  assert_equal(1, ShareCounts.googlebuzz(SOME_URL))
144
143
  end
145
144
 
146
145
  test ".googlebuzz with raise_exceptions=true should raise exception" do
147
- stub_request(:get, GoogleBuzz.api).with(:query => GoogleBuzz.params.to_hash).to_raise(Exception)
146
+ stub_request(:get, GoogleBuzz.api).with(:query => to_merged_hash(GoogleBuzz.params)).to_raise(Exception)
148
147
  assert_raise(Exception) { ShareCounts.googlebuzz(SOME_URL, true) }
149
148
  end
150
149
 
151
150
  test ".stumbleupon should return the stumbleupon score" do
152
- stub_request(:get, StumbleUpon.api).with(:query => StumbleUpon.params.to_hash).to_return(:body => StumbleUpon.html)
151
+ stub_request(:get, StumbleUpon.api).with(:query => to_merged_hash(StumbleUpon.params)).to_return(:body => StumbleUpon.html)
153
152
  assert_equal(6, ShareCounts.stumbleupon(SOME_URL))
154
153
  end
155
154
 
156
155
  test ".stumbleupon with raise_exceptions=true should raise exception" do
157
- stub_request(:get, StumbleUpon.api).with(:query => StumbleUpon.params.to_hash).to_raise(Exception)
156
+ stub_request(:get, StumbleUpon.api).with(:query => to_merged_hash(StumbleUpon.params)).to_raise(Exception)
158
157
  assert_raise(Exception) { ShareCounts.stumbleupon(SOME_URL, true) }
159
158
  end
160
159
 
161
160
  test ".reddit_with_permalink should return a hash with Reddit score and permalink" do
162
- stub_request(:get, Reddit.api).with(:query => Reddit.params.to_hash).to_return(:body => Reddit.json)
161
+ stub_request(:get, Reddit.api).with(:query => to_merged_hash(Reddit.params)).to_return(:body => Reddit.json)
163
162
  assert_equal({ "permalink" => "/r/ruby/comments/ffik5/geeky_cv_d/", "score" => 31 }, ShareCounts.reddit_with_permalink(SOME_URL))
164
163
  end
165
164
 
166
165
  test ".reddit_with_permalink with raise_exceptions=true should raise exception" do
167
- stub_request(:get, Reddit.api).with(:query => Reddit.params.to_hash).to_raise(Exception)
166
+ stub_request(:get, Reddit.api).with(:query => to_merged_hash(Reddit.params)).to_raise(Exception)
168
167
  assert_raise(Exception) { ShareCounts.reddit_with_permalink(SOME_URL, true) }
169
168
  end
170
169
 
@@ -174,8 +173,8 @@ class ShareCountsTest < ActiveSupport::TestCase
174
173
  end
175
174
 
176
175
  test ".selected should only return scores for the networks specified" do
177
- stub_request(:get, Twitter.api).with(:query => Twitter.params.to_hash).to_return(:body => Twitter.json)
178
- stub_request(:get, Linkedin.api).with(:query => Linkedin.params.to_hash).to_return(:body => Linkedin.json)
176
+ stub_request(:get, Twitter.api).with(:query => to_merged_hash(Twitter.params)).to_return(:body => Twitter.json)
177
+ stub_request(:get, Linkedin.api).with(:query => to_merged_hash(Linkedin.params)).to_return(:body => Linkedin.json)
179
178
 
180
179
  assert_equal({ :twitter => 35, :linkedin => 23 }, ShareCounts.selected(SOME_URL, ["twitter", "linkedin"]))
181
180
  end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: share_counts
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 31
4
5
  prerelease:
5
- version: 0.1.1
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
6
11
  platform: ruby
7
12
  authors:
8
13
  - Vito Botta
@@ -21,6 +26,9 @@ dependencies:
21
26
  requirements:
22
27
  - - ">="
23
28
  - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
24
32
  version: "0"
25
33
  type: :runtime
26
34
  version_requirements: *id001
@@ -32,6 +40,9 @@ dependencies:
32
40
  requirements:
33
41
  - - ">="
34
42
  - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
35
46
  version: "0"
36
47
  type: :runtime
37
48
  version_requirements: *id002
@@ -43,6 +54,9 @@ dependencies:
43
54
  requirements:
44
55
  - - ">="
45
56
  - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
46
60
  version: "0"
47
61
  type: :runtime
48
62
  version_requirements: *id003
@@ -54,6 +68,9 @@ dependencies:
54
68
  requirements:
55
69
  - - ">="
56
70
  - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
57
74
  version: "0"
58
75
  type: :runtime
59
76
  version_requirements: *id004
@@ -65,6 +82,9 @@ dependencies:
65
82
  requirements:
66
83
  - - ">="
67
84
  - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
68
88
  version: "0"
69
89
  type: :development
70
90
  version_requirements: *id005
@@ -76,6 +96,9 @@ dependencies:
76
96
  requirements:
77
97
  - - ">="
78
98
  - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
79
102
  version: "0"
80
103
  type: :development
81
104
  version_requirements: *id006
@@ -87,6 +110,9 @@ dependencies:
87
110
  requirements:
88
111
  - - ">="
89
112
  - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
90
116
  version: "0"
91
117
  type: :development
92
118
  version_requirements: *id007
@@ -98,6 +124,9 @@ dependencies:
98
124
  requirements:
99
125
  - - ">="
100
126
  - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
101
130
  version: "0"
102
131
  type: :development
103
132
  version_requirements: *id008
@@ -109,6 +138,9 @@ dependencies:
109
138
  requirements:
110
139
  - - ">="
111
140
  - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
112
144
  version: "0"
113
145
  type: :development
114
146
  version_requirements: *id009
@@ -131,7 +163,6 @@ files:
131
163
  - autotest/discover.rb
132
164
  - autotest/rules.rb
133
165
  - lib/share_counts.rb
134
- - lib/share_counts/array.rb
135
166
  - lib/share_counts/caching.rb
136
167
  - lib/share_counts/common.rb
137
168
  - lib/share_counts/reddit.rb
@@ -162,12 +193,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
193
  requirements:
163
194
  - - ">="
164
195
  - !ruby/object:Gem::Version
196
+ hash: 3
197
+ segments:
198
+ - 0
165
199
  version: "0"
166
200
  required_rubygems_version: !ruby/object:Gem::Requirement
167
201
  none: false
168
202
  requirements:
169
203
  - - ">="
170
204
  - !ruby/object:Gem::Version
205
+ hash: 3
206
+ segments:
207
+ - 0
171
208
  version: "0"
172
209
  requirements: []
173
210
 
@@ -1,5 +0,0 @@
1
- class Array
2
- def to_hash
3
- @hash ||= self.inject({}){|r, c| r.merge!(c); r }
4
- end
5
- end