defender 2.0.0 → 2.0.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.
- data/lib/defender/spammable.rb +16 -1
- data/lib/defender/version.rb +1 -1
- metadata +56 -87
- data/.document +0 -5
- data/.gitignore +0 -25
- data/Gemfile +0 -7
- data/LICENSE +0 -20
- data/README.md +0 -193
- data/Rakefile +0 -25
- data/defender.gemspec +0 -22
- data/spec/defender/spammable_spec.rb +0 -124
- data/spec/fake_defensio.rb +0 -36
- data/spec/spec.opts +0 -2
- data/spec/spec_helper.rb +0 -7
data/lib/defender/spammable.rb
CHANGED
@@ -44,14 +44,29 @@ module Defender
|
|
44
44
|
# database and in defensio.
|
45
45
|
# @option options [String] :api_key Your Defensio API key. Get one at
|
46
46
|
# defensio.com.
|
47
|
+
# @option options [Boolean] :test_mode Set Defender in test mode. See
|
48
|
+
# {#test_mode}.
|
47
49
|
#
|
48
50
|
def configure_defender(options)
|
49
51
|
keys = options.delete(:keys)
|
50
52
|
_defensio_keys.merge!(keys) unless keys.nil?
|
51
53
|
api_key = options.delete(:api_key)
|
52
54
|
Defender.api_key = api_key unless api_key.nil?
|
55
|
+
self.test_mode = options.delete(:test_mode)
|
53
56
|
end
|
54
57
|
|
58
|
+
##
|
59
|
+
# Set this to true to put Defender in "test mode". When in test mode, you
|
60
|
+
# can check if your code is working properly you can specify in the
|
61
|
+
# content field what kind of response you want. If you want a comment to
|
62
|
+
# be marked as spam with a spaminess of 0.85 you write [spam,0.85]
|
63
|
+
# somewhere in the content field of the document. If you want a malicious
|
64
|
+
# response with a spaminess of 0.99 you write [malicious,0.99] and for an
|
65
|
+
# innocent response you write [innocent,0.25]. This is the preferred way
|
66
|
+
# of testing, if you write spammy comments you might hurt the Defensio
|
67
|
+
# performance.
|
68
|
+
attr_accessor :test_mode
|
69
|
+
|
55
70
|
##
|
56
71
|
# Returns the key-attribute mapping used.
|
57
72
|
#
|
@@ -122,7 +137,7 @@ module Defender
|
|
122
137
|
end
|
123
138
|
data.merge!({
|
124
139
|
'platform' => 'ruby',
|
125
|
-
'type' => 'comment'
|
140
|
+
'type' => (self.class.test_mode ? 'test' : 'comment')
|
126
141
|
})
|
127
142
|
data.merge!(defensio_data) if defined?(@_defensio_data)
|
128
143
|
document = Defender.defensio.post_document(data).last
|
data/lib/defender/version.rb
CHANGED
metadata
CHANGED
@@ -1,127 +1,96 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: defender
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 2
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
version: 2.0.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.1
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Henrik Hodne
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2011-01-19 00:00:00 +00:00
|
12
|
+
date: 2011-07-02 00:00:00.000000000 +02:00
|
18
13
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
21
16
|
name: defensio
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: &2168669980 !ruby/object:Gem::Requirement
|
24
18
|
none: false
|
25
|
-
requirements:
|
19
|
+
requirements:
|
26
20
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
- 9
|
31
|
-
- 1
|
21
|
+
- !ruby/object:Gem::Version
|
32
22
|
version: 0.9.1
|
33
23
|
type: :runtime
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: activemodel
|
37
24
|
prerelease: false
|
38
|
-
|
25
|
+
version_requirements: *2168669980
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: activemodel
|
28
|
+
requirement: &2168669400 !ruby/object:Gem::Requirement
|
39
29
|
none: false
|
40
|
-
requirements:
|
30
|
+
requirements:
|
41
31
|
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
segments:
|
44
|
-
- 3
|
45
|
-
- 0
|
46
|
-
- 0
|
32
|
+
- !ruby/object:Gem::Version
|
47
33
|
version: 3.0.0
|
48
34
|
type: :runtime
|
49
|
-
|
50
|
-
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2168669400
|
37
|
+
- !ruby/object:Gem::Dependency
|
51
38
|
name: bundler
|
39
|
+
requirement: &2168668920 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
52
46
|
prerelease: false
|
53
|
-
|
47
|
+
version_requirements: *2168668920
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rake
|
50
|
+
requirement: &2168668400 !ruby/object:Gem::Requirement
|
54
51
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
- 1
|
60
|
-
- 0
|
61
|
-
- 0
|
62
|
-
version: 1.0.0
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
63
56
|
type: :development
|
64
|
-
|
65
|
-
|
66
|
-
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *2168668400
|
59
|
+
description:
|
60
|
+
email:
|
67
61
|
- dvyjones@dvyjones.com
|
68
62
|
executables: []
|
69
|
-
|
70
63
|
extensions: []
|
71
|
-
|
72
64
|
extra_rdoc_files: []
|
73
|
-
|
74
|
-
files:
|
75
|
-
- .document
|
76
|
-
- .gitignore
|
77
|
-
- Gemfile
|
78
|
-
- LICENSE
|
79
|
-
- README.md
|
80
|
-
- Rakefile
|
81
|
-
- defender.gemspec
|
82
|
-
- lib/defender.rb
|
65
|
+
files:
|
83
66
|
- lib/defender/defender_error.rb
|
84
67
|
- lib/defender/spammable.rb
|
85
68
|
- lib/defender/test/comment.rb
|
86
69
|
- lib/defender/version.rb
|
87
|
-
-
|
88
|
-
- spec/fake_defensio.rb
|
89
|
-
- spec/spec.opts
|
90
|
-
- spec/spec_helper.rb
|
70
|
+
- lib/defender.rb
|
91
71
|
has_rdoc: true
|
92
|
-
homepage:
|
72
|
+
homepage:
|
93
73
|
licenses: []
|
94
|
-
|
95
74
|
post_install_message:
|
96
75
|
rdoc_options: []
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
79
|
none: false
|
102
|
-
requirements:
|
103
|
-
- -
|
104
|
-
- !ruby/object:Gem::Version
|
105
|
-
|
106
|
-
|
107
|
-
version: "0"
|
108
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
85
|
none: false
|
110
|
-
requirements:
|
111
|
-
- -
|
112
|
-
- !ruby/object:Gem::Version
|
113
|
-
|
114
|
-
- 0
|
115
|
-
version: "0"
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
116
90
|
requirements: []
|
117
|
-
|
118
91
|
rubyforge_project:
|
119
|
-
rubygems_version: 1.
|
92
|
+
rubygems_version: 1.6.2
|
120
93
|
signing_key:
|
121
94
|
specification_version: 3
|
122
95
|
summary: ActiveModel plugin for Defensio.
|
123
|
-
test_files:
|
124
|
-
- spec/defender/spammable_spec.rb
|
125
|
-
- spec/fake_defensio.rb
|
126
|
-
- spec/spec.opts
|
127
|
-
- spec/spec_helper.rb
|
96
|
+
test_files: []
|
data/.document
DELETED
data/.gitignore
DELETED
data/Gemfile
DELETED
data/LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2009-2010 Henrik Hodne
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
DELETED
@@ -1,193 +0,0 @@
|
|
1
|
-
Defender
|
2
|
-
========
|
3
|
-
|
4
|
-
Defender is a wrapper for the [Defensio][0] spam filtering API. From
|
5
|
-
their own site:
|
6
|
-
|
7
|
-
> More than just another spam filter, Defensio also eliminates malware
|
8
|
-
> and other unwanted or risky content to fully protect your blog or Web
|
9
|
-
> 2.0 application.
|
10
|
-
|
11
|
-
Defensio is able to not only find spam, but also filter profanity and
|
12
|
-
other similarities. It can also see the difference between malicious
|
13
|
-
material and spammy material.
|
14
|
-
|
15
|
-
|
16
|
-
Overview
|
17
|
-
--------
|
18
|
-
|
19
|
-
With Defender you can submit documents to Defensio, which will look for
|
20
|
-
spam and malicious content in the documents.
|
21
|
-
|
22
|
-
A document contains content to be analyzed by Defensio, or that has been
|
23
|
-
analyzed.
|
24
|
-
|
25
|
-
To use Defender, you need to retrieve an API key from
|
26
|
-
[Defensio][4]. Then add Defender (`gem 'defender'`) to your Gemfile and run
|
27
|
-
`bundle install`. To set up Defender, open your Comment class and include
|
28
|
-
Defender::Spammable, and configure it by adding your API key. Your comment
|
29
|
-
class should look something like this:
|
30
|
-
|
31
|
-
class Comment < ActiveRecord::Base
|
32
|
-
include Defender::Spammable
|
33
|
-
configure_defender :api_key => '0123456789abcdef'
|
34
|
-
end
|
35
|
-
|
36
|
-
Now you need to add a few fields to your model. Defender requires a boolean
|
37
|
-
field named "spam" (this will be `true` for comments marked as spam, and
|
38
|
-
`false otherwise`), and a string field named "defensio_sig" (this will include
|
39
|
-
a unique identifier so you can later mark false positives and negatives).
|
40
|
-
Defender will also set the spaminess field if it exists (the spaminess field
|
41
|
-
should be a float that can range between 0.00 and 1.00, where 0.00 is the
|
42
|
-
least spammy and 1.00 is the most spammy). After you've done this, you're
|
43
|
-
probably done setting up Defender, although you should read on as there's some
|
44
|
-
more things you should know.
|
45
|
-
|
46
|
-
Defender will automatically get the comment body, author name, email, IP and
|
47
|
-
URL if you have them in your comment class with "standard names". Defender
|
48
|
-
will look for fields named "body", "content" and "comment" (in that order) for
|
49
|
-
the comment content, "author_name", "author" for the author name,
|
50
|
-
"author_email", "email" for the author email, "author_ip", "ip" for the author
|
51
|
-
IP, "author_url", "url" for the author URL. Only the comment content is the
|
52
|
-
required one of these. If you're not using those attribute names, look further
|
53
|
-
down in the readme under "Defining your own attribute names".
|
54
|
-
|
55
|
-
Not using ActiveRecord? No problem, Defender supports all libraries that
|
56
|
-
support ActiveModel, including Mongoid, MongoMapper and DataMapper. The syntax
|
57
|
-
is the exact same, just use the method your library uses to set up the fields
|
58
|
-
needed.
|
59
|
-
|
60
|
-
|
61
|
-
Defining your own attribute names
|
62
|
-
---------------------------------
|
63
|
-
|
64
|
-
Defensio supports a large amount of attributes you can send to it, and the
|
65
|
-
more you send the more accurately it can determine whether it's spam or not.
|
66
|
-
For some of these attributes, Defender will use conventions and try to find
|
67
|
-
the attribute, but not all are set up (look at
|
68
|
-
Defender::Spammable::DEFENSIO_KEYS for the exact keys it uses). If you are
|
69
|
-
using other attribute names, or want to add more to get more accurate spam
|
70
|
-
evaluation, you do that in the `configure_defender` method. Pass in another
|
71
|
-
option called `:keys`, which should be a hash of defensio key names and
|
72
|
-
attribute names. The list of defensio key names are after the code example,
|
73
|
-
and the attribute name is just a symbol. So if your comment content field is
|
74
|
-
called "the_comment_itself", your comment class should look like this:
|
75
|
-
|
76
|
-
class Comment
|
77
|
-
include Defender::Spammable
|
78
|
-
configure_defender :api_key => '0123456789abcdef', :keys => { 'content' => :the_comment_itself }
|
79
|
-
end
|
80
|
-
|
81
|
-
If you don't want to store all the information in the database, you can also
|
82
|
-
use the `defensio_data` method. In the model, before saving, call
|
83
|
-
`defensio_data` with a hash containing the data you want to send. The keys
|
84
|
-
should be strings, you can see all the possible values listed below. The
|
85
|
-
`defensio_data` method can be called several times with more data.
|
86
|
-
|
87
|
-
These are the keys defensio supports (at the time of writing, see
|
88
|
-
http://defensio.com/api for a completely up-to-date list):
|
89
|
-
|
90
|
-
* **author-email**: The email address of the author of the document.
|
91
|
-
* **author-ip**: The IP address of the author of the document.
|
92
|
-
* **author-logged-in**: Whether or not the user posting the document is logged
|
93
|
-
onto your Web site, either through your own authentication method or
|
94
|
-
through OpenID.
|
95
|
-
* **author-name**: The name of the author of the document.
|
96
|
-
* **author-openid**: The OpenID URL of the logged-on user. Must be used in
|
97
|
-
conjunction with user-logged-in=true.
|
98
|
-
* **author-trusted**: Whether or not the user is an administrator, moderator,
|
99
|
-
or editor of your Web site. Pass true only if you can guarantee that the
|
100
|
-
user has been authenticated, has a role of responsibility, and can be
|
101
|
-
trusted as a good Web citizen.
|
102
|
-
* **author-url**: The URL of the person posting the document.
|
103
|
-
* **browser-cookies**: Whether or not the Web browser used to post the
|
104
|
-
document (ie. the comment) has cookies enabled. If no such detection has
|
105
|
-
been made, leave this value empty.
|
106
|
-
* **browser-javascript**: Whether or not the Web browser used to post the
|
107
|
-
document (ie. the comment) has JavaScript enabled. If no such detection
|
108
|
-
has been made, leave this value empty.
|
109
|
-
* **document-permalink**: The URL to the document being posted.
|
110
|
-
* **http-headers**: Contains the HTTP headers sent with the request. You can
|
111
|
-
send a few values or all values. Because this information helps Defensio
|
112
|
-
determine if a document is innocent or not, the more headers you send, the
|
113
|
-
better. The format of this value is one key/value pair per line, each line
|
114
|
-
starting with the key followed by a colon and then the value.
|
115
|
-
* **parent-document-date**: The date the parent document was posted. For
|
116
|
-
example, on a blog, this would be the date the article related to the
|
117
|
-
comment (document) was posted. If you're using threaded comments, send the
|
118
|
-
date the article was posted, not the date the parent comment was posted.
|
119
|
-
* **parent-document-permalink**: The URL of the parent document. For example,
|
120
|
-
on a blog, this would be the URL the article related to the comment
|
121
|
-
(document) was posted.
|
122
|
-
* **referrer**: Provide the value of the HTTP_REFERER (note spelling) in this
|
123
|
-
field.
|
124
|
-
* **title**: Provide the title of the document being sent. For example, this
|
125
|
-
might be the title of a blog article.
|
126
|
-
|
127
|
-
|
128
|
-
Development
|
129
|
-
-----------
|
130
|
-
|
131
|
-
Want to help out on Defender?
|
132
|
-
|
133
|
-
First, you should clone the repo and run the features and specs:
|
134
|
-
|
135
|
-
git clone git://github.com/dvyjones/defender.git
|
136
|
-
cd defender
|
137
|
-
rake spec
|
138
|
-
|
139
|
-
Feel free to ping the mailing list if you have any problems and we'll
|
140
|
-
try to sort it out.
|
141
|
-
|
142
|
-
|
143
|
-
Contributing
|
144
|
-
------------
|
145
|
-
|
146
|
-
Once you've made your great commits:
|
147
|
-
|
148
|
-
1. [Fork][1] defender
|
149
|
-
2. Create a topic branch - `git checkout -b my_branch`
|
150
|
-
3. Push to your branch - `git push origin my_branch`
|
151
|
-
4. Create an [Issue][2] with a link to your branch
|
152
|
-
5. That's it!
|
153
|
-
|
154
|
-
You might want to checkout our [Contributing][cb] wiki page for
|
155
|
-
information on coding standards, new features, etc.
|
156
|
-
|
157
|
-
|
158
|
-
Mailing List
|
159
|
-
------------
|
160
|
-
|
161
|
-
To join the list simply send an email to <defender@librelist.com>. This
|
162
|
-
will subscribe you and send you information about your subscription,
|
163
|
-
include unsubscribe information.
|
164
|
-
|
165
|
-
The archive can be found at <http://librelist.com/browser/>.
|
166
|
-
|
167
|
-
|
168
|
-
Meta
|
169
|
-
----
|
170
|
-
|
171
|
-
* Code: `git clone git://github.com/dvyjones/defender.git`
|
172
|
-
* Home: <http://github.com/dvyjones/defender/>
|
173
|
-
* Docs: <http://rubydoc.info/github/dvyjones/defender/maser/frames>
|
174
|
-
* Bugs: <http://github.com/dvyjones/defender/issues>
|
175
|
-
* List: <defender@librelist.com>
|
176
|
-
* Gems: <http://rubygems.org/gems/defender>
|
177
|
-
|
178
|
-
This project uses [Semantic Versioning][sv].
|
179
|
-
|
180
|
-
|
181
|
-
Author
|
182
|
-
------
|
183
|
-
|
184
|
-
Henrik Hodne :: <dvyjones@binaryhex.com> :: @[dvyjones][5]
|
185
|
-
|
186
|
-
[0]: http://defensio.com
|
187
|
-
[1]: http://help.github.com/forking/
|
188
|
-
[2]: http://github.com/dvyjones/defender/issues
|
189
|
-
[3]: http://defensio.com/api
|
190
|
-
[4]: http://defensio.com/signup/
|
191
|
-
[5]: http://twitter.com/dvyjones
|
192
|
-
[sv]: http://semver.org
|
193
|
-
[cb]: http://wiki.github.com/dvyjones/defender/contributing
|
data/Rakefile
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# coding:utf-8
|
2
|
-
$:.unshift File.expand_path('../lib', __FILE__)
|
3
|
-
|
4
|
-
require 'bundler'
|
5
|
-
require 'rubygems'
|
6
|
-
require 'rubygems/specification'
|
7
|
-
require 'defender'
|
8
|
-
|
9
|
-
Bundler::GemHelper.install_tasks
|
10
|
-
|
11
|
-
require 'rspec/core/rake_task'
|
12
|
-
RSpec::Core::RakeTask.new(:spec) do |t|
|
13
|
-
t.rspec_opts = %w{-fs --color}
|
14
|
-
end
|
15
|
-
|
16
|
-
begin
|
17
|
-
require 'yard'
|
18
|
-
rescue LoadError
|
19
|
-
raise 'Run `gem install yard` to generate docs'
|
20
|
-
else
|
21
|
-
YARD::Rake::YardocTask.new do |conf|
|
22
|
-
conf.options = ['-mmarkdown', '-rREADME.md']
|
23
|
-
conf.files = ['lib/**/*.rb', '-', 'LICENSE']
|
24
|
-
end
|
25
|
-
end
|
data/defender.gemspec
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path("../lib/defender/version", __FILE__)
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = "defender"
|
6
|
-
s.version = Defender::VERSION
|
7
|
-
s.platform = Gem::Platform::RUBY
|
8
|
-
s.authors = ['Henrik Hodne']
|
9
|
-
s.email = ['dvyjones@dvyjones.com']
|
10
|
-
s.homepage = 'http://rubygems.org/gems/dvyjones'
|
11
|
-
s.summary = 'ActiveModel plugin for Defensio.'
|
12
|
-
s.description = 'An ActiveModel plugin for Defensio.'
|
13
|
-
|
14
|
-
s.add_dependency('defensio', '~> 0.9.1')
|
15
|
-
s.add_dependency('activemodel', '~> 3.0.0')
|
16
|
-
s.add_development_dependency('bundler', '~> 1.0.0')
|
17
|
-
|
18
|
-
s.files = `git ls-files`.split("\n")
|
19
|
-
s.test_files = `git ls-files -- spec/*`.split("\n")
|
20
|
-
|
21
|
-
s.require_path = ['lib']
|
22
|
-
end
|
@@ -1,124 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Defender
|
4
|
-
describe Spammable do
|
5
|
-
describe '.configure_defender' do
|
6
|
-
it 'sets the attribute-data key mappers' do
|
7
|
-
Comment.configure_defender(:keys => {'foo' => :bar, 'foobar' => :baz})
|
8
|
-
Comment._defensio_keys.should include({'foo' => :bar, 'foobar' => :baz})
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'sets the API key' do
|
12
|
-
Comment.configure_defender(:api_key => 'foobar')
|
13
|
-
Defender.api_key.should == 'foobar'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#spam?' do
|
18
|
-
it 'returns the "spam" attribute unless it is nil' do
|
19
|
-
comment_class = Class.new
|
20
|
-
comment_class.instance_eval { attr_accessor :spam }
|
21
|
-
def comment_class.before_save(*args, &block); end
|
22
|
-
comment_class.send(:define_method, :new_record?) { false }
|
23
|
-
comment_class.send(:include, Defender::Spammable)
|
24
|
-
comment = comment_class.new
|
25
|
-
comment.spam = true
|
26
|
-
comment.spam?.should be_true
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'returns nil for a new record' do
|
30
|
-
comment_class = Class.new
|
31
|
-
comment_class.instance_eval { attr_accessor :spam }
|
32
|
-
def comment_class.before_save(*args, &block); end
|
33
|
-
comment_class.send(:define_method, :new_record?) { true }
|
34
|
-
comment_class.send(:include, Defender::Spammable)
|
35
|
-
comment = comment_class.new
|
36
|
-
comment.spam?.should be_nil
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'raises a DefenderError if no spam attribute exists' do
|
40
|
-
comment_class = Class.new
|
41
|
-
def comment_class.before_save(*args, &block); end
|
42
|
-
comment_class.send(:define_method, :new_record?) { false }
|
43
|
-
comment_class.send(:include, Defender::Spammable)
|
44
|
-
comment = comment_class.new
|
45
|
-
expect { comment.spam? }.to raise_error(Defender::DefenderError)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe '#defensio_data' do
|
50
|
-
it 'merges in more data to be sent to Defensio' do
|
51
|
-
comment = Comment.new
|
52
|
-
comment.defensio_data({'foo' => 'FOOBAR', 'foobar' => 'baz'})
|
53
|
-
comment.defensio_data.should include({'foo' => 'FOOBAR', 'foobar' => 'baz'})
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'overwrites values repassed' do
|
57
|
-
comment = Comment.new
|
58
|
-
comment.defensio_data({'foo' => 'FOOBAR'})
|
59
|
-
comment.defensio_data({'foo' => 'baz'})
|
60
|
-
comment.defensio_data['foo'].should == 'baz'
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'leaves values that aren\'t modified' do
|
64
|
-
comment = Comment.new
|
65
|
-
comment.defensio_data({'foo' => 'baz'})
|
66
|
-
comment.defensio_data({'bar' => 'foobar'})
|
67
|
-
comment.defensio_data['foo'].should == 'baz'
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe '#_defender_before_save' do
|
72
|
-
it 'sets the attributes returned from defensio' do
|
73
|
-
comment = Comment.new
|
74
|
-
comment.body = '[innocent,0.9]'
|
75
|
-
comment.save
|
76
|
-
comment.spam.should be_false
|
77
|
-
comment.defensio_sig.should_not be_nil
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'sends the information off to Defensio' do
|
81
|
-
old_defensio = Defender.defensio
|
82
|
-
defensio = double('defensio')
|
83
|
-
defensio.should_receive(:post_document) { [200, {'signature' => 1234567890, 'spaminess' => 0.9, 'allow' => true}] }
|
84
|
-
Defender.defensio = defensio
|
85
|
-
comment = Comment.new
|
86
|
-
comment.body = 'Hello, world!'
|
87
|
-
comment.save
|
88
|
-
Defender.defensio = old_defensio
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe '#_pick_attribute' do
|
93
|
-
it 'returns the value of the attribute passed if it exists' do
|
94
|
-
comment = Comment.new
|
95
|
-
comment.body = 'Foobar!'
|
96
|
-
comment.send(:_pick_attribute, :body).should == 'Foobar!'
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'returns the value for the first attribute that exists in a list of attributes' do
|
100
|
-
comment = Comment.new
|
101
|
-
comment.body = 'Foobar!'
|
102
|
-
comment.send(:_pick_attribute, [:content, :body]).should == 'Foobar!'
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'returns nil if no attribute with the given names exists' do
|
106
|
-
comment = Comment.new
|
107
|
-
comment.send(:_pick_attribute, :bogus_attribute).should be_nil
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
describe '#_get_defensio_document' do
|
112
|
-
it 'retrieves the document from Defensio' do
|
113
|
-
old_defensio = Defender.defensio
|
114
|
-
defensio = double('defensio')
|
115
|
-
defensio.should_receive(:get_document) { [200, {'status' => 'succeed'}] }
|
116
|
-
Defender.defensio = defensio
|
117
|
-
comment = Comment.new
|
118
|
-
comment.defensio_sig = '0123456789abcdef'
|
119
|
-
comment.send(:_get_defensio_document)
|
120
|
-
Defender.defensio = old_defensio
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
data/spec/fake_defensio.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
class FakeDefensio
|
2
|
-
def initialize
|
3
|
-
@documents = {}
|
4
|
-
end
|
5
|
-
|
6
|
-
def post_document(data)
|
7
|
-
content = data[:content] || data['content']
|
8
|
-
classification, spaminess = content[1..-2].split(',')
|
9
|
-
signature = "#{rand}#{content}".hash
|
10
|
-
|
11
|
-
@documents[signature] = {
|
12
|
-
'api-version' => '2.0',
|
13
|
-
'status' => 'success',
|
14
|
-
'message' => '',
|
15
|
-
'signature' => signature,
|
16
|
-
'allow' => (classification == 'innocent'),
|
17
|
-
'classification' => classification,
|
18
|
-
'spaminess' => spaminess.to_f,
|
19
|
-
'profanity-match' => false
|
20
|
-
}
|
21
|
-
|
22
|
-
[200, @documents[signature]]
|
23
|
-
end
|
24
|
-
|
25
|
-
def get_document(signature)
|
26
|
-
if @documents.has_key?(signature)
|
27
|
-
[200, @documents[signature]]
|
28
|
-
else
|
29
|
-
[404,
|
30
|
-
'api-version' => '2.0',
|
31
|
-
'status' => 'failure',
|
32
|
-
'message' => 'document not found'
|
33
|
-
]
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
data/spec/spec.opts
DELETED