catptcha 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
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
- echo "gem 'catptcha'" >> Gemfile
17
- echo "load 'tasks/catptcha.rake'" >> Rakefile
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
- ruby script/generate catptcha_migration add_catptcha_tables
24
- rake db:migrate
23
+ ruby script/generate catptcha_migration add_catptcha_tables
24
+ rake db:migrate
25
25
 
26
26
  Seed the images.
27
27
 
28
- rake catptcha:seed
28
+ rake catptcha:seed
29
29
 
30
30
  Run the tests.
31
31
 
32
- rake db:test:prepare
33
- rake catptcha:test
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
 
@@ -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.3'
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 check_guesses *ids
16
- guesses = ids.flatten.compact
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.id in (?) and kittens = ?', guesses, true
41
+ 'catptcha_photos.key in (?) and kittens = ?', guesses, true
22
42
  ])
23
43
  end
24
44
  end
@@ -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(&:id))
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(&:id)[0,3])
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
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: catptcha
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zack Hobson