rakismet 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ = 1.2.0
2
+ * Rakismet attribute mappings are now inheritable
1
3
  = 1.1.2
2
4
  * Explicitly load version
3
5
  = 1.1.1
data/lib/rakismet.rb CHANGED
@@ -55,8 +55,11 @@ module Rakismet
55
55
  args.merge!(:blog => Rakismet.url)
56
56
  akismet = URI.parse(call_url(function))
57
57
  _, response = Net::HTTP::Proxy(proxy_host, proxy_port).start(akismet.host) do |http|
58
- data = args.map { |k,v| "#{k}=#{CGI.escape(v.to_str)}" }.join('&')
59
- http.post(akismet.path, data, Rakismet.headers)
58
+ params = args.map do |k,v|
59
+ param = v.class < String ? v.to_str : v.to_s # for ActiveSupport::SafeBuffer and Nil, respectively
60
+ "#{k}=#{CGI.escape(param)}"
61
+ end
62
+ http.post(akismet.path, params.join('&'), Rakismet.headers)
60
63
  end
61
64
  response
62
65
  end
@@ -26,6 +26,11 @@ module Rakismet
26
26
  self.akismet_attrs[f] = v
27
27
  end
28
28
  end
29
+
30
+ def inherited(subclass)
31
+ super
32
+ subclass.rakismet_attrs akismet_attrs.dup
33
+ end
29
34
  end
30
35
 
31
36
  module InstanceMethods
@@ -1,3 +1,3 @@
1
1
  module Rakismet
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.0"
3
3
  end
data/rakismet.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.homepage = "http://github.com/joshfrench/rakismet"
12
12
  s.summary = "Akismet and TypePad AntiSpam integration for Rails."
13
13
  s.description = "Rakismet is the easiest way to integrate Akismet or TypePad's AntiSpam into your Rails app."
14
- s.date = "2011-08-11"
14
+ s.date = "2012-04-19"
15
15
 
16
16
  s.rubyforge_project = "rakismet"
17
17
 
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ PROC = proc { author.reverse }
4
+
5
+ class BlockAkismetModel
6
+ include Rakismet::Model
7
+ rakismet_attrs :author => PROC
8
+ end
9
+
10
+ describe BlockAkismetModel do
11
+
12
+ before do
13
+ @block = BlockAkismetModel.new
14
+ comment_attrs.each_pair { |k,v| @block.stub!(k).and_return(v) }
15
+ end
16
+
17
+ it "should accept a block" do
18
+ puts BlockAkismetModel.akismet_attrs.inspect
19
+ BlockAkismetModel.akismet_attrs[:comment_author].should eql(PROC)
20
+ end
21
+
22
+ it "should eval block with self = instance" do
23
+ data = @block.send(:akismet_data)
24
+ data[:comment_author].should eql(comment_attrs[:author].reverse)
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ MAPPED_PARAMS = { :comment_type => :type2, :author => :author2, :content => :content2,
4
+ :author_email => :author_email2, :author_url => :author_url2 }
5
+
6
+ class CustomAkismetModel
7
+ include Rakismet::Model
8
+ rakismet_attrs MAPPED_PARAMS.dup
9
+ end
10
+
11
+
12
+ describe CustomAkismetModel do
13
+ it "should override default mappings" do
14
+ [:comment_type, :author, :author_url, :author_email, :content].each do |field|
15
+ fieldname = field.to_s =~ %r(^comment_) ? field : "comment_#{field}".intern
16
+ CustomAkismetModel.akismet_attrs[fieldname].should eql(MAPPED_PARAMS[field])
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ EXTRA = { :extra => :extra, :another => lambda { } }
4
+
5
+ class ExtendedAkismetModel
6
+ include Rakismet::Model
7
+ rakismet_attrs EXTRA.dup
8
+ end
9
+
10
+ describe ExtendedAkismetModel do
11
+ it "should include additional attributes" do
12
+ [:extra, :another].each do |field|
13
+ ExtendedAkismetModel.akismet_attrs[field].should eql(EXTRA[field])
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,98 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe AkismetModel do
4
+
5
+ before do
6
+ @model = AkismetModel.new
7
+ comment_attrs.each_pair { |k,v| @model.stub!(k).and_return(v) }
8
+ end
9
+
10
+ it "should have default mappings" do
11
+ [:comment_type, :author, :author_email, :author_url, :content].each do |field|
12
+ fieldname = field.to_s =~ %r(^comment_) ? field : "comment_#{field}".intern
13
+ AkismetModel.akismet_attrs[fieldname].should eql(field)
14
+ end
15
+ end
16
+
17
+ it "should have request mappings" do
18
+ [:user_ip, :user_agent, :referrer].each do |field|
19
+ AkismetModel.akismet_attrs[field].should eql(field)
20
+ end
21
+ end
22
+
23
+ it "should populate comment type" do
24
+ @model.send(:akismet_data)[:comment_type].should == comment_attrs[:comment_type]
25
+ end
26
+
27
+ describe ".spam?" do
28
+
29
+ it "should use request variables from Rakismet.request if absent in model" do
30
+ [:user_ip, :user_agent, :referrer].each do |field|
31
+ @model.should_not respond_to(:field)
32
+ end
33
+ Rakismet.stub!(:request).and_return(request)
34
+ Rakismet.should_receive(:akismet_call).
35
+ with('comment-check', akismet_attrs.merge(:user_ip => '127.0.0.1',
36
+ :user_agent => 'RSpec',
37
+ :referrer => 'http://test.host/referrer'))
38
+ @model.spam?
39
+ end
40
+
41
+ it "should cache result of #spam?" do
42
+ Rakismet.should_receive(:akismet_call).once
43
+ @model.spam?
44
+ @model.spam?
45
+ end
46
+
47
+ it "should be true if comment is spam" do
48
+ Rakismet.stub!(:akismet_call).and_return('true')
49
+ @model.should be_spam
50
+ end
51
+
52
+ it "should be false if comment is not spam" do
53
+ Rakismet.stub!(:akismet_call).and_return('false')
54
+ @model.should_not be_spam
55
+ end
56
+
57
+ it "should set akismet_response" do
58
+ Rakismet.stub!(:akismet_call).and_return('response')
59
+ @model.spam?
60
+ @model.akismet_response.should eql('response')
61
+ end
62
+
63
+ it "should not throw an error if request vars are missing" do
64
+ Rakismet.stub!(:request).and_return(empty_request)
65
+ lambda { @model.spam? }.should_not raise_error(NoMethodError)
66
+ end
67
+ end
68
+
69
+
70
+ describe ".spam!" do
71
+ it "should call Base.akismet_call with submit-spam" do
72
+ Rakismet.should_receive(:akismet_call).with('submit-spam', akismet_attrs)
73
+ @model.spam!
74
+ end
75
+
76
+ it "should mutate #spam?" do
77
+ Rakismet.stub!(:akismet_call)
78
+ @model.instance_variable_set(:@_spam, false)
79
+ @model.spam!
80
+ @model.should be_spam
81
+ end
82
+ end
83
+
84
+ describe ".ham!" do
85
+ it "should call Base.akismet_call with submit-ham" do
86
+ Rakismet.should_receive(:akismet_call).with('submit-ham', akismet_attrs)
87
+ @model.ham!
88
+ end
89
+
90
+ it "should mutate #spam?" do
91
+ Rakismet.stub!(:akismet_call)
92
+ @model.instance_variable_set(:@_spam, true)
93
+ @model.ham!
94
+ @model.should_not be_spam
95
+ end
96
+ end
97
+
98
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ class RequestParams
4
+ include Rakismet::Model
5
+ attr_accessor :user_ip, :user_agent, :referrer
6
+ end
7
+
8
+ describe RequestParams do
9
+ before do
10
+ @model = RequestParams.new
11
+ attrs = comment_attrs(:user_ip => '192.168.0.1', :user_agent => 'Rakismet', :referrer => 'http://localhost/referrer')
12
+ attrs.each_pair { |k,v| @model.stub!(k).and_return(v) }
13
+ end
14
+
15
+ it "should use local values even if Rakismet.request is populated" do
16
+ Rakismet.stub(:request).and_return(request)
17
+ Rakismet.should_receive(:akismet_call).
18
+ with('comment-check', akismet_attrs.merge(:user_ip => '192.168.0.1',
19
+ :user_agent => 'Rakismet',
20
+ :referrer => 'http://localhost/referrer'))
21
+ @model.spam?
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ class Subclass < AkismetModel
4
+ end
5
+
6
+ describe Subclass do
7
+ it "should inherit parent's rakismet attrs" do
8
+ Subclass.akismet_attrs.should eql AkismetModel.akismet_attrs # key/value equality
9
+ end
10
+
11
+ it "should get a new copy of parent's rakismet attrs" do
12
+ Subclass.akismet_attrs.should_not equal AkismetModel.akismet_attrs # object equality
13
+ end
14
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,17 +1,34 @@
1
1
  require File.expand_path "lib/rakismet"
2
+ require 'ostruct'
2
3
 
3
4
  RSpec.configure do |config|
4
5
  config.mock_with :rspec
5
6
  end
6
7
 
7
- class Class
8
- def subclass(base_name, &body)
9
- klass = Class.new(self)
10
- class_name = "#{self.name}_#{base_name}"
11
- instance_eval do
12
- const_set(class_name, klass)
13
- end
14
- klass.instance_eval(&body)
15
- klass
16
- end
8
+ class AkismetModel
9
+ include Rakismet::Model
17
10
  end
11
+
12
+ def comment_attrs(attrs={})
13
+ { :comment_type => 'test', :author => 'Rails test',
14
+ :author_email => 'test@test.host', :author_url => 'test.host',
15
+ :content => 'comment content', :blog => Rakismet.url }.merge(attrs)
16
+ end
17
+
18
+ def akismet_attrs(attrs={})
19
+ { :comment_type => 'test', :comment_author_email => 'test@test.host',
20
+ :comment_author => 'Rails test', :comment_author_url => 'test.host',
21
+ :comment_content => 'comment content' }.merge(attrs)
22
+ end
23
+
24
+ def request
25
+ OpenStruct.new(:user_ip => '127.0.0.1',
26
+ :user_agent => 'RSpec',
27
+ :referrer => 'http://test.host/referrer')
28
+ end
29
+
30
+ def empty_request
31
+ OpenStruct.new(:user_ip => nil,
32
+ :user_agent => nil,
33
+ :referrer => nil)
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rakismet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-11 00:00:00.000000000Z
12
+ date: 2012-04-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Rakismet is the easiest way to integrate Akismet or TypePad's AntiSpam
15
15
  into your Rails app.
@@ -32,8 +32,13 @@ files:
32
32
  - lib/rakismet/version.rb
33
33
  - rakismet.gemspec
34
34
  - spec/.rspec
35
+ - spec/models/block_params_spec.rb
36
+ - spec/models/custom_params_spec.rb
37
+ - spec/models/extended_params_spec.rb
38
+ - spec/models/rakismet_model_spec.rb
39
+ - spec/models/request_params_spec.rb
40
+ - spec/models/subclass_spec.rb
35
41
  - spec/rakismet_middleware_spec.rb
36
- - spec/rakismet_model_spec.rb
37
42
  - spec/rakismet_spec.rb
38
43
  - spec/spec_helper.rb
39
44
  homepage: http://github.com/joshfrench/rakismet
@@ -56,12 +61,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
61
  version: '0'
57
62
  requirements: []
58
63
  rubyforge_project: rakismet
59
- rubygems_version: 1.8.6
64
+ rubygems_version: 1.8.22
60
65
  signing_key:
61
66
  specification_version: 3
62
67
  summary: Akismet and TypePad AntiSpam integration for Rails.
63
68
  test_files:
69
+ - spec/models/block_params_spec.rb
70
+ - spec/models/custom_params_spec.rb
71
+ - spec/models/extended_params_spec.rb
72
+ - spec/models/rakismet_model_spec.rb
73
+ - spec/models/request_params_spec.rb
74
+ - spec/models/subclass_spec.rb
64
75
  - spec/rakismet_middleware_spec.rb
65
- - spec/rakismet_model_spec.rb
66
76
  - spec/rakismet_spec.rb
67
77
  - spec/spec_helper.rb
@@ -1,246 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
- require 'ostruct'
3
-
4
- class AkismetModel
5
- include Rakismet::Model
6
- end
7
-
8
- class StoredParams
9
- include Rakismet::Model
10
- attr_accessor :user_ip, :user_agent, :referrer
11
- end
12
-
13
- describe AkismetModel do
14
-
15
- before do
16
- @model = AkismetModel.new
17
- comment_attrs.each_pair { |k,v| @model.stub!(k).and_return(v) }
18
- end
19
-
20
- it "should have default mappings" do
21
- [:comment_type, :author, :author_email, :author_url, :content].each do |field|
22
- fieldname = field.to_s =~ %r(^comment_) ? field : "comment_#{field}".intern
23
- AkismetModel.akismet_attrs[fieldname].should eql(field)
24
- end
25
- end
26
-
27
- it "should have request mappings" do
28
- [:user_ip, :user_agent, :referrer].each do |field|
29
- AkismetModel.akismet_attrs[field].should eql(field)
30
- end
31
- end
32
-
33
- it "should populate comment type" do
34
- @model.send(:akismet_data)[:comment_type].should == comment_attrs[:comment_type]
35
- end
36
-
37
- mapped_params = { :comment_type => :type2, :author => :author2, :content => :content2,
38
- :author_email => :author_email2, :author_url => :author_url2 }
39
-
40
- describe override = AkismetModel.subclass('Override') { rakismet_attrs(mapped_params.dup) } do
41
- it "should override default mappings" do
42
- [:comment_type, :author, :author_url, :author_email, :content].each do |field|
43
- fieldname = field.to_s =~ %r(^comment_) ? field : "comment_#{field}".intern
44
- override.akismet_attrs[fieldname].should eql(mapped_params[field])
45
- end
46
- end
47
- end
48
-
49
- extended_params = { :user_ip => :stored_ip, :user_agent => :stored_agent,
50
- :referrer => :stored_referrer }
51
-
52
- describe extended = AkismetModel.subclass('Extended') { rakismet_attrs(extended_params.dup) } do
53
-
54
- before do
55
- @extended = extended.new
56
- attrs = comment_attrs(:stored_ip => '127.0.0.1', :stored_agent => 'RSpec', :stored_referrer => 'http://test.host/')
57
- attrs.each_pair { |k,v| @extended.stub!(k).and_return(v) }
58
- Rakismet.stub(:request).and_return(empty_request)
59
- end
60
-
61
- it "should extend optional mappings" do
62
- [:user_ip, :user_agent, :referrer].each do |field|
63
- extended.akismet_attrs[field].should eql(extended_params[field])
64
- end
65
- end
66
-
67
- describe ".spam!" do
68
- it "should use stored request vars if available" do
69
- Rakismet.should_receive(:akismet_call).
70
- with('submit-spam', akismet_attrs(:user_ip => '127.0.0.1', :user_agent => 'RSpec',
71
- :referrer => 'http://test.host/'))
72
- @extended.spam!
73
- end
74
- end
75
-
76
- describe ".ham!" do
77
- it "should use stored request vars if available" do
78
- Rakismet.should_receive(:akismet_call).
79
- with('submit-ham', akismet_attrs(:user_ip => '127.0.0.1', :user_agent => 'RSpec',
80
- :referrer => 'http://test.host/'))
81
- @extended.ham!
82
- end
83
- end
84
- end
85
-
86
- @proc = proc { author.reverse }
87
- block_params = { :author => @proc }
88
-
89
- describe block = AkismetModel.subclass('Block') { rakismet_attrs(block_params) } do
90
-
91
- before do
92
- @block = block.new
93
- comment_attrs.each_pair { |k,v| @block.stub!(k).and_return(v) }
94
- end
95
-
96
- it "should accept a block" do
97
- block.akismet_attrs[:author].should eql(@proc)
98
- end
99
-
100
- it "should eval block with self = instance" do
101
- data = @block.send(:akismet_data)
102
- data[:comment_author].should eql(comment_attrs[:author].reverse)
103
- end
104
- end
105
-
106
- extra_params = { :extra => :extra, :another => lambda { } }
107
-
108
- describe extra = AkismetModel.subclass('ExtraParams') { rakismet_attrs(extra_params.dup) } do
109
- it "should map additional attributes" do
110
- [:extra, :another].each do |field|
111
- extra.akismet_attrs[field].should eql(extra_params[field])
112
- end
113
- end
114
- end
115
-
116
- string_params = { :comment_type => 'pingback' }
117
-
118
- describe string = AkismetModel.subclass('StringParams') { rakismet_attrs(string_params) } do
119
-
120
- before do
121
- @string = string.new
122
- comment_attrs.each_pair { |k,v| @string.stub!(k).and_return(v) }
123
- end
124
-
125
- it "should map string attributes" do
126
- @string.send(:akismet_data)[:comment_type].should eql('pingback')
127
- end
128
- end
129
-
130
- describe ".spam?" do
131
-
132
- it "should use request variables from Rakismet.request if absent in model" do
133
- [:user_ip, :user_agent, :referrer].each do |field|
134
- @model.should_not respond_to(:field)
135
- end
136
- Rakismet.stub!(:request).and_return(request)
137
- Rakismet.should_receive(:akismet_call).
138
- with('comment-check', akismet_attrs.merge(:user_ip => '127.0.0.1',
139
- :user_agent => 'RSpec',
140
- :referrer => 'http://test.host/referrer'))
141
- @model.spam?
142
- end
143
-
144
- it "should cache result of #spam?" do
145
- Rakismet.should_receive(:akismet_call).once
146
- @model.spam?
147
- @model.spam?
148
- end
149
-
150
- it "should be true if comment is spam" do
151
- Rakismet.stub!(:akismet_call).and_return('true')
152
- @model.should be_spam
153
- end
154
-
155
- it "should be false if comment is not spam" do
156
- Rakismet.stub!(:akismet_call).and_return('false')
157
- @model.should_not be_spam
158
- end
159
-
160
- it "should set akismet_response" do
161
- Rakismet.stub!(:akismet_call).and_return('response')
162
- @model.spam?
163
- @model.akismet_response.should eql('response')
164
- end
165
-
166
- it "should not throw an error if request vars are missing" do
167
- Rakismet.stub!(:request).and_return(empty_request)
168
- lambda { @model.spam? }.should_not raise_error(NoMethodError)
169
- end
170
- end
171
-
172
- describe StoredParams do
173
- before do
174
- @model = StoredParams.new
175
- comment_attrs.each_pair { |k,v| @model.stub!(k).and_return(v) }
176
- end
177
-
178
- it "should use local values even if Rakismet.request is populated" do
179
- Rakismet.stub!(:request).and_return(request)
180
- @model.user_ip = '192.168.0.1'
181
- @model.user_agent = 'Rakismet'
182
- @model.referrer = 'http://localhost/referrer'
183
-
184
- Rakismet.should_receive(:akismet_call).
185
- with('comment-check', akismet_attrs.merge(:user_ip => '192.168.0.1',
186
- :user_agent => 'Rakismet',
187
- :referrer => 'http://localhost/referrer'))
188
- @model.spam?
189
- end
190
- end
191
-
192
- describe ".spam!" do
193
- it "should call Base.akismet_call with submit-spam" do
194
- Rakismet.should_receive(:akismet_call).with('submit-spam', akismet_attrs)
195
- @model.spam!
196
- end
197
-
198
- it "should mutate #spam?" do
199
- Rakismet.stub!(:akismet_call)
200
- @model.instance_variable_set(:@_spam, false)
201
- @model.spam!
202
- @model.should be_spam
203
- end
204
- end
205
-
206
- describe ".ham!" do
207
- it "should call Base.akismet_call with submit-ham" do
208
- Rakismet.should_receive(:akismet_call).with('submit-ham', akismet_attrs)
209
- @model.ham!
210
- end
211
-
212
- it "should mutate #spam?" do
213
- Rakismet.stub!(:akismet_call)
214
- @model.instance_variable_set(:@_spam, true)
215
- @model.ham!
216
- @model.should_not be_spam
217
- end
218
- end
219
-
220
- private
221
-
222
- def comment_attrs(attrs={})
223
- { :comment_type => 'test', :author => 'Rails test',
224
- :author_email => 'test@test.host', :author_url => 'test.host',
225
- :content => 'comment content', :blog => Rakismet.url }.merge(attrs)
226
- end
227
-
228
- def akismet_attrs(attrs={})
229
- { :comment_type => 'test', :comment_author_email => 'test@test.host',
230
- :comment_author => 'Rails test', :comment_author_url => 'test.host',
231
- :comment_content => 'comment content' }.merge(attrs)
232
- end
233
-
234
- let(:request) {
235
- OpenStruct.new(:user_ip => '127.0.0.1',
236
- :user_agent => 'RSpec',
237
- :referrer => 'http://test.host/referrer')
238
- }
239
-
240
- let(:empty_request) {
241
- OpenStruct.new(:user_ip => nil,
242
- :user_agent => nil,
243
- :referrer => nil)
244
- }
245
-
246
- end