winton-captcha 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -13,45 +13,28 @@ Install
13
13
 
14
14
  <pre>
15
15
  {
16
+ # Captcha colors
16
17
  :colors => {
17
- :background => '#1B1C20',
18
- :font => '#7385C5'
18
+ :background => '#FFFFFF',
19
+ :font => '#080288'
19
20
  },
20
- # number of captcha images to generate
21
- :count => 500,
22
- :destination => "#{RAILS_ROOT}/public/images/captchas"
23
- ),
24
- :dimensions => {
25
- # canvas height (px)
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 public/images/captchas/* to your .gitignore
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
- public/images/captchas/*
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
- Captchas will generate and re-generate automatically (see the <code>generate_every</code> option).
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
- Access <code>/captcha</code> to retrieve the image, <code>/captcha/new</code> to grab a new image, and <code>session[:captcha]</code> for the captcha code.
84
+ Use <code>session[:captcha]</code> to store, retrieve, and reset the captcha code.
data/captcha.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'captcha'
3
- s.version = '1.0.6'
3
+ s.version = '1.1.0'
4
4
  s.date = '2009-03-24'
5
5
 
6
6
  s.summary = "An Rmagick based, Google-style captcha generator"
data/init.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'captcha'
2
- if File.exists?("#{RAILS_ROOT}/lib/captcha_config.rb")
2
+ if defined?(RAILS_ROOT) && File.exists?("#{RAILS_ROOT}/lib/captcha_config.rb")
3
3
  require "#{RAILS_ROOT}/lib/captcha_config"
4
4
  end
5
5
 
@@ -13,7 +13,7 @@ module Captcha
13
13
 
14
14
  module InstanceMethods
15
15
  def new
16
- files = Dir["#{Captcha::Config.options[:destination]}/*.jpg"]
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
@@ -1,7 +1,8 @@
1
1
  module Captcha
2
2
  class Config
3
3
 
4
- PRODUCTION = defined?(RAILS_ENV) && RAILS_ENV == 'production'
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winton-captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Winton Welsh