nice_text_captcha 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,100 @@
1
+ Nice Text Captcha
2
+ =================
3
+
4
+ by Neil Smith
5
+
6
+ About
7
+ -----
8
+
9
+ This generates text-based CATPCHAs that are "nice", by my own personal
10
+ subjective definition!
11
+
12
+ Questions are purposely simple, and examples would include:
13
+
14
+ * How many letters are there in the word 'White'?
15
+ * In the word "tiger", what is the letter in the 5th position?
16
+ * How many animals are in the words: "cat", 'Orange', and "dolphin"?
17
+ * What is 'four' + "nine"?
18
+
19
+ Why Another CAPTCHA Solution?
20
+ -----------------------------
21
+
22
+ There are plenty of choices out there, but none quite met my personal needs (or
23
+ some client's needs) for one reason or another:
24
+
25
+ * I wanted a self-contained solution that wasn't reliant on a 3rd party service
26
+ * Relatedly, I didn't want to have to muck around with API keys to get it working
27
+ * I wanted a few different question types, not just simple maths-based ones
28
+ * I didn't want long/odd words and case that make the question difficult to
29
+ understand at first glance (so no "What's the first adverb in the list
30
+ discombobulation, deFeNEStraTE, DiscOncErTinGly, DiSiNTEGRATing. Enter the
31
+ name of a good Jack Black film if you don't think there are any adverbs. Enter
32
+ a surprising fact about cheese if you don't think Jack Black has done any
33
+ good films.")
34
+
35
+ Implementation
36
+ --------------
37
+
38
+ You call a method in your view which both generates a random question, and places
39
+ the possible answers into a flash[] object, stored in a SHA2 hash.
40
+
41
+ (It's stored in a flash, as Rails handles ageing the data after a request - if we
42
+ used the session, it would hang around for the duration of the session.)
43
+
44
+ Then, in your controller action, you call a method which extracts the answers from
45
+ the flash, and injects them into your model.
46
+
47
+ A validation method on the model finally checks the answer against the hashed
48
+ possible answers, and pushes an error onto the base of your model if the answer
49
+ doesn't match.
50
+
51
+ For numerical answers ("What is 'two' plus 'three'?"), both numbers ("3") and
52
+ words ("three") are accepted.
53
+
54
+ Requirements
55
+ ------------
56
+
57
+ * Tested against Rails 2.3 - currently untested against Rails 3.
58
+ * 'linguistics' gem for turning numbers into words and ordinals (gem install linguistics)
59
+ * 'Digest/SHA2' for hashing the possible answers.
60
+
61
+ Installation
62
+ ------------
63
+
64
+ Simply add:
65
+
66
+ config.gem 'nice_text_captcha'
67
+
68
+ to your environment.rb file, and install the gem via your preferred method.
69
+
70
+ Usage Instructions
71
+ ------------------
72
+
73
+ In your model, you need to add some validation:
74
+
75
+ class Thing < ActiveRecord::Base
76
+ validate :check_nice_text_captcha
77
+ end
78
+
79
+ In your controller, you need to add a call to populate the answers from the
80
+ data in the session:
81
+
82
+ class ThingsController < ActiveRecord::Base
83
+ def create
84
+ @thing = Thing.new(params[:thing]) # as normal
85
+ validate_nice_text_captcha_for(@thing)
86
+ end
87
+ end
88
+
89
+ Finally, in your view, you need to render the question by calling nice_text_captcha,
90
+ and give the user a text box for their answer:
91
+
92
+ <% form_for(@thing) do |f| %>
93
+ <!-- normal form elements here -->
94
+
95
+ <%= f.nice_text_captcha %> <!-- this will generate a label with the catcha text inside -->
96
+ <%= f.text_field :nice_text_captcha %>
97
+
98
+ <!-- probably a submit button here -->
99
+
100
+ <% end %>
@@ -1,4 +1,3 @@
1
- #config.gem "linguistics"
2
1
  require 'linguistics'
3
2
  Linguistics::use(:en)
4
3
 
@@ -9,13 +8,17 @@ require 'nice_text_captcha/action_controller_extensions'
9
8
  require 'nice_text_captcha/form_builder_extensions'
10
9
 
11
10
  require 'nice_text_captcha/types/default_question'
11
+ require 'nice_text_captcha/types/fixed_question'
12
12
  require 'nice_text_captcha/types/letter_position_question'
13
13
  require 'nice_text_captcha/types/maths_question'
14
14
  require 'nice_text_captcha/types/word_length_question'
15
15
  require 'nice_text_captcha/types/words_in_list_question'
16
16
 
17
+ # Mix in form builder helper
17
18
  ActionView::Helpers::FormBuilder.send :include, NiceTextCaptcha::FormBuilderExtensions
18
19
 
20
+ # Mix in validation
19
21
  ActiveRecord::Base.send :include, NiceTextCaptcha::ActiveRecordExtensions
20
22
 
23
+ # Mix in controller-controlled validation
21
24
  ActionController::Base.send :include, NiceTextCaptcha::ActionControllerExtensions
@@ -5,13 +5,16 @@ module NiceTextCaptcha
5
5
  attr_reader :question
6
6
  attr_reader :answers
7
7
 
8
+ NICE_TEXT_CAPTCHA_TYPE_DEFAULTS = [
9
+ Types::FixedQuestion,
10
+ Types::MathsQuestion,
11
+ Types::LetterPositionQuestion,
12
+ Types::WordsInListQuestion,
13
+ Types::WordLengthQuestion,
14
+ ]
15
+
8
16
  def initialize
9
- captcha = [
10
- Types::MathsQuestion,
11
- Types::LetterPositionQuestion,
12
- Types::WordsInListQuestion,
13
- Types::WordLengthQuestion,
14
- ].rand.new
17
+ captcha = (NICE_TEXT_CAPTCHA_TYPES rescue NICE_TEXT_CAPTCHA_TYPE_DEFAULTS).rand.new
15
18
  @question = captcha.question
16
19
  @answers = captcha.answers
17
20
  end
@@ -0,0 +1,25 @@
1
+ class NiceTextCaptcha::Types::FixedQuestion < NiceTextCaptcha::Types::DefaultQuestion
2
+
3
+ def initialize
4
+ question = random_question
5
+ @question = question.first
6
+ @answers = question.last
7
+ end
8
+
9
+ private
10
+
11
+ def random_question
12
+ [
13
+ ["Is water wet or dry?", ["wet"]],
14
+ ["Is snow hot or cold?", ["cold"]],
15
+ ["Do dogs have tails?", ["yes", "y"]],
16
+ ["How many noses have you got?", ["1", "one"]],
17
+ ["Are elephants big or small?", ["big"]],
18
+ ["How many legs has a cat got?", ["4", "four"]],
19
+ ["Do shoes go on your hands or your feet?", ["feet", "your feet"]],
20
+ ].rand
21
+ end
22
+
23
+ end
24
+
25
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nice_text_captcha
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Neil Smith
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-06 00:00:00 +01:00
18
+ date: 2010-10-12 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -26,27 +26,28 @@ executables: []
26
26
  extensions: []
27
27
 
28
28
  extra_rdoc_files:
29
- - README
29
+ - README.markdown
30
30
  files:
31
- - README
31
+ - README.markdown
32
32
  - lib/nice_text_captcha/action_controller_extensions.rb
33
33
  - lib/nice_text_captcha/active_record_extensions.rb
34
34
  - lib/nice_text_captcha/captcha.rb
35
35
  - lib/nice_text_captcha/form_builder_extensions.rb
36
36
  - lib/nice_text_captcha/types/default_question.rb
37
+ - lib/nice_text_captcha/types/fixed_question.rb
37
38
  - lib/nice_text_captcha/types/letter_position_question.rb
38
39
  - lib/nice_text_captcha/types/maths_question.rb
39
40
  - lib/nice_text_captcha/types/word_length_question.rb
40
41
  - lib/nice_text_captcha/types/words_in_list_question.rb
41
42
  - lib/nice_text_captcha.rb
42
43
  has_rdoc: true
43
- homepage:
44
+ homepage: http://github.com/smugx/nice_text_captcha
44
45
  licenses: []
45
46
 
46
47
  post_install_message:
47
48
  rdoc_options:
48
49
  - --main
49
- - README
50
+ - README.markdown
50
51
  require_paths:
51
52
  - lib
52
53
  required_ruby_version: !ruby/object:Gem::Requirement
data/README DELETED
@@ -1,73 +0,0 @@
1
- Nice Text Captcha
2
- by Neil Smith
3
-
4
- About
5
- -----
6
-
7
- This generates text-based CATPCHAs that are "nice", by my own personal
8
- subjective definition!
9
-
10
- Questions are purposely simple, and examples would include:
11
-
12
- * How many letters are there in the word 'White'?
13
- * In the word "tiger", what is the letter in the 5th position?
14
- * How many animals are in the words: "cat", "orange", and "dolphin"?
15
- * What is 'four' + "nine"?
16
-
17
- Implementation
18
- --------------
19
-
20
- You call a method in your view which both generates a random question, and places
21
- the possible answers into a flash[] object, stored in a SHA2 hash.
22
-
23
- (It's stored in a flash, as Rails handles ageing the data after a request - if we
24
- used the session, it would hang around for the duration of the session.)
25
-
26
- Then, in your controller action, you call a method which extracts the answers from
27
- the flash, and injects them into your model.
28
-
29
- A validation method on the model finally checks the answer against the hashed
30
- possible answers, and pushes an error onto the base of your model if the answer
31
- doesn't match.
32
-
33
- For numerical answers ("What is 'two' plus 'three'?"), both numbers ("3") and
34
- words ("three") are accepted.
35
-
36
- Requirements
37
- ------------
38
-
39
- Tested against Rails 2.3 - currently untested against Rails 3.
40
- 'linguistics' gem for turning numbers into words and ordinals.
41
- 'Digest/SHA2' for hashing the possible answers.
42
-
43
- Instructions
44
- ------------
45
-
46
- In your model, you need to add some validation:
47
-
48
- class Thing < ActiveRecord::Base
49
- validate :check_nice_text_captcha
50
- end
51
-
52
- In your controller, you need to add a call to populate the answers from the
53
- data in the session:
54
-
55
- class ThingsController < ActiveRecord::Base
56
- def create
57
- @thing = Thing.new(params[:thing]) # as normal
58
- validate_nice_text_captcha_for(@thing)
59
- end
60
- end
61
-
62
- Finally, in your view, you need to render the question by calling nice_text_captcha,
63
- and give the user a text box for their answer:
64
-
65
- <% form_for(@thing) do |f| %>
66
- <!-- normal form elements here -->
67
-
68
- <%= f.nice_text_captcha %><br />
69
- <%= f.text_field :nice_text_captcha %><br />
70
-
71
- <!-- probably a submit button here -->
72
-
73
- <% end %>