catptcha 0.0.3 → 0.0.4
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.markdown +21 -7
- data/lib/catptcha/photo_group.rb +0 -1
- data/lib/catptcha.rb +24 -4
- data/test/catptcha_test.rb +7 -2
- metadata +3 -3
data/README.markdown
CHANGED
@@ -13,24 +13,38 @@ From within your Rails application:
|
|
13
13
|
|
14
14
|
Add the gem to your Gemfile and load the rake tasks.
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
echo "gem 'catptcha'" >> Gemfile
|
17
|
+
echo "load 'tasks/catptcha.rake'" >> Rakefile
|
18
18
|
|
19
19
|
Put your flickr keys in config/flickr.yml, see flickr.example.yml.
|
20
20
|
|
21
21
|
Generate and run the migration.
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
ruby script/generate catptcha_migration add_catptcha_tables
|
24
|
+
rake db:migrate
|
25
25
|
|
26
26
|
Seed the images.
|
27
27
|
|
28
|
-
|
28
|
+
rake catptcha:seed
|
29
29
|
|
30
30
|
Run the tests.
|
31
31
|
|
32
|
-
|
33
|
-
|
32
|
+
rake db:test:prepare
|
33
|
+
rake catptcha:test
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
Include the partial in your form contents.
|
38
|
+
|
39
|
+
render :inline => Catptcha.puzzle_tags
|
40
|
+
|
41
|
+
Check the result in your controller.
|
42
|
+
|
43
|
+
if Catptcha.check_guess(params[:catptcha_guess])`.
|
44
|
+
# passed
|
45
|
+
else
|
46
|
+
# failed
|
47
|
+
end
|
34
48
|
|
35
49
|
## Author
|
36
50
|
|
data/lib/catptcha/photo_group.rb
CHANGED
@@ -16,7 +16,6 @@ class Catptcha::PhotoGroup < ActiveRecord::Base
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.random count
|
19
|
-
# TODO generate an identity/key column that isn't easy to guess
|
20
19
|
Catptcha::Photo.all(
|
21
20
|
:conditions => ['photo_group_id in (?)', all.map(&:id)],
|
22
21
|
:offset => rand(photos_count-count).to_i,
|
data/lib/catptcha.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Catptcha
|
2
|
-
VERSION = '0.0.
|
2
|
+
VERSION = '0.0.4'
|
3
3
|
|
4
4
|
class << self
|
5
5
|
# Get a list of 3 kitten photos
|
@@ -12,13 +12,33 @@ module Catptcha
|
|
12
12
|
PhotoGroup.not_kittens.random(6)
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
|
15
|
+
def puzzle
|
16
|
+
(kittens + not_kittens).sort_by {rand}
|
17
|
+
end
|
18
|
+
|
19
|
+
def puzzle_tags
|
20
|
+
tags = '<div class="catptcha" style="width:350px"><p>Click the kittens!</p>'
|
21
|
+
puzzle.each do |photo|
|
22
|
+
tags << <<-EOT
|
23
|
+
<span class="catptcha_guess" style="float:left">
|
24
|
+
<input type="checkbox" id="catptcha_guess_#{photo.key}" name="catptcha_guess[]" value="#{ photo.key }" />
|
25
|
+
<label for="catptcha_guess_#{photo.key }">
|
26
|
+
<img src="#{ photo.url }" />
|
27
|
+
</label>
|
28
|
+
</span>
|
29
|
+
EOT
|
30
|
+
end
|
31
|
+
tags << '</div>'
|
32
|
+
tags
|
33
|
+
end
|
34
|
+
|
35
|
+
def check_guess *keys
|
36
|
+
guesses = keys.flatten.compact
|
17
37
|
if guesses.size != 3
|
18
38
|
return false
|
19
39
|
end
|
20
40
|
3 == Photo.count(:include => 'photo_group', :conditions => [
|
21
|
-
'catptcha_photos.
|
41
|
+
'catptcha_photos.key in (?) and kittens = ?', guesses, true
|
22
42
|
])
|
23
43
|
end
|
24
44
|
end
|
data/test/catptcha_test.rb
CHANGED
@@ -12,10 +12,15 @@ class CatptchaTest < Test::Unit::TestCase
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_kittens
|
15
|
-
assert Catptcha.check_guesses(Catptcha.kittens.map(&:
|
15
|
+
assert Catptcha.check_guesses(Catptcha.kittens.map(&:key))
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_not_kittens
|
19
|
-
assert !Catptcha.check_guesses(Catptcha.not_kittens.map(&:
|
19
|
+
assert !Catptcha.check_guesses(Catptcha.not_kittens.map(&:key)[0,3])
|
20
20
|
end
|
21
|
+
|
22
|
+
def test_puzzle
|
23
|
+
assert_equal 9, Catptcha.puzzle.size
|
24
|
+
end
|
25
|
+
|
21
26
|
end
|
metadata
CHANGED