glebtv-simple_captcha 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1 +1,105 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ task :test_images do
4
+ require 'fileutils'
5
+ require 'active_support/all'
6
+ require File.join(File.dirname(__FILE__), "lib", "simple_captcha")
7
+
8
+ SimpleCaptcha::Utils # autoload
9
+
10
+ module SimpleCaptcha #:nodoc
11
+ module Utils #:nodoc
12
+ def self.simple_captcha_new_value(key) #:nodoc
13
+ self.random_str
14
+ end
15
+ end
16
+
17
+ class Test #:nodoc
18
+ include SimpleCaptcha::ImageHelpers
19
+
20
+ def run
21
+ generate_simple_captcha_image('test')
22
+ end
23
+ end
24
+ end
25
+
26
+ tmp_path = File.join(File.dirname(__FILE__), 'test_images')
27
+
28
+ Dir.mkdir(tmp_path, 0777) unless File.directory?(tmp_path)
29
+
30
+ tmp_file = SimpleCaptcha::Test.new.run
31
+ FileUtils.mv(tmp_file, File.join(tmp_path, '130x40_font30.png'))
32
+
33
+ SimpleCaptcha.setup do |sc|
34
+ sc.image_size = '120x60'
35
+ sc.length = 5
36
+ sc.image_color = "#31203C"
37
+ sc.image_style = 'distorted_black'
38
+ sc.distortion = 'medium'
39
+ end
40
+
41
+ tmp_file = SimpleCaptcha::Test.new.run
42
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
43
+
44
+ SimpleCaptcha.setup do |sc|
45
+ sc.image_size = '120x40'
46
+ # sc.image_color = "#31203C"
47
+ sc.image_style = 'distorted_black'
48
+ sc.distortion = 'medium'
49
+ end
50
+
51
+ tmp_file = SimpleCaptcha::Test.new.run
52
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
53
+
54
+
55
+ SimpleCaptcha.setup do |sc|
56
+ sc.pointsize = 20
57
+ sc.image_color = '#666666'
58
+ sc.image_size = '100x30'
59
+ end
60
+ tmp_file = SimpleCaptcha::Test.new.run
61
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
62
+
63
+ SimpleCaptcha.setup do |sc|
64
+ sc.pointsize = 40
65
+ sc.image_color = '#666666'
66
+ sc.image_size = '150x60'
67
+ end
68
+ tmp_file = SimpleCaptcha::Test.new.run
69
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
70
+
71
+
72
+ SimpleCaptcha.setup do |sc|
73
+ sc.pointsize = 16
74
+ sc.image_color = '#0000ff'
75
+ sc.image_size = '80x20'
76
+ end
77
+ tmp_file = SimpleCaptcha::Test.new.run
78
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
79
+
80
+ SimpleCaptcha.setup do |sc|
81
+ sc.pointsize = 14
82
+ sc.image_color = '#0000ff'
83
+ sc.image_size = '70x16'
84
+ end
85
+ tmp_file = SimpleCaptcha::Test.new.run
86
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
87
+
88
+ SimpleCaptcha.setup do |sc|
89
+ sc.pointsize = 16
90
+ sc.image_color = '#0000ff'
91
+ sc.image_size = '80x20'
92
+ end
93
+ tmp_file = SimpleCaptcha::Test.new.run
94
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
95
+
96
+
97
+ SimpleCaptcha.setup do |sc|
98
+ sc.pointsize = 40
99
+ sc.distortion = 'high'
100
+ sc.image_color = '#666666'
101
+ sc.image_size = '150x60'
102
+ end
103
+ tmp_file = SimpleCaptcha::Test.new.run
104
+ FileUtils.mv(tmp_file, File.join(tmp_path, "distorted.png"))
105
+ end
@@ -1,37 +1,40 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module SimpleCaptcha
4
- autoload :Utils, 'simple_captcha/utils'
4
+ autoload :Utils, File.dirname(__FILE__) + '/simple_captcha/utils'
5
5
 
6
- autoload :ImageHelpers, 'simple_captcha/image'
7
- autoload :ViewHelper, 'simple_captcha/view'
8
- autoload :ControllerHelpers, 'simple_captcha/controller'
6
+ autoload :ImageHelpers, File.dirname(__FILE__) + '/simple_captcha/image'
7
+ autoload :ViewHelper, File.dirname(__FILE__) + '/simple_captcha/view'
8
+ autoload :ControllerHelpers, File.dirname(__FILE__) + '/simple_captcha/controller'
9
9
 
10
- autoload :FormBuilder, 'simple_captcha/hooks/form_builder'
10
+ autoload :FormBuilder, File.dirname(__FILE__) + '/simple_captcha/hooks/form_builder'
11
11
 
12
- if Object.const_defined?("Formtastic")
13
- require "simple_captcha/hooks/formtastic"
12
+ if Object.const_defined?('Formtastic')
13
+ require File.dirname(__FILE__) + '/simple_captcha/hooks/formtastic'
14
14
  end
15
15
 
16
- if Object.const_defined?("SimpleForm")
17
- require "simple_captcha/hooks/simple_form"
16
+ if Object.const_defined?('SimpleForm')
17
+ require File.dirname(__FILE__) + '/simple_captcha/hooks/simple_form'
18
18
  end
19
19
 
20
- if Object.const_defined?("Mongoid")
21
- autoload :SimpleCaptchaData, 'simple_captcha/storage/mongoid'
20
+ if Object.const_defined?('Mongoid')
21
+ autoload :SimpleCaptchaData, File.dirname(__FILE__) + '/simple_captcha/storage/mongoid'
22
22
  else
23
- autoload :SimpleCaptchaData, 'simple_captcha/storage/active_record'
24
- autoload :ModelHelpers, 'simple_captcha/active_record'
23
+ autoload :SimpleCaptchaData, File.dirname(__FILE__) + '/simple_captcha/storage/active_record'
24
+ autoload :ModelHelpers, File.dirname(__FILE__) + '/simple_captcha/active_record'
25
25
  end
26
26
 
27
- autoload :Middleware, 'simple_captcha/middleware'
27
+ autoload :Middleware, File.dirname(__FILE__) + '/simple_captcha/middleware'
28
28
 
29
29
  mattr_accessor :image_size
30
- @@image_size = "100x28"
30
+ @@image_size = "130x40"
31
31
 
32
32
  mattr_accessor :length
33
33
  @@length = 5
34
34
 
35
+ mattr_accessor :charset
36
+ @@charset = '23456789abcdefghkmnpqrstwxyz'
37
+
35
38
  # 'embosed_silver',
36
39
  # 'simply_red',
37
40
  # 'simply_green',
@@ -72,14 +72,16 @@ module SimpleCaptcha #:nodoc
72
72
 
73
73
  params << "-size #{SimpleCaptcha.image_size} xc:transparent"
74
74
  params << "-gravity \"Center\""
75
+ psz = SimpleCaptcha.pointsize
75
76
  if params.join(' ').index('-pointsize').nil?
76
- params << "-pointsize #{SimpleCaptcha.pointsize}"
77
+ params << "-pointsize #{psz}"
77
78
  end
79
+
78
80
  dst = Tempfile.new(RUBY_VERSION < '1.9' ? 'simple_captcha.png' : ['simple_captcha', '.png'], SimpleCaptcha.tmp_path)
79
81
  dst.binmode
80
82
  text.split(//).each_with_index do |letter, index|
81
- i = -60 + (index*25) + rand(-6..6)
82
- params << "-draw \"translate #{i},#{rand(-6..6)} skewX #{rand(-15..15)} gravity center text 0,0 '#{letter}'\" "
83
+ i = -(1.5 * psz) + (index * 0.75 * psz) + rand(-3..3)
84
+ params << "-draw \"translate #{i},#{rand(-3..3)} skewX #{rand(-15..15)} gravity center text 0,0 '#{letter}'\" "
83
85
  end
84
86
 
85
87
  params << "-wave #{amplitude}x#{frequency}"
@@ -24,17 +24,19 @@ module SimpleCaptcha #:nodoc
24
24
  SimpleCaptchaData.get_data(key).value rescue nil
25
25
  end
26
26
 
27
+ def self.random_str()
28
+ charset = SimpleCaptcha.charset.split(//)
29
+ size = SimpleCaptcha.length
30
+
31
+ (0...size).map{ charset.to_a[rand(charset.size)] }.join
32
+ end
33
+
27
34
  def self.simple_captcha_new_value(key) #:nodoc
28
35
  begin
29
- # very unsafe to display same code over and over
30
- value = ''
31
- # SimpleCaptcha.length.times{value << (48 + rand(10)).chr}
32
-
33
- SimpleCaptcha.length.times{value << (65 + rand(26)).chr}
34
36
  d = SimpleCaptchaData.get_data(key)
35
- d.value = value
37
+ d.value = self.random_str
36
38
  d.save!
37
- value
39
+ d.value
38
40
  rescue
39
41
  nil
40
42
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module SimpleCaptcha
3
- VERSION = "0.3.1".freeze
3
+ VERSION = "0.4.1".freeze
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glebtv-simple_captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-12 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-03-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
14
30
  description: .
15
31
  email: i@gleb.tv
16
32
  executables: []
@@ -59,10 +75,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
75
  version: '0'
60
76
  requirements: []
61
77
  rubyforge_project:
62
- rubygems_version: 1.8.22
78
+ rubygems_version: 1.8.25
63
79
  signing_key:
64
80
  specification_version: 3
65
81
  summary: A fork of a fork of a fork of simple_captcha.
66
82
  test_files:
67
83
  - test/simple_captcha_test.rb
68
- has_rdoc: