traptcha 0.0.1 → 0.0.2

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/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  pkg/*
4
4
  vendor/*
5
5
  tmp/*
6
+ Gemfile.lock
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in traptcha.gemspec
4
4
  gemspec
5
+
6
+ gem "rails", "~> 3.0.0"
@@ -0,0 +1,12 @@
1
+ class Traptcha::CaptchasController < ApplicationController
2
+ def show
3
+ session[:captcha] = generated_captcha.value
4
+ send_data generated_captcha.to_png
5
+ end
6
+
7
+ protected
8
+
9
+ def generated_captcha
10
+ @generated_captcha ||= Traptcha::Captcha.generate
11
+ end
12
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ resource :captcha, :only => :show, :module => :traptcha
3
+ end
@@ -0,0 +1,33 @@
1
+ module Traptcha
2
+ class Captcha
3
+ attr_accessor :text, :image_generator, :encryptor
4
+
5
+ def initialize(text = "")
6
+ @text = text
7
+ @image_generator = Traptcha.default_image_generator
8
+ @encryptor = Traptcha.default_encryptor
9
+ end
10
+
11
+ def value
12
+ @value ||= @encryptor.digest(text)
13
+ end
14
+
15
+ def to_png
16
+ @image_generator.new(text).output.to_blob
17
+ end
18
+
19
+ class << self
20
+ def generate
21
+ new generate_random_captcha
22
+ end
23
+
24
+ def generate_random_captcha
25
+ 3.times.map { valid_chars[rand(valid_chars.length)] }.to_s
26
+ end
27
+
28
+ def valid_chars
29
+ Traptcha.valid_chars - Traptcha.ignored_chars
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ module Traptcha
2
+ module ControllerHelpers
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :valid_captcha?
7
+ end
8
+
9
+ def valid_captcha?
10
+ params[:captcha] && session[:captcha] && Traptcha::Captcha.new(params[:captcha]).value == session[:captcha]
11
+ end
12
+
13
+ def validate_captcha
14
+ raise Traptcha::InvalidCaptcha unless valid_captcha?
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'digest/md5'
2
+
3
+ module Traptcha
4
+ module Encryptors
5
+ class MD5
6
+ def self.digest(text)
7
+ Digest::MD5.hexdigest(text)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require "rails"
2
+
3
+ module Traptcha
4
+ class Engine < Rails::Engine
5
+ initializer "traptcha" do |app|
6
+ ActiveSupport.on_load(:action_controller) do
7
+ ActionController::Base.send :include, Traptcha::ControllerHelpers
8
+ end
9
+
10
+ ActiveSupport.on_load(:action_view) do
11
+ ActionView::Base.send :include, Traptcha::ViewHelpers
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,7 @@
1
1
  module Traptcha
2
2
  class Image
3
+ delegate :to_png, :to => :output
4
+
3
5
  def initialize(text, options = {})
4
6
  @text = text
5
7
  @canvas = Magick::Image.new(150, 100)
@@ -0,0 +1,4 @@
1
+ module Traptcha
2
+ class InvalidCaptcha < StandardError
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module Traptcha
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,27 @@
1
+ module Traptcha
2
+ module ViewHelpers
3
+ def captcha_image_tag
4
+ image_tag captcha_path
5
+ end
6
+
7
+ def captcha_label_tag
8
+ label_tag :captcha
9
+ end
10
+
11
+ def captcha_input_tag
12
+ text_field_tag :captcha
13
+ end
14
+
15
+ def captcha_tag
16
+ if block_given?
17
+ yield captcha_image_tag, captcha_label_tag, captcha_input_tag
18
+ else
19
+ content_tag :ul do
20
+ concat content_tag(:li, captcha_label_tag)
21
+ concat content_tag(:li, captcha_image_tag)
22
+ concat content_tag(:li, captcha_input_tag)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/traptcha.rb CHANGED
@@ -1,25 +1,31 @@
1
+ require 'traptcha/engine'
2
+
1
3
  module Traptcha
2
- autoload :Image, 'traptcha/image'
3
- autoload :WaveImage, 'traptcha/wave_image'
4
-
5
- class << self
6
-
7
- def default_wave_variation
8
- @@default_wave_variation ||= (20..90)
9
- end
10
-
11
- def default_wave_variation=(variation)
12
- @@default_wave_variation = variation
13
- end
14
-
15
- def default_wave_amplitude
16
- @@default_wave_amplitude ||= 5
17
- end
18
-
19
- def default_wave_amplitude=(amplitude)
20
- @@default_wave_amplitude = amplitude
21
- end
22
- end
4
+ autoload :Image, 'traptcha/image'
5
+ autoload :WaveImage, 'traptcha/wave_image'
6
+ autoload :Encryptors, 'traptcha/encryptors'
7
+ autoload :Captcha, 'traptcha/captcha'
8
+ autoload :ControllerHelpers, 'traptcha/controller_helpers'
9
+ autoload :ViewHelpers, 'traptcha/view_helpers'
10
+ autoload :InvalidCaptcha, 'traptcha/invalid_captcha'
11
+
12
+ mattr_accessor :valid_chars
13
+ @@valid_chars = ('a'..'z').to_a + (0..9).map(&:to_s).to_a
14
+
15
+ mattr_accessor :ignored_chars
16
+ @@ignored_chars = %w(y k u v m n l i o 0 1 9 q e)
17
+
18
+ mattr_accessor :default_image_generator
19
+ @@default_image_generator = WaveImage
20
+
21
+ mattr_accessor :default_encryptor
22
+ @@default_encryptor = Encryptors::MD5
23
+
24
+ mattr_accessor :default_wave_variation
25
+ @@default_wave_variation = (20..90)
26
+
27
+ mattr_accessor :default_wave_amplitude
28
+ @@default_wave_amplitude = 5
23
29
 
24
30
  def self.setup
25
31
  yield self
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traptcha
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rodrigo Navarro
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-14 00:00:00 -03:00
18
+ date: 2011-03-15 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -60,11 +60,18 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - .gitignore
62
62
  - Gemfile
63
- - Gemfile.lock
64
63
  - Rakefile
64
+ - app/controllers/traptcha/captchas_controller.rb
65
+ - config/routes.rb
65
66
  - lib/traptcha.rb
67
+ - lib/traptcha/captcha.rb
68
+ - lib/traptcha/controller_helpers.rb
69
+ - lib/traptcha/encryptors.rb
70
+ - lib/traptcha/engine.rb
66
71
  - lib/traptcha/image.rb
72
+ - lib/traptcha/invalid_captcha.rb
67
73
  - lib/traptcha/version.rb
74
+ - lib/traptcha/view_helpers.rb
68
75
  - lib/traptcha/wave_image.rb
69
76
  - spec/spec_helper.rb
70
77
  - spec/traptcha/image_spec.rb
data/Gemfile.lock DELETED
@@ -1,26 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- traptcha (0.0.1)
5
- rmagick
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- diff-lcs (1.1.2)
11
- rmagick (2.13.1)
12
- rspec (2.5.0)
13
- rspec-core (~> 2.5.0)
14
- rspec-expectations (~> 2.5.0)
15
- rspec-mocks (~> 2.5.0)
16
- rspec-core (2.5.1)
17
- rspec-expectations (2.5.0)
18
- diff-lcs (~> 1.1.2)
19
- rspec-mocks (2.5.0)
20
-
21
- PLATFORMS
22
- ruby
23
-
24
- DEPENDENCIES
25
- rspec (~> 2.5.0)
26
- traptcha!