rakismet 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/README.md +4 -4
- data/VERSION.yml +4 -3
- data/lib/rakismet.rb +5 -2
- data/lib/rakismet/controller.rb +3 -10
- data/lib/rakismet/filter.rb +10 -0
- data/lib/rakismet/model.rb +14 -8
- data/spec/controllers/rakismet_controller_spec.rb +5 -16
- data/spec/models/base_spec.rb +1 -1
- data/spec/models/rakismet_filter_spec.rb +24 -0
- data/spec/models/rakismet_model_spec.rb +34 -16
- metadata +20 -5
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -72,8 +72,8 @@ however. If yours differ, just tell Rakismet what to call them:
|
|
72
72
|
class Comment
|
73
73
|
include Rakismet::Model
|
74
74
|
attr_accessor :commenter_name, :commenter_email
|
75
|
-
|
76
|
-
|
75
|
+
rakismet_attrs :author => :commenter_name,
|
76
|
+
:author_email => :commenter_email
|
77
77
|
end
|
78
78
|
|
79
79
|
Or you can pass in a proc, to access associations:
|
@@ -81,8 +81,8 @@ Or you can pass in a proc, to access associations:
|
|
81
81
|
class Comment < ActiveRecord::Base
|
82
82
|
include Rakismet::Model
|
83
83
|
belongs_to :author
|
84
|
-
|
85
|
-
|
84
|
+
rakismet_attrs :author => proc { author.name },
|
85
|
+
:author_email => proc { author.email }
|
86
86
|
end
|
87
87
|
|
88
88
|
Rakismet::Controller
|
data/VERSION.yml
CHANGED
data/lib/rakismet.rb
CHANGED
@@ -2,6 +2,10 @@ require 'net/http'
|
|
2
2
|
require 'uri'
|
3
3
|
require 'yaml'
|
4
4
|
|
5
|
+
require 'rakismet/model'
|
6
|
+
require 'rakismet/filter'
|
7
|
+
require 'rakismet/controller'
|
8
|
+
|
5
9
|
module Rakismet
|
6
10
|
def self.version
|
7
11
|
@version ||= begin
|
@@ -11,7 +15,7 @@ module Rakismet
|
|
11
15
|
end
|
12
16
|
|
13
17
|
class Base
|
14
|
-
cattr_accessor :valid_key, :
|
18
|
+
cattr_accessor :valid_key, :current_request
|
15
19
|
|
16
20
|
class << self
|
17
21
|
def validate_key
|
@@ -58,7 +62,6 @@ module Rakismet
|
|
58
62
|
end
|
59
63
|
|
60
64
|
Undefined = Class.new(NameError)
|
61
|
-
NoBinding = Class.new(NameError)
|
62
65
|
|
63
66
|
HEADERS = {
|
64
67
|
'User-Agent' => "Rails/#{Rails::VERSION::STRING} | Rakismet/#{Rakismet.version}",
|
data/lib/rakismet/controller.rb
CHANGED
@@ -4,22 +4,15 @@ module Rakismet
|
|
4
4
|
def self.included(base)
|
5
5
|
base.class_eval do
|
6
6
|
extend ClassMethods
|
7
|
-
around_filter
|
7
|
+
around_filter Rakismet::Filter
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
11
|
-
def rakismet(&block)
|
12
|
-
Rakismet::Base.rakismet_binding = binding
|
13
|
-
yield
|
14
|
-
Rakismet::Base.rakismet_binding = nil
|
15
|
-
end
|
16
|
-
private :rakismet
|
17
10
|
|
18
11
|
module ClassMethods
|
19
12
|
def rakismet_filter(opts={})
|
20
|
-
skip_filter
|
13
|
+
skip_filter Rakismet::Filter # in case we're inheriting/overriding an existing Rakismet filter
|
21
14
|
opts.assert_valid_keys(:only, :except)
|
22
|
-
self.around_filter
|
15
|
+
self.around_filter Rakismet::Filter, opts
|
23
16
|
end
|
24
17
|
end
|
25
18
|
|
data/lib/rakismet/model.rb
CHANGED
@@ -30,25 +30,31 @@ module Rakismet
|
|
30
30
|
|
31
31
|
module InstanceMethods
|
32
32
|
def spam?
|
33
|
-
|
33
|
+
if instance_variable_defined? :@_spam
|
34
|
+
@_spam
|
35
|
+
else
|
36
|
+
data = akismet_data
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
38
|
+
unless Rakismet::Base.current_request.nil?
|
39
|
+
{ :referrer => :referer, :user_ip => :remote_ip,
|
40
|
+
:user_agent => :user_agent }.each_pair do |k,v|
|
41
|
+
data[k] = Rakismet::Base.current_request.send(v) || ''
|
42
|
+
end
|
39
43
|
end
|
40
|
-
end
|
41
44
|
|
42
|
-
|
43
|
-
|
45
|
+
self.akismet_response = Rakismet::Base.akismet_call('comment-check', data)
|
46
|
+
@_spam = self.akismet_response == 'true'
|
47
|
+
end
|
44
48
|
end
|
45
49
|
|
46
50
|
def spam!
|
47
51
|
Rakismet::Base.akismet_call('submit-spam', akismet_data)
|
52
|
+
@_spam = true
|
48
53
|
end
|
49
54
|
|
50
55
|
def ham!
|
51
56
|
Rakismet::Base.akismet_call('submit-ham', akismet_data)
|
57
|
+
@_spam = false
|
52
58
|
end
|
53
59
|
|
54
60
|
private
|
@@ -11,31 +11,20 @@ class StubController < ActionController::Base
|
|
11
11
|
end
|
12
12
|
|
13
13
|
describe StubController do
|
14
|
-
|
15
|
-
it "should set Rakismet::Base.rakismet_binding" do
|
16
|
-
Rakismet::Base.should_receive(:rakismet_binding=).twice
|
17
|
-
get :one
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should return Rakismet::Base.rakismet_binding to nil after request" do
|
21
|
-
get :one
|
22
|
-
Rakismet::Base.rakismet_binding.should be_nil
|
23
|
-
end
|
24
|
-
|
25
14
|
it "should add around_filter" do
|
26
|
-
StubController.filter_chain.map(&:
|
15
|
+
StubController.filter_chain.map(&:method).should include(Rakismet::Filter)
|
27
16
|
end
|
28
17
|
end
|
29
18
|
|
30
19
|
describe StubController.subclass('OnlyActions') { rakismet_filter(:only => :one) } do
|
31
20
|
|
32
21
|
it "should add around filter to specified actions" do
|
33
|
-
Rakismet::Base.should_receive(:
|
22
|
+
Rakismet::Base.should_receive(:current_request=).twice
|
34
23
|
get :one
|
35
24
|
end
|
36
25
|
|
37
26
|
it "should not add around filter to unspecified actions" do
|
38
|
-
Rakismet::Base.should_not_receive(:
|
27
|
+
Rakismet::Base.should_not_receive(:current_request=)
|
39
28
|
get :two
|
40
29
|
end
|
41
30
|
end
|
@@ -43,12 +32,12 @@ end
|
|
43
32
|
describe StubController.subclass('ExceptActions') { rakismet_filter(:except => :one) } do
|
44
33
|
|
45
34
|
it "should not add around filter to specified actions" do
|
46
|
-
Rakismet::Base.should_not_receive(:
|
35
|
+
Rakismet::Base.should_not_receive(:current_request=)
|
47
36
|
get :one
|
48
37
|
end
|
49
38
|
|
50
39
|
it "should add around filter to other actions" do
|
51
|
-
Rakismet::Base.should_receive(:
|
40
|
+
Rakismet::Base.should_receive(:current_request=).twice
|
52
41
|
get :two
|
53
42
|
end
|
54
43
|
end
|
data/spec/models/base_spec.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
Rakismet::TestingError = Class.new(StandardError)
|
4
|
+
|
5
|
+
describe Rakismet::Base do
|
6
|
+
before do
|
7
|
+
@request = mock('request')
|
8
|
+
@controller = mock('controller', :request => @request)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should set Rakismet::Base.current_request" do
|
12
|
+
Rakismet::Base.should_receive(:current_request=).with(@request).ordered
|
13
|
+
Rakismet::Base.should_receive(:current_request=).with(nil).ordered
|
14
|
+
Rakismet::Filter.filter(@controller, &lambda{})
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not retain the request object in case of error" do
|
18
|
+
begin
|
19
|
+
Rakismet::Filter.filter(@controller, &lambda{ raise Rakismet::TestingError })
|
20
|
+
rescue Rakismet::TestingError
|
21
|
+
Rakismet::Base.current_request.should be_nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -11,7 +11,7 @@ class StoredParams
|
|
11
11
|
end
|
12
12
|
|
13
13
|
describe AkismetModel do
|
14
|
-
|
14
|
+
|
15
15
|
before do
|
16
16
|
@model = AkismetModel.new
|
17
17
|
comment_attrs.each_pair { |k,v| @model.stub!(k).and_return(v) }
|
@@ -125,7 +125,7 @@ describe AkismetModel do
|
|
125
125
|
describe ".spam?" do
|
126
126
|
|
127
127
|
before do
|
128
|
-
Rakismet::Base.
|
128
|
+
Rakismet::Base.current_request = request
|
129
129
|
end
|
130
130
|
|
131
131
|
it "should eval request variables in context of Base.rakismet_binding" do
|
@@ -136,6 +136,12 @@ describe AkismetModel do
|
|
136
136
|
@model.spam?
|
137
137
|
end
|
138
138
|
|
139
|
+
it "should cache result of #spam?" do
|
140
|
+
Rakismet::Base.should_receive(:akismet_call).once
|
141
|
+
@model.spam?
|
142
|
+
@model.spam?
|
143
|
+
end
|
144
|
+
|
139
145
|
it "should be true if comment is spam" do
|
140
146
|
Rakismet::Base.stub!(:akismet_call).and_return('true')
|
141
147
|
@model.should be_spam
|
@@ -153,14 +159,14 @@ describe AkismetModel do
|
|
153
159
|
end
|
154
160
|
|
155
161
|
it "should not throw an error if request vars are missing" do
|
156
|
-
Rakismet::Base.
|
162
|
+
Rakismet::Base.current_request = empty_request
|
157
163
|
lambda { @model.spam? }.should_not raise_error(NoMethodError)
|
158
164
|
end
|
159
165
|
end
|
160
166
|
|
161
167
|
describe StoredParams do
|
162
168
|
before do
|
163
|
-
Rakismet::Base.
|
169
|
+
Rakismet::Base.current_request = nil
|
164
170
|
@model = StoredParams.new
|
165
171
|
comment_attrs.each_pair { |k,v| @model.stub!(k).and_return(v) }
|
166
172
|
end
|
@@ -183,6 +189,13 @@ describe AkismetModel do
|
|
183
189
|
Rakismet::Base.should_receive(:akismet_call).with('submit-spam', akismet_attrs)
|
184
190
|
@model.spam!
|
185
191
|
end
|
192
|
+
|
193
|
+
it "should mutate #spam?" do
|
194
|
+
Rakismet::Base.stub!(:akismet_call)
|
195
|
+
@model.instance_variable_set(:@_spam, false)
|
196
|
+
@model.spam!
|
197
|
+
@model.should be_spam
|
198
|
+
end
|
186
199
|
end
|
187
200
|
|
188
201
|
describe ".ham!" do
|
@@ -190,6 +203,13 @@ describe AkismetModel do
|
|
190
203
|
Rakismet::Base.should_receive(:akismet_call).with('submit-ham', akismet_attrs)
|
191
204
|
@model.ham!
|
192
205
|
end
|
206
|
+
|
207
|
+
it "should mutate #spam?" do
|
208
|
+
Rakismet::Base.stub!(:akismet_call)
|
209
|
+
@model.instance_variable_set(:@_spam, true)
|
210
|
+
@model.ham!
|
211
|
+
@model.should_not be_spam
|
212
|
+
end
|
193
213
|
end
|
194
214
|
|
195
215
|
private
|
@@ -206,18 +226,16 @@ describe AkismetModel do
|
|
206
226
|
:comment_content => 'comment content' }.merge(attrs)
|
207
227
|
end
|
208
228
|
|
209
|
-
def
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
:referer => nil)
|
220
|
-
binding
|
229
|
+
def request
|
230
|
+
OpenStruct.new(:remote_ip => '127.0.0.1',
|
231
|
+
:user_agent => 'RSpec',
|
232
|
+
:referer => 'http://test.host/referrer')
|
233
|
+
end
|
234
|
+
|
235
|
+
def empty_request
|
236
|
+
OpenStruct.new(:remote_ip => nil,
|
237
|
+
:user_agent => nil,
|
238
|
+
:referer => nil)
|
221
239
|
end
|
222
240
|
|
223
241
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rakismet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 13
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 4
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Josh French
|
@@ -9,7 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-05-27 00:00:00 -04:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -33,9 +39,11 @@ files:
|
|
33
39
|
- install.rb
|
34
40
|
- lib/rakismet.rb
|
35
41
|
- lib/rakismet/controller.rb
|
42
|
+
- lib/rakismet/filter.rb
|
36
43
|
- lib/rakismet/model.rb
|
37
44
|
- spec/controllers/rakismet_controller_spec.rb
|
38
45
|
- spec/models/base_spec.rb
|
46
|
+
- spec/models/rakismet_filter_spec.rb
|
39
47
|
- spec/models/rakismet_model_spec.rb
|
40
48
|
- spec/spec.opts
|
41
49
|
- spec/spec_helper.rb
|
@@ -50,26 +58,33 @@ rdoc_options:
|
|
50
58
|
require_paths:
|
51
59
|
- lib
|
52
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
53
62
|
requirements:
|
54
63
|
- - ">="
|
55
64
|
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
56
68
|
version: "0"
|
57
|
-
version:
|
58
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
59
71
|
requirements:
|
60
72
|
- - ">="
|
61
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
62
77
|
version: "0"
|
63
|
-
version:
|
64
78
|
requirements: []
|
65
79
|
|
66
80
|
rubyforge_project: rakismet
|
67
|
-
rubygems_version: 1.3.
|
81
|
+
rubygems_version: 1.3.7
|
68
82
|
signing_key:
|
69
83
|
specification_version: 3
|
70
84
|
summary: Akismet and TypePad AntiSpam integration for Rails.
|
71
85
|
test_files:
|
72
86
|
- spec/controllers/rakismet_controller_spec.rb
|
73
87
|
- spec/models/base_spec.rb
|
88
|
+
- spec/models/rakismet_filter_spec.rb
|
74
89
|
- spec/models/rakismet_model_spec.rb
|
75
90
|
- spec/spec_helper.rb
|