winton-captcha 1.0.6 → 1.1.0
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 +45 -38
- data/captcha.gemspec +1 -1
- data/init.rb +1 -1
- data/lib/captcha/action.rb +1 -1
- data/lib/captcha/config.rb +11 -1
- metadata +1 -1
data/README.markdown
CHANGED
@@ -13,45 +13,28 @@ Install
|
|
13
13
|
|
14
14
|
<pre>
|
15
15
|
{
|
16
|
+
# Captcha colors
|
16
17
|
:colors => {
|
17
|
-
:background => '#
|
18
|
-
:font => '#
|
18
|
+
:background => '#FFFFFF',
|
19
|
+
:font => '#080288'
|
19
20
|
},
|
20
|
-
#
|
21
|
-
:count => 500,
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
:height => 31,
|
27
|
-
# canvas width (px)
|
28
|
-
:width => 90
|
29
|
-
},
|
30
|
-
:generate_every => 24 * 60 * 60,
|
31
|
-
# http://www.imagemagick.org/RMagick/doc/image2.html#implode
|
32
|
-
:implode => 0.2,
|
33
|
-
:letters => {
|
34
|
-
# text baseline (px)
|
35
|
-
:baseline => 24,
|
36
|
-
# number of letters in captcha
|
37
|
-
:count => 6,
|
38
|
-
:ignore => ['a','e','i','o','u','l','j','q'],
|
39
|
-
# font size (pts)
|
40
|
-
:points => 34,
|
41
|
-
# width of a character (used to decrease or increase space between characters) (px)
|
42
|
-
:width => 14
|
43
|
-
},
|
44
|
-
:ttf => File.expand_path("#{File.dirname(__FILE__)}/../../resources/captcha.ttf"),
|
45
|
-
# http://www.imagemagick.org/RMagick/doc/image3.html#wave
|
46
|
-
:wave => {
|
47
|
-
# range is used for randomness (px)
|
48
|
-
:wavelength => (20..70),
|
49
|
-
# distance between peak and valley of sin wave (px)
|
50
|
-
:amplitude => 3
|
51
|
-
}
|
21
|
+
# Number of captcha images to generate
|
22
|
+
:count => RAILS_ENV == 'production' ? 500 : 10,
|
23
|
+
# Where to write captchas
|
24
|
+
:destination => "#{RAILS_ROOT}/public/images/captchas",
|
25
|
+
# Generate new batch every day
|
26
|
+
:generate_every => RAILS_ENV == 'production' ? 24 * 60 * 60 : 10 ** 8
|
52
27
|
}
|
53
28
|
</pre>
|
54
29
|
|
30
|
+
See <code>lib/captcha/config.rb</code> for more options.
|
31
|
+
|
32
|
+
### Add public/images/captchas/* to your .gitignore file
|
33
|
+
|
34
|
+
<pre>
|
35
|
+
public/images/captchas/*
|
36
|
+
</pre>
|
37
|
+
|
55
38
|
### Create a captcha controller
|
56
39
|
|
57
40
|
script/generate controller Captchas
|
@@ -68,10 +51,34 @@ class CaptchasController < ApplicationController
|
|
68
51
|
end
|
69
52
|
</pre>
|
70
53
|
|
71
|
-
### Add
|
54
|
+
### Add acts\_as\_captcha to your model
|
55
|
+
|
56
|
+
<pre>
|
57
|
+
acts_as_captcha "does not have the correct code"
|
58
|
+
</pre>
|
59
|
+
|
60
|
+
The parameter is either the error added to the captcha input or nil to add a generic error to base.
|
61
|
+
|
62
|
+
### In your view
|
63
|
+
|
64
|
+
<pre>
|
65
|
+
<img src="/captcha?<%= Time.now.to_i %>" onclick="this.src = '/captcha/new?' + (new Date()).getTime()" />
|
66
|
+
<%= text_field_tag(:captcha) %>
|
67
|
+
</pre>
|
68
|
+
|
69
|
+
### In your controller
|
70
|
+
|
71
|
+
<pre>
|
72
|
+
model_instance.known_captcha = session[:captcha]
|
73
|
+
model_instance.captcha = params[:captcha]
|
74
|
+
</pre>
|
75
|
+
|
76
|
+
### More information
|
72
77
|
|
73
|
-
|
78
|
+
Captchas will generate and re-generate automatically when a Rails instance starts. Captcha keeps the old batch of captchas around until the next re-generate so users do not get 404s.
|
74
79
|
|
75
|
-
|
80
|
+
We used the following URLs in our view:
|
81
|
+
* <code>/captcha</code> to retrieve the image
|
82
|
+
* <code>/captcha/new</code> to grab a new image
|
76
83
|
|
77
|
-
|
84
|
+
Use <code>session[:captcha]</code> to store, retrieve, and reset the captcha code.
|
data/captcha.gemspec
CHANGED
data/init.rb
CHANGED
data/lib/captcha/action.rb
CHANGED
@@ -13,7 +13,7 @@ module Captcha
|
|
13
13
|
|
14
14
|
module InstanceMethods
|
15
15
|
def new
|
16
|
-
files =
|
16
|
+
files = Captcha::Config.newest_captchas
|
17
17
|
session[:captcha] = File.basename(files[rand(files.length)], '.jpg')
|
18
18
|
redirect_to(:action => :show)
|
19
19
|
end
|
data/lib/captcha/config.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module Captcha
|
2
2
|
class Config
|
3
3
|
|
4
|
-
PRODUCTION = defined?(RAILS_ENV)
|
4
|
+
PRODUCTION = defined?(RAILS_ENV) ?
|
5
|
+
RAILS_ENV == 'production' || RAILS_ENV == 'staging' : false
|
5
6
|
ROOT = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/" : ''
|
6
7
|
|
7
8
|
@@options = {
|
@@ -61,6 +62,15 @@ module Captcha
|
|
61
62
|
File.basename f, '.jpg'
|
62
63
|
end
|
63
64
|
end
|
65
|
+
|
66
|
+
def self.newest_captchas
|
67
|
+
captchas = self.captchas_ordered_by_modified
|
68
|
+
if captchas
|
69
|
+
captchas[0..@@options[:count]-1]
|
70
|
+
else
|
71
|
+
[]
|
72
|
+
end
|
73
|
+
end
|
64
74
|
|
65
75
|
def self.options
|
66
76
|
@@options
|