semantic_antispam 0.1.0 → 0.1.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/README.md +5 -0
- data/lib/semantic_antispam/antispam.rb +6 -4
- data/lib/semantic_antispam/version.rb +1 -1
- data/spec/semantic_antispam_spec.rb +9 -6
- data/spec/spec_helper.rb +9 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -22,6 +22,11 @@ If you are not using Rails or want to save this file in a different location:
|
|
22
22
|
|
23
23
|
Semantic::Antispam.config_file = Rails.root + '/antispam.yml'
|
24
24
|
|
25
|
+
You can also customize the validation error message by adding a key to your translations file:
|
26
|
+
|
27
|
+
semantic_antispam:
|
28
|
+
error_msg: "I'm not sure you're human. I'll give you another chance, please answer the question again."
|
29
|
+
|
25
30
|
## Usage
|
26
31
|
|
27
32
|
If you want to check the humanness of your commenters, just modify your `Comment` model:
|
@@ -32,12 +32,14 @@ module Semantic
|
|
32
32
|
module ClassMethods
|
33
33
|
def semantic_antispam
|
34
34
|
class_eval do
|
35
|
-
|
36
|
-
|
35
|
+
attr_accessor :antispam_answer
|
36
|
+
attr_writer :antispam_hash
|
37
37
|
|
38
38
|
private
|
39
39
|
def semantic_antispam_question
|
40
40
|
@semantic_antispam_question ||= Semantic::Antispam.questions.sample
|
41
|
+
@antispam_hash ||= @semantic_antispam_question[:hash]
|
42
|
+
@semantic_antispam_question
|
41
43
|
end
|
42
44
|
|
43
45
|
public
|
@@ -46,13 +48,13 @@ module Semantic
|
|
46
48
|
end
|
47
49
|
|
48
50
|
def antispam_hash
|
49
|
-
|
51
|
+
semantic_antispam_question[:hash]
|
50
52
|
end
|
51
53
|
|
52
54
|
validate :check_semantic_antispam, :on => :create
|
53
55
|
private
|
54
56
|
def check_semantic_antispam
|
55
|
-
errors.add :antispam_answer,
|
57
|
+
errors.add :antispam_answer, I18n.t("semantic_antispam.error_msg", :default=>"SPAM") unless antispam_answer and Semantic::Antispam.find(antispam_hash)[:answer].downcase == antispam_answer.downcase
|
56
58
|
end
|
57
59
|
end
|
58
60
|
end
|
@@ -24,12 +24,15 @@ describe Comment do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should succeed with a valid answer" do
|
27
|
-
|
28
|
-
when /sea/i then 'blue'
|
29
|
-
when /sky/i then 'blue'
|
30
|
-
when /france/i then 'paris'
|
31
|
-
end
|
32
|
-
@comment.antispam_answer = answer
|
27
|
+
@comment.antispam_answer = answer_correctly(@comment.antispam_question)
|
33
28
|
@comment.should be_valid
|
34
29
|
end
|
30
|
+
|
31
|
+
it "should work with a reused hash" do
|
32
|
+
@comment.antispam_answer = 'shit'
|
33
|
+
@comment.should_not be_valid
|
34
|
+
@new_comment = Comment.new(@comment.attributes)
|
35
|
+
@new_comment.antispam_answer = answer_correctly(@new_comment.antispam_question)
|
36
|
+
@new_comment.should be_valid
|
37
|
+
end
|
35
38
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -23,4 +23,13 @@ class Comment < ActiveRecord::Base
|
|
23
23
|
semantic_antispam
|
24
24
|
end
|
25
25
|
|
26
|
+
|
27
|
+
def answer_correctly(question)
|
28
|
+
answer = case question
|
29
|
+
when /sea/i then 'blue'
|
30
|
+
when /sky/i then 'blue'
|
31
|
+
when /france/i then 'paris'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
26
35
|
Semantic::Antispam.config_file = File.dirname(__FILE__) + '/antispam.yml'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: semantic_antispam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jordi Romero
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-12-23 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|