dalli_captcha 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/MIT-LICENSE +20 -20
  2. data/README.md +129 -127
  3. data/Rakefile +40 -40
  4. data/app/assets/javascripts/dalli_captcha/application.js +15 -15
  5. data/app/assets/stylesheets/dalli_captcha/application.css +13 -13
  6. data/app/controllers/dalli_captcha/application_controller.rb +4 -4
  7. data/app/controllers/dalli_captcha/captcha_controller.rb +13 -13
  8. data/app/helpers/dalli_captcha/application_helper.rb +4 -4
  9. data/app/views/layouts/dalli_captcha/application.html.erb +14 -14
  10. data/config/routes.rb +3 -3
  11. data/lib/dalli_captcha.rb +120 -120
  12. data/lib/dalli_captcha/controller.rb +14 -14
  13. data/lib/dalli_captcha/engine.rb +5 -5
  14. data/lib/dalli_captcha/image.rb +94 -94
  15. data/lib/dalli_captcha/key_handler.rb +41 -41
  16. data/lib/dalli_captcha/model.rb +45 -45
  17. data/lib/dalli_captcha/version.rb +3 -3
  18. data/lib/generators/install_generator.rb +21 -21
  19. data/lib/generators/templates/README +29 -29
  20. data/lib/generators/templates/dalli_captcha.rb +73 -73
  21. data/lib/tasks/dalli_captcha_tasks.rake +4 -4
  22. data/test/dalli_captcha_test.rb +7 -7
  23. data/test/dummy/README.rdoc +261 -261
  24. data/test/dummy/Rakefile +7 -7
  25. data/test/dummy/app/assets/javascripts/application.js +15 -15
  26. data/test/dummy/app/assets/stylesheets/application.css +13 -13
  27. data/test/dummy/app/controllers/application_controller.rb +3 -3
  28. data/test/dummy/app/helpers/application_helper.rb +2 -2
  29. data/test/dummy/app/views/layouts/application.html.erb +14 -14
  30. data/test/dummy/config.ru +4 -4
  31. data/test/dummy/config/application.rb +64 -64
  32. data/test/dummy/config/boot.rb +9 -9
  33. data/test/dummy/config/database.yml +25 -25
  34. data/test/dummy/config/environment.rb +5 -5
  35. data/test/dummy/config/environments/development.rb +37 -37
  36. data/test/dummy/config/environments/production.rb +67 -67
  37. data/test/dummy/config/environments/test.rb +37 -37
  38. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -7
  39. data/test/dummy/config/initializers/inflections.rb +15 -15
  40. data/test/dummy/config/initializers/mime_types.rb +5 -5
  41. data/test/dummy/config/initializers/secret_token.rb +7 -7
  42. data/test/dummy/config/initializers/session_store.rb +8 -8
  43. data/test/dummy/config/initializers/wrap_parameters.rb +14 -14
  44. data/test/dummy/config/locales/en.yml +5 -5
  45. data/test/dummy/config/routes.rb +4 -4
  46. data/test/dummy/log/development.log +95 -0
  47. data/test/dummy/public/404.html +26 -26
  48. data/test/dummy/public/422.html +26 -26
  49. data/test/dummy/public/500.html +25 -25
  50. data/test/dummy/script/rails +6 -6
  51. data/test/integration/navigation_test.rb +10 -10
  52. data/test/test_helper.rb +15 -15
  53. metadata +55 -54
@@ -1,41 +1,41 @@
1
- module DalliCaptcha
2
- class KeyHandler
3
- def initialize(captcha_key)
4
- @dalli = DalliCaptcha.dalli
5
- @captcha_key = captcha_key
6
- @options = DalliCaptcha.options
7
-
8
- @string_key = "#{@options[:dalli_scope]}:#{@captcha_key}:string"
9
- @locked_times_key = "#{@options[:dalli_scope]}:#{@captcha_key}:locked_times"
10
- end
11
-
12
- #set string in memcache
13
- def set(string)
14
- string = @options[:case_sensitive] ? string : string.downcase
15
- @dalli.set(@string_key,string,@options[:expired_time])
16
-
17
- #locked_times = @dalli.get(@locked_times_key).to_i
18
- #@dalli.incrby(@locked_times_key, 1)
19
- #@dalli.expire(@locked_times_key, @options[:locked_time]) if locked_times == 0
20
- end
21
-
22
- def delete
23
- @dalli.delete(@string_key)
24
- #@dalli.del(@locked_times_key)
25
- end
26
-
27
- def locked?
28
- locked_times = @dalli.get(@locked_times_key).to_i
29
- @captcha_key.blank? || (locked_times >= @options[:locked_times])
30
- end
31
-
32
- def valid?(captcha)
33
- string = @dalli.get(@string_key)
34
- if captcha.blank? || string.blank?
35
- return false
36
- else
37
- string == (@options[:case_sensitive] ? captcha : captcha.downcase)
38
- end
39
- end
40
- end
41
- end
1
+ module DalliCaptcha
2
+ class KeyHandler
3
+ def initialize(captcha_key)
4
+ @dalli = DalliCaptcha.dalli
5
+ @captcha_key = captcha_key
6
+ @options = DalliCaptcha.options
7
+
8
+ @string_key = "#{@options[:dalli_scope]}:#{@captcha_key}:string"
9
+ #@locked_times_key = "#{@options[:dalli_scope]}:#{@captcha_key}:locked_times"
10
+ end
11
+
12
+ #set string in memcache
13
+ def set(string)
14
+ string = @options[:case_sensitive] ? string : string.downcase
15
+ @dalli.set(@string_key,string,@options[:expired_time])
16
+
17
+ #locked_times = @dalli.get(@locked_times_key).to_i
18
+ #@dalli.incrby(@locked_times_key, 1)
19
+ #@dalli.expire(@locked_times_key, @options[:locked_time]) if locked_times == 0
20
+ end
21
+
22
+ def delete
23
+ @dalli.delete(@string_key)
24
+ #@dalli.del(@locked_times_key)
25
+ end
26
+
27
+ def locked?
28
+ locked_times = @dalli.get(@locked_times_key).to_i
29
+ @captcha_key.blank? || (locked_times >= @options[:locked_times])
30
+ end
31
+
32
+ def valid?(captcha)
33
+ string = @dalli.get(@string_key)
34
+ if captcha.blank? || string.blank?
35
+ return false
36
+ else
37
+ string == (@options[:case_sensitive] ? captcha : captcha.downcase)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,45 +1,45 @@
1
- module DalliCaptcha
2
- module Model
3
- extend ActiveSupport::Concern
4
-
5
- def self.included(base)
6
- base.extend(ClassMethods)
7
- base.send :validate, :check_captcha
8
- base.send :after_save, :delete_captcha
9
- base.send :attr_accessor, :captcha, :captcha_key
10
- end
11
-
12
- module ClassMethods
13
- end
14
-
15
- def key_handler
16
- @key_handler ||= DalliCaptcha::KeyHandler.new(captcha_key)
17
- end
18
-
19
- def check_captcha
20
- if without_captcha?
21
- @skip_captcha = false
22
- else
23
- errors.add(:captcha, DalliCaptcha::options[:error_message]) unless key_handler.valid?(captcha)
24
- end
25
- end
26
-
27
- def delete_captcha
28
- key_handler.delete
29
- end
30
-
31
- def without_captcha?
32
- !!@skip_captcha
33
- end
34
-
35
- def valid_without_captcha?
36
- @skip_captcha = true
37
- self.valid?
38
- end
39
-
40
- def save_without_captcha
41
- @skip_captcha = true
42
- self.save
43
- end
44
- end
45
- end
1
+ module DalliCaptcha
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ base.send :validate, :check_captcha
8
+ base.send :after_save, :delete_captcha
9
+ base.send :attr_accessor, :captcha, :captcha_key
10
+ end
11
+
12
+ module ClassMethods
13
+ end
14
+
15
+ def key_handler
16
+ @key_handler ||= DalliCaptcha::KeyHandler.new(captcha_key)
17
+ end
18
+
19
+ def check_captcha
20
+ if without_captcha?
21
+ @skip_captcha = false
22
+ else
23
+ errors.add(:captcha, DalliCaptcha::options[:error_message]) unless key_handler.valid?(captcha)
24
+ end
25
+ end
26
+
27
+ def delete_captcha
28
+ key_handler.delete
29
+ end
30
+
31
+ def without_captcha?
32
+ !!@skip_captcha
33
+ end
34
+
35
+ def valid_without_captcha?
36
+ @skip_captcha = true
37
+ self.valid?
38
+ end
39
+
40
+ def save_without_captcha
41
+ @skip_captcha = true
42
+ self.save
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
- module DalliCaptcha
2
- VERSION = "0.0.2"
3
- end
1
+ module DalliCaptcha
2
+ VERSION = "0.0.3"
3
+ end
@@ -1,21 +1,21 @@
1
-
2
- require 'rails/generators'
3
-
4
- module DalliCaptcha
5
- module Generators
6
- class InstallGenerator < Rails::Generators::Base
7
- source_root File.expand_path('../templates', __FILE__)
8
-
9
- desc "Copy DalliCaptcha default files"
10
-
11
- def copy_initializer
12
- copy_file "redis_captcha.rb", "config/initializers/redis_captcha.rb"
13
- end
14
-
15
- def show_readme
16
- readme "README"
17
- end
18
-
19
- end
20
- end
21
- end
1
+
2
+ require 'rails/generators'
3
+
4
+ module DalliCaptcha
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ desc "Copy DalliCaptcha default files"
10
+
11
+ def copy_initializer
12
+ copy_file "dalli_captcha.rb", "config/initializers/dalli_captcha.rb"
13
+ end
14
+
15
+ def show_readme
16
+ readme "README"
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -1,29 +1,29 @@
1
- ===============================================================================
2
-
3
- 1. Setup "config/initializers/dalli_captcha.rb"
4
-
5
- 2. Mount RedisCaptcha::Engine in your router.rb
6
-
7
- mount RedisCaptcha::Engine => '/captcha', :as => :captcha
8
-
9
- 3. Add to your controller
10
-
11
- before_filter :generate_captcha_key
12
-
13
- 4. Add to your model
14
-
15
- include RedisCaptcha::Model
16
-
17
- 5. If you set "config.active_record.whitelist_attributes = true" in "config/application.rb", remember to add attr_accessible to your model
18
-
19
- attr_accessible :captcha, :captcha_key
20
-
21
- 6. Add to your view
22
-
23
- <%= image_tag(captcha_path(:timestamp => Time.now.to_i), :alt => "captcha") %>
24
- <%= f.text_field :captcha %>
25
- <%= f.hidden_field :captcha_key, :value => session[:captcha_key] %>
26
-
27
- Enjoy it :)
28
-
29
- ===============================================================================
1
+ ===============================================================================
2
+
3
+ 1. Setup "config/initializers/dalli_captcha.rb"
4
+
5
+ 2. Mount DalliCaptcha::Engine in your router.rb
6
+
7
+ mount DalliCaptcha::Engine => '/captcha', :as => :captcha
8
+
9
+ 3. Add to your controller
10
+
11
+ before_filter :generate_captcha_key
12
+
13
+ 4. Add to your model
14
+
15
+ include DalliCaptcha::Model
16
+
17
+ 5. If you set "config.active_record.whitelist_attributes = true" in "config/application.rb", remember to add attr_accessible to your model
18
+
19
+ attr_accessible :captcha, :captcha_key
20
+
21
+ 6. Add to your view
22
+
23
+ <%= image_tag(captcha_path(:timestamp => Time.now.to_i), :alt => "captcha") %>
24
+ <%= f.text_field :captcha %>
25
+ <%= f.hidden_field :captcha_key, :value => session[:captcha_key] %>
26
+
27
+ Enjoy it :)
28
+
29
+ ===============================================================================
@@ -1,73 +1,73 @@
1
- DalliCaptcha.setup do |config|
2
-
3
- # ==> Dalli configuration
4
- config.dalli_config = {
5
- :host => "localhost:11211",
6
- :compress => true,
7
- :namespace => "captcha"
8
- }
9
-
10
- # ==> Dalli scope configuration
11
- config.dalli_scope = "DalliCaptcha"
12
-
13
- # ==> Dalli key expired time
14
- config.expired_time = 1800
15
-
16
- # ==> Dalli locked times and locked time
17
- config.locked_times = 30
18
- config.locked_time = 600
19
-
20
- # ==> ImageMagick path
21
- config.image_magick_path = ""
22
-
23
- # ==> Tempfile path
24
- config.tempfile_path = "/tmp"
25
-
26
- # ==> Tempfile name
27
- config.tempfile_name = "dalli_captcha"
28
-
29
- # ==> Tempfile type and content type
30
- config.tempfile_type = ".png"
31
- config.content_type = "image/png"
32
-
33
- # ==> Image width and height
34
- config.width = 120
35
- config.height = 30
36
-
37
- # ==> Char set
38
- # You can add/remove char you want
39
- config.chars = %w(2 3 4 5 6 7 9 a c d e f g h j k m n p q r s t w x y z A C D E F G H J K L M N P Q R S T X Y Z)
40
-
41
- # ==> String length
42
- # You can set the same value if you don't want it to be random length
43
- config.string_length = {
44
- :max => 6,
45
- :min => 4
46
- }
47
-
48
- # ==> Image font color and background
49
- config.font_color = "gray"
50
- config.background = "white"
51
-
52
- # ==> Lines on your captcha
53
- # You can set 0, 0 if you don't want to draw any line on your captcha
54
- config.line = {
55
- :max => 4,
56
- :min => 2
57
- }
58
- config.line_color = "gary"
59
-
60
- # ==> Swirl
61
- # http://www.imagemagick.org/Usage/warping/#swirl
62
- config.swirl_range = {
63
- :max => 20,
64
- :min => -20
65
- }
66
-
67
- # ==> Case sensitive
68
- config.case_sensitive = false
69
-
70
- # ==> Error message
71
- # if you want to use I18n in your model, you can set it to be nil
72
- config.error_message = "is invalid"
73
- end
1
+ DalliCaptcha.setup do |config|
2
+
3
+ # ==> Dalli configuration
4
+ config.dalli_config = {
5
+ :host => "localhost:11211",
6
+ :compress => true,
7
+ :namespace => "captcha"
8
+ }
9
+
10
+ # ==> Dalli scope configuration
11
+ config.dalli_scope = "DalliCaptcha"
12
+
13
+ # ==> Dalli key expired time
14
+ config.expired_time = 1800
15
+
16
+ # ==> Dalli locked times and locked time
17
+ config.locked_times = 30
18
+ config.locked_time = 600
19
+
20
+ # ==> ImageMagick path
21
+ config.image_magick_path = ""
22
+
23
+ # ==> Tempfile path
24
+ config.tempfile_path = "/tmp"
25
+
26
+ # ==> Tempfile name
27
+ config.tempfile_name = "dalli_captcha"
28
+
29
+ # ==> Tempfile type and content type
30
+ config.tempfile_type = ".png"
31
+ config.content_type = "image/png"
32
+
33
+ # ==> Image width and height
34
+ config.width = 120
35
+ config.height = 30
36
+
37
+ # ==> Char set
38
+ # You can add/remove char you want
39
+ config.chars = %w(2 3 4 5 6 7 9 a c d e f g h j k m n p q r s t w x y z A C D E F G H J K L M N P Q R S T X Y Z)
40
+
41
+ # ==> String length
42
+ # You can set the same value if you don't want it to be random length
43
+ config.string_length = {
44
+ :max => 6,
45
+ :min => 4
46
+ }
47
+
48
+ # ==> Image font color and background
49
+ config.font_color = "gray"
50
+ config.background = "white"
51
+
52
+ # ==> Lines on your captcha
53
+ # You can set 0, 0 if you don't want to draw any line on your captcha
54
+ config.line = {
55
+ :max => 4,
56
+ :min => 2
57
+ }
58
+ config.line_color = "gary"
59
+
60
+ # ==> Swirl
61
+ # http://www.imagemagick.org/Usage/warping/#swirl
62
+ config.swirl_range = {
63
+ :max => 20,
64
+ :min => -20
65
+ }
66
+
67
+ # ==> Case sensitive
68
+ config.case_sensitive = false
69
+
70
+ # ==> Error message
71
+ # if you want to use I18n in your model, you can set it to be nil
72
+ config.error_message = "is invalid"
73
+ end
@@ -1,4 +1,4 @@
1
- # desc "Explaining what the task does"
2
- # task :dalli_captcha do
3
- # # Task goes here
4
- # end
1
+ # desc "Explaining what the task does"
2
+ # task :dalli_captcha do
3
+ # # Task goes here
4
+ # end
@@ -1,7 +1,7 @@
1
- require 'test_helper'
2
-
3
- class DalliCaptchaTest < ActiveSupport::TestCase
4
- test "truth" do
5
- assert_kind_of Module, DalliCaptcha
6
- end
7
- end
1
+ require 'test_helper'
2
+
3
+ class DalliCaptchaTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, DalliCaptcha
6
+ end
7
+ end