easy_captcha 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # jeweler generated
12
+ pkg
13
+
14
+ .DS_Store
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,67 @@
1
+ == EasyCaptcha
2
+ A simple captcha implementation for rails 3 based on rmagick
3
+
4
+ == Installation
5
+ To install easy_captcha
6
+ gem install easy_captcha
7
+
8
+ Add following line to routes.rb
9
+ match "captcha" => EasyCaptcha::Controller
10
+
11
+ == Configuration
12
+ You can write this in "config/initializers/easy_captcha.rb", if you want to customize the default configuration
13
+
14
+ EasyCaptcha.setup do |config|
15
+ # Chars
16
+ # config.chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
17
+ # Length
18
+ # config.length = 6
19
+ # Font
20
+ # config.font_size = 24
21
+ # config.font_fill_color = '#333333'
22
+ # config.font_stroke_color = '#000000'
23
+ # config.font_stroke = 0
24
+ # config.font_family = File.expand_path('../../resources/afont.ttf', __FILE__)
25
+
26
+ # Image
27
+ #config.image_height = 40
28
+ #config.image_width = 140
29
+ #config.image_background_color = "#FFFFFF"
30
+
31
+ # Wave
32
+ # config.wave = true
33
+ # config.wave_length = (60..100)
34
+ # config.wave_amplitude = (3..5)
35
+
36
+ # Sketch
37
+ # config.sketch = true
38
+ # config.sketch_radius = 3
39
+ # config.sketch_sigma = 1
40
+
41
+ # Implode
42
+ # config.implode = 0.1
43
+
44
+ # Gaussian Blur
45
+ # config.blur = true
46
+ # config.blur_radius = 1
47
+ # config.blur_sigma = 2
48
+ end
49
+
50
+ == Example
51
+
52
+ <% form_tag '/' do %>
53
+ <% if request.post? %>
54
+ <p><%= valid_captcha?(params[:captcha]) ? 'valid' : 'invalid' %> captcha</p>
55
+ <% end %>
56
+ <p><%= captcha_tag %></p>
57
+ <p><%= text_field_tag :captcha %></p>
58
+ <p><%= submit_tag 'Validate' %></p>
59
+ <% end %>
60
+
61
+ == Maintainers
62
+
63
+ * Marco Scholl (http://github.com/traxanos)
64
+
65
+ == License
66
+
67
+ Copyright (c) 2010 Marco Scholl, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "easy_captcha"
8
+ gemspec.summary = "Captcha-Plugin for Rails"
9
+ gemspec.description = "Captcha-Plugin for Rails. IMage generate based on rmagick"
10
+ gemspec.email = "develop@marco-scholl.de"
11
+ gemspec.homepage = "http://github.com/traxanos/easy_captcha"
12
+ gemspec.authors = ["Marco Scholl"]
13
+ gemspec.add_development_dependency "rspec", ">= 1.2.9"
14
+ gemspec.add_runtime_dependency "rails", ">= 3.0.0"
15
+ gemspec.add_runtime_dependency "rmagick"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ begin
37
+ require 'reek/adapters/rake_task'
38
+ Reek::RakeTask.new do |t|
39
+ t.fail_on_error = true
40
+ t.verbose = false
41
+ t.source_files = 'lib/**/*.rb'
42
+ end
43
+ rescue LoadError
44
+ task :reek do
45
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
46
+ end
47
+ end
48
+
49
+ task :default => :spec
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "easy_captcha #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{easy_captcha}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Marco Scholl"]
12
+ s.date = %q{2010-09-04}
13
+ s.description = %q{Captcha-Plugin for Rails. IMage generate based on rmagick}
14
+ s.email = %q{develop@marco-scholl.de}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "easy_captcha.gemspec",
25
+ "init.rb",
26
+ "install.rb",
27
+ "lib/easy_captcha.rb",
28
+ "lib/easy_captcha/captcha.rb",
29
+ "lib/easy_captcha/controller.rb",
30
+ "lib/easy_captcha/controller_helpers.rb",
31
+ "lib/easy_captcha/view_helpers.rb",
32
+ "resources/captcha.ttf",
33
+ "test/easy_captcha_test.rb",
34
+ "test/test_helper.rb",
35
+ "uninstall.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/traxanos/easy_captcha}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.7}
41
+ s.summary = %q{Captcha-Plugin for Rails}
42
+ s.test_files = [
43
+ "test/easy_captcha_test.rb",
44
+ "test/test_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
53
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
54
+ s.add_runtime_dependency(%q<rmagick>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
58
+ s.add_dependency(%q<rmagick>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
+ s.add_dependency(%q<rails>, [">= 3.0.0"])
63
+ s.add_dependency(%q<rmagick>, [">= 0"])
64
+ end
65
+ end
66
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'easy_captcha'
data/install.rb ADDED
File without changes
@@ -0,0 +1,60 @@
1
+ module EasyCaptcha
2
+ # captcha generation class
3
+ class Captcha
4
+ # code for captcha generation
5
+ attr_reader :code
6
+ # blob of generated captcha image
7
+ attr_reader :image
8
+
9
+ # generate captcha by code
10
+ def initialize(code)
11
+ @code = code
12
+ generate_captcha
13
+ end
14
+
15
+ def inspect #:nodoc:
16
+ "<EasyCaptcha::Captcha @code=#{code}>"
17
+ end
18
+
19
+ private
20
+
21
+ def generate_captcha #:nodoc:
22
+ canvas = Magick::Image.new(EasyCaptcha.image_width, EasyCaptcha.image_height) do |variable|
23
+ self.background_color = EasyCaptcha.image_background_color unless EasyCaptcha.image_background_color.nil?
24
+ end
25
+
26
+ # Render the text in the image
27
+ canvas.annotate(Magick::Draw.new, 0,0,0,0, code) {
28
+ self.gravity = Magick::CenterGravity
29
+ self.font_family = EasyCaptcha.font_family
30
+ self.font_weight = Magick::LighterWeight
31
+ self.fill = EasyCaptcha.font_fill_color
32
+ if EasyCaptcha.font_stroke.to_i > 0
33
+ self.stroke = EasyCaptcha.font_stroke_color
34
+ self.stroke_width = EasyCaptcha.font_stroke
35
+ end
36
+ self.pointsize = EasyCaptcha.font_size
37
+ }
38
+
39
+ # Blur
40
+ canvas = canvas.blur_image(EasyCaptcha.blur_radius, EasyCaptcha.blur_sigma) if EasyCaptcha.blur?
41
+
42
+ # Wave
43
+ w = EasyCaptcha.wave_length
44
+ a = EasyCaptcha.wave_amplitude
45
+ canvas = canvas.wave(rand(a.last - a.first) + a.first, rand(w.last - w.first) + w.first) if EasyCaptcha.wave?
46
+
47
+ # Sketch
48
+ canvas = canvas.sketch(EasyCaptcha.sketch_radius, EasyCaptcha.sketch_sigma, rand(180)) if EasyCaptcha.sketch?
49
+
50
+ # Implode
51
+ canvas = canvas.implode(EasyCaptcha.implode.to_f) if EasyCaptcha.implode.is_a? Float
52
+
53
+ # Crop image because to big after waveing
54
+ canvas = canvas.crop(Magick::CenterGravity, EasyCaptcha.image_width, EasyCaptcha.image_height)
55
+
56
+ @image = canvas.to_blob { self.format = 'PNG' }
57
+ canvas.destroy!
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,10 @@
1
+ module EasyCaptcha
2
+ # captcha controller
3
+ # match "captcha" => EasyCaptcha::Controller
4
+ class Controller < ActionController::Base
5
+ # captcha action send the generated image to browser
6
+ def captcha
7
+ send_data generate_captcha, :disposition => 'inline', :type => 'image/png'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ module EasyCaptcha
2
+ # helper class for ActionController
3
+ module ControllerHelpers
4
+
5
+ # declare helper_methods
6
+ def self.included(base)
7
+ base.class_eval do
8
+ helper_method :valid_captcha?
9
+ end
10
+ end
11
+
12
+ # generate captcha image and return it as blob
13
+ def generate_captcha
14
+ Captcha.new(generate_captcha_code).image
15
+ end
16
+
17
+ # generate captcha code, save in session and return
18
+ def generate_captcha_code
19
+ session[:captcha] = EasyCaptcha.length.times.collect { EasyCaptcha.chars[rand(EasyCaptcha.chars.size)] }.join
20
+ end
21
+
22
+ # validate given captcha code
23
+ def valid_captcha?(code)
24
+ session[:captcha].to_s.upcase == code.to_s.upcase
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ module EasyCaptcha
2
+ # helper class for ActionView
3
+ module ViewHelpers
4
+ # generate an image_tag for captcha image
5
+ def captcha_tag(*args)
6
+ options = args.extract_options!
7
+ image_tag(captcha_path(:i => Time.now.to_i), options)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,78 @@
1
+ require 'active_support'
2
+ # Captcha-Plugin for Rails
3
+ module EasyCaptcha
4
+ autoload :Captcha, 'easy_captcha/captcha'
5
+ autoload :Controller, 'easy_captcha/controller'
6
+ autoload :ViewHelpers, 'easy_captcha/view_helpers'
7
+ autoload :ControllerHelpers, 'easy_captcha/controller_helpers'
8
+
9
+ # Chars
10
+ mattr_accessor :chars
11
+ @@chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
12
+ # Length
13
+ mattr_accessor :length
14
+ @@length = 6
15
+ # Font
16
+ mattr_accessor :font_size, :font_fill_color, :font_family, :font_stroke, :font_stroke_color
17
+ @@font_size = 24
18
+ @@font_fill_color = '#333333'
19
+ @@font_family = File.expand_path('../../../resources/captcha.ttf', __FILE__)
20
+ @@font_stroke = '#000000'
21
+ @@font_stroke_color = 0
22
+ # Image
23
+ mattr_accessor :image_width, :image_height, :image_background_color
24
+ @@image_width = 140
25
+ @@image_height = 40
26
+ @@image_background_color = '#FFFFFF'
27
+ # Sketch
28
+ mattr_accessor :sketch, :sketch_radius, :sketch_sigma
29
+ @@sketch = true
30
+ @@sketch_radius = 3
31
+ @@sketch_sigma = 1
32
+ # Wave
33
+ mattr_accessor :wave, :wave_length, :wave_amplitude
34
+ @@wave = true
35
+ @@wave_length = (60..100)
36
+ @@wave_amplitude = (3..5)
37
+ # Implode
38
+ mattr_accessor :implode
39
+ @@implode = 0.05
40
+ # Gaussian Blur
41
+ mattr_accessor :blur, :blur_radius, :blur_sigma
42
+ @@blur = true
43
+ @@blur_radius = 1
44
+ @@blur_sigma = 2
45
+
46
+ class << self
47
+ # to configure easy_captcha
48
+ # for a sample look the readme.rdoc file
49
+ def setup
50
+ yield self
51
+ end
52
+
53
+ def sketch? #:nodoc:
54
+ sketch
55
+ end
56
+
57
+ def wave? #:nodoc:
58
+ wave
59
+ end
60
+
61
+ def blur? #:nodoc:
62
+ blur
63
+ end
64
+
65
+ # called by rails after initialize
66
+ def init
67
+ ActionController::Base.send :include, ControllerHelpers
68
+ ActionView::Base.send :include, ViewHelpers
69
+ end
70
+ end
71
+ end
72
+
73
+ Rails.configuration.after_initialize do #:nodoc:
74
+ EasyCaptcha.init
75
+ end
76
+
77
+
78
+
Binary file
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class EasyCaptchaTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
data/uninstall.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_captcha
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Marco Scholl
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-04 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rails
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 3
45
+ - 0
46
+ - 0
47
+ version: 3.0.0
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rmagick
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ description: Captcha-Plugin for Rails. IMage generate based on rmagick
64
+ email: develop@marco-scholl.de
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files:
70
+ - README.rdoc
71
+ files:
72
+ - .gitignore
73
+ - MIT-LICENSE
74
+ - README.rdoc
75
+ - Rakefile
76
+ - VERSION
77
+ - easy_captcha.gemspec
78
+ - init.rb
79
+ - install.rb
80
+ - lib/easy_captcha.rb
81
+ - lib/easy_captcha/captcha.rb
82
+ - lib/easy_captcha/controller.rb
83
+ - lib/easy_captcha/controller_helpers.rb
84
+ - lib/easy_captcha/view_helpers.rb
85
+ - resources/captcha.ttf
86
+ - test/easy_captcha_test.rb
87
+ - test/test_helper.rb
88
+ - uninstall.rb
89
+ has_rdoc: true
90
+ homepage: http://github.com/traxanos/easy_captcha
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --charset=UTF-8
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.3.7
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Captcha-Plugin for Rails
121
+ test_files:
122
+ - test/easy_captcha_test.rb
123
+ - test/test_helper.rb