traptcha 0.0.1

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 ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
4
+ vendor/*
5
+ tmp/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in traptcha.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
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!
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,43 @@
1
+ module Traptcha
2
+ class Image
3
+ def initialize(text, options = {})
4
+ @text = text
5
+ @canvas = Magick::Image.new(150, 100)
6
+ @canvas.format = "PNG"
7
+ @options = options
8
+ end
9
+
10
+ def save(path)
11
+ output.write(File.join(path, @text + ".png"))
12
+ end
13
+
14
+ def output
15
+ generate
16
+ @canvas
17
+ end
18
+
19
+ protected
20
+
21
+ def generate
22
+ write_letters
23
+ end
24
+
25
+ def write_letters
26
+ image = ::Magick::Draw.new
27
+
28
+ image.font_family = "Thonburi"
29
+ image.font_weight = ::Magick::BoldWeight
30
+
31
+ position = 0
32
+
33
+ @text.chars.each do |letter|
34
+ image.annotate(@canvas, 0, 0, position, 90, letter) do
35
+ self.fill = "black"
36
+ self.pointsize = (40..80).to_a.choice
37
+ end
38
+
39
+ position += (40..55).to_a.choice
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Traptcha
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ module Traptcha
2
+ class WaveImage < Image
3
+ attr_accessor :variation, :amplitude
4
+
5
+ def variation
6
+ @variation ||= @options[:variation] ||= Traptcha.default_wave_variation
7
+ end
8
+
9
+ def amplitude
10
+ @amplitude ||= @options[:amplitude] || Traptcha.default_wave_amplitude
11
+ end
12
+
13
+ protected
14
+
15
+ def generate
16
+ super
17
+ wavenize
18
+ end
19
+
20
+ def wavenize
21
+ @canvas = @canvas.wave(amplitude, random_variation)
22
+ end
23
+
24
+ private
25
+
26
+ def random_variation
27
+ rand(variation.last - variation.first) + variation.first
28
+ end
29
+ end
30
+ end
data/lib/traptcha.rb ADDED
@@ -0,0 +1,27 @@
1
+ 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
23
+
24
+ def self.setup
25
+ yield self
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ require 'traptcha'
2
+
3
+ shared_examples_for "a image generator" do
4
+ subject { described_class.new("test") }
5
+ let(:path) { File.expand_path(File.dirname(__FILE__) + "/tmp") }
6
+
7
+ before do
8
+ FileUtils.mkdir_p path
9
+ end
10
+
11
+ it "saves a image file on an specific path" do
12
+ expect { subject.save(path) }.to change { Dir["#{path}/*"].size }
13
+ end
14
+
15
+ after do
16
+ FileUtils.rm_rf path
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+ require "rmagick"
3
+ require "fileutils"
4
+
5
+ describe Traptcha::Image do
6
+ it_behaves_like "a image generator"
7
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+ require "rmagick"
3
+ require "fileutils"
4
+
5
+ describe Traptcha::WaveImage do
6
+ it_behaves_like "a image generator"
7
+
8
+ describe "#amplitude" do
9
+ let(:amplitude) { 4 }
10
+
11
+ it "can be informed as a option on the initialization process" do
12
+ Traptcha::WaveImage.new("test", :amplitude => amplitude).amplitude.should == amplitude
13
+ end
14
+ end
15
+
16
+ describe "#wave_variation" do
17
+ let(:variation) { 10..20 }
18
+
19
+ it "can be informed as a option on the initialization process" do
20
+ Traptcha::WaveImage.new("test", :variation => variation).variation.should == variation
21
+ end
22
+ end
23
+ end
data/traptcha.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "traptcha/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "traptcha"
7
+ s.version = Traptcha::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Rodrigo Navarro"]
10
+ s.email = ["reu@rnavarro.com.br"]
11
+ s.homepage = "http://github.com/reu/traptcha"
12
+ s.summary = "Simple captcha for rails"
13
+ s.description = "Captcha generator for rails"
14
+
15
+ s.rubyforge_project = "traptcha"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "rmagick"
23
+
24
+ s.add_development_dependency "rspec", "~> 2.5.0"
25
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: traptcha
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Rodrigo Navarro
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-14 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rmagick
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 27
44
+ segments:
45
+ - 2
46
+ - 5
47
+ - 0
48
+ version: 2.5.0
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: Captcha generator for rails
52
+ email:
53
+ - reu@rnavarro.com.br
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - .gitignore
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - Rakefile
65
+ - lib/traptcha.rb
66
+ - lib/traptcha/image.rb
67
+ - lib/traptcha/version.rb
68
+ - lib/traptcha/wave_image.rb
69
+ - spec/spec_helper.rb
70
+ - spec/traptcha/image_spec.rb
71
+ - spec/traptcha/wave_image_spec.rb
72
+ - traptcha.gemspec
73
+ has_rdoc: true
74
+ homepage: http://github.com/reu/traptcha
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project: traptcha
103
+ rubygems_version: 1.5.0
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Simple captcha for rails
107
+ test_files:
108
+ - spec/spec_helper.rb
109
+ - spec/traptcha/image_spec.rb
110
+ - spec/traptcha/wave_image_spec.rb