rucaptcha 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 696d907bf4da18cbb818072c2c9ec9bd404e4389
4
- data.tar.gz: 0e1e1629e95bf36d0689b1fff423a67773c1e0db
3
+ metadata.gz: 726ad396a3d2d992dd37ee322ee9624c2245a33f
4
+ data.tar.gz: ddcffb0566969992ab238a32eb6cd571d641b46f
5
5
  SHA512:
6
- metadata.gz: 87a1455083f1eb353db3dfe94f9c94e7ae648346848d15237aaa241b01ba8e0dd496b6178375957657155ed98a3b7930c41528bcffd9d56806c79051d4e5c1f6
7
- data.tar.gz: ea055d9b0de33f891d17523bcc920d245e815b731aaaeb054f7310c459bbdffa357880cdf21847d691a3eb0ce73e90766dd4345f4be2d8649d763c8fcd7e82f6
6
+ metadata.gz: 87dddae022cb9c3794abe475554315ae29c6f100539fc9964a023acfdbd7b601c620482f186cd484ee7cd397f2af57dd5f18b564ab54faf162b210683c3166f7
7
+ data.tar.gz: 2ee2e5fd0acac9b5e3d546eb6f9e16ac76dad9fac6a3cd2560b2ec51a1ef594b88c13483efbc45c7c02ee6587cd3a6d3a59046931a707bc3aef88bdbd0f404a6
@@ -1,3 +1,10 @@
1
+ 0.2.0
2
+ -----
3
+
4
+ - Added file cache, can setup how many images you want generate by `config.cache_limit`,
5
+ RuCaptcha will use cache for next requests.
6
+ When you restart Rails processes it will generate new again and clean the old caches.
7
+
1
8
  0.1.4
2
9
  -----
3
10
 
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # RuCaptcha
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/rucaptcha.svg)](https://badge.fury.io/rb/rucaptcha)
4
+ [![Build Status](https://travis-ci.org/huacnlee/rucaptcha.svg)](https://travis-ci.org/huacnlee/rucaptcha)
4
5
 
5
6
  This is a Captcha gem for Rails Applications. It runs an ImageMagick command to draw Captcha image - so it has NO performance issues or memory leak issues.
6
7
 
@@ -10,17 +11,37 @@ Idea by: https://ruby-china.org/topics/20558#reply4
10
11
 
11
12
  [中文介绍和使用说明](https://ruby-china.org/topics/27832)
12
13
 
13
- ### Requirements
14
+
15
+ ## Feature
16
+
17
+ - Only need `ImageMagick`, No `RMagick`, No `mini_magick`;
18
+ - For Rails Application;
19
+ - Simple, Easy to use;
20
+ - File Caching for performance.
21
+
22
+ ## Requirements
14
23
 
15
24
  - ImageMagick
16
25
 
17
- ### Example
26
+ #### Ubuntu
27
+
28
+ ```
29
+ sudo apt-get install imagemagick
30
+ ```
31
+
32
+ #### Mac OS X
33
+
34
+ ```bash
35
+ brew install imagemagick ghostscript
36
+ ```
37
+
38
+ ## Example
18
39
 
19
40
  ![rucaptcha1](https://cloud.githubusercontent.com/assets/5518/10726119/a844dfce-7c0b-11e5-99c3-a818f3ef3dd2.png) ![rucaptcha2](https://cloud.githubusercontent.com/assets/5518/10747608/2f2f5f10-7c92-11e5-860b-914db5695a57.png) ![rucaptcha3](https://cloud.githubusercontent.com/assets/5518/10747609/2f5bbac4-7c92-11e5-8192-4aa5dfb025b7.png) ![rucaptcha4](https://cloud.githubusercontent.com/assets/5518/10747611/2f7c6a12-7c92-11e5-8730-de7295b36dd6.png) ![rucaptcha5](https://cloud.githubusercontent.com/assets/5518/10747610/2f7a9d86-7c92-11e5-911a-44596c9aeef5.png)
20
41
 
21
42
 
22
43
 
23
- ### Usage
44
+ ## Usage
24
45
 
25
46
  Put rucaptcha in your `Gemfile`:
26
47
 
@@ -38,6 +59,9 @@ RuCaptcha.configure do
38
59
  self.width = 180
39
60
  # Image height, default: 48
40
61
  self.height = 48
62
+ # Cache generated images in file store, this is config files limit, default: 100
63
+ # set 0 to disable file cache.
64
+ self.cache_limit = 100
41
65
  end
42
66
  ```
43
67
 
@@ -79,7 +103,7 @@ View `app/views/account/new.html.erb`
79
103
  </form>
80
104
  ```
81
105
 
82
- ## Test skip captcha validation
106
+ ### Write your test skip captcha validation
83
107
 
84
108
  ```rb
85
109
  describe 'sign up and login', type: :feature do
@@ -91,7 +115,5 @@ describe 'sign up and login', type: :feature do
91
115
  end
92
116
  ```
93
117
 
94
- ## TODO
95
118
 
96
- - Use [rtesseract](https://github.com/dannnylo/rtesseract) to test OCR.
97
119
 
@@ -5,6 +5,7 @@ require_relative 'rucaptcha/version'
5
5
  require_relative 'rucaptcha/configuration'
6
6
  require_relative 'rucaptcha/controller_helpers'
7
7
  require_relative 'rucaptcha/view_helpers'
8
+ require_relative 'rucaptcha/cache'
8
9
  require_relative 'rucaptcha/captcha'
9
10
  require_relative 'rucaptcha/engine'
10
11
 
@@ -13,19 +14,24 @@ module RuCaptcha
13
14
  def config
14
15
  return @config if defined?(@config)
15
16
  @config = Configuration.new
16
- @config.len = 4
17
- @config.width = 150
18
- @config.height = 48
19
- @config.implode = 0.4
17
+ @config.len = 4
18
+ @config.width = 150
19
+ @config.height = 48
20
+ @config.implode = 0.4
21
+ @config.cache_limit = 100
20
22
  @config
21
23
  end
22
24
 
23
25
  def configure(&block)
24
26
  config.instance_exec(&block)
27
+
28
+ # enable cache if cache_limit less than 1
29
+ if config.cache_limit >= 1
30
+ RuCaptcha::Captcha.send(:include, RuCaptcha::Cache)
31
+ end
25
32
  end
26
33
  end
27
34
  end
28
35
 
29
-
30
- ActionController::Base.send :include, RuCaptcha::ControllerHelpers
31
- ActionView::Base.send :include, RuCaptcha::ViewHelpers
36
+ ActionController::Base.send(:include, RuCaptcha::ControllerHelpers)
37
+ ActionView::Base.send(:include, RuCaptcha::ViewHelpers)
@@ -0,0 +1,44 @@
1
+ module RuCaptcha
2
+ # File Cache
3
+ module Cache
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class << self
8
+ alias_method_chain :create, :cache
9
+ alias_method_chain :random_chars, :cache
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def create_with_cache(code)
15
+ cache.fetch(code) do
16
+ create_without_cache(code)
17
+ end
18
+ end
19
+
20
+ def random_chars_with_cache
21
+ if cached_codes.length >= RuCaptcha.config.cache_limit
22
+ return cached_codes.sample
23
+ else
24
+ code = random_chars_without_cache
25
+ cached_codes << code
26
+ return code
27
+ end
28
+ end
29
+
30
+ def cache
31
+ return @cache if defined?(@cache)
32
+
33
+ cache_path = Rails.root.join('tmp', 'cache', 'rucaptcha')
34
+ @cache = ActiveSupport::Cache::FileStore.new(cache_path)
35
+ @cache.clear
36
+ @cache
37
+ end
38
+
39
+ def cached_codes
40
+ @cached_codes ||= []
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,44 +2,53 @@ require 'posix-spawn'
2
2
 
3
3
  module RuCaptcha
4
4
  class Captcha
5
- def self.rand_color
6
- r = rand(129).to_s(8).to_i
7
- rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
5
+ class << self
6
+ def rand_color
7
+ rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
8
8
 
9
- "rgba(#{rgb.join(',')},1)"
10
- end
11
-
12
- def self.create(code)
13
- size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}"
14
- font_size = (RuCaptcha.config.height * 0.8).to_i
15
- half_width = RuCaptcha.config.width / 2
16
- half_height = RuCaptcha.config.height / 2
17
- line_color = rand_color
9
+ "rgba(#{rgb.join(',')},1)"
10
+ end
18
11
 
19
- chars = code.split('')
20
- text_opts = []
21
- text_top = (RuCaptcha.config.height - font_size) / 2
22
- text_padding = 5
23
- text_width = (RuCaptcha.config.width / chars.size) - text_padding * 2
24
- text_left = 5
25
- chars.each_with_index do |char, i|
26
- text_opts << %(-fill '#{rand_color}' -draw 'text #{(text_left + text_width) * i + text_left},#{text_top} "#{char}"')
12
+ def random_chars
13
+ chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
14
+ chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s)
15
+ chars
27
16
  end
28
17
 
29
- command = <<-CODE
30
- convert -size #{size} #{text_opts.join(' ')} \
31
- -draw 'stroke #{line_color} line #{rand(10)},#{rand(20)} #{half_width + rand(half_width)},#{rand(half_height)}' \
32
- -draw 'stroke #{line_color} line #{rand(10)},#{rand(25)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
33
- -draw 'stroke #{line_color} line #{rand(10)},#{rand(30)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
34
- -wave #{rand(2) + 2}x#{rand(2) + 1} \
35
- -gravity NorthWest -sketch 1x10+#{rand(1)} -pointsize #{font_size} -weight 700 \
36
- -implode #{RuCaptcha.config.implode} label:- png:-
37
- CODE
38
- command.strip!
39
- # puts command
40
- pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command)
41
- Process.waitpid(pid)
42
- stdout.read
18
+ # Create Captcha image by code
19
+ def create(code)
20
+ size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}"
21
+ font_size = (RuCaptcha.config.height * 0.8).to_i
22
+ half_width = RuCaptcha.config.width / 2
23
+ half_height = RuCaptcha.config.height / 2
24
+ line_color = rand_color
25
+ chars = code.split('')
26
+ text_opts = []
27
+ text_top = (RuCaptcha.config.height - font_size) / 2
28
+ text_padding = 5
29
+ text_width = (RuCaptcha.config.width / chars.size) - text_padding * 2
30
+ text_left = 5
31
+
32
+ chars.each_with_index do |char, i|
33
+ text_opts << %(-fill '#{rand_color}' -draw 'text #{(text_left + text_width) * i + text_left},#{text_top} "#{char}"')
34
+ end
35
+
36
+ command = <<-CODE
37
+ convert -size #{size} #{text_opts.join(' ')} \
38
+ -draw 'stroke #{line_color} line #{rand(10)},#{rand(20)} #{half_width + rand(half_width)},#{rand(half_height)}' \
39
+ -draw 'stroke #{line_color} line #{rand(10)},#{rand(25)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
40
+ -draw 'stroke #{line_color} line #{rand(10)},#{rand(30)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
41
+ -wave #{rand(2) + 2}x#{rand(2) + 1} \
42
+ -gravity NorthWest -sketch 1x10+#{rand(1)} -pointsize #{font_size} -weight 700 \
43
+ -implode #{RuCaptcha.config.implode} label:- png:-
44
+ CODE
45
+
46
+ command.strip!
47
+ # puts command
48
+ pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command)
49
+ Process.waitpid(pid)
50
+ stdout.read
51
+ end
43
52
  end
44
53
  end
45
54
  end
@@ -1,5 +1,15 @@
1
1
  module RuCaptcha
2
2
  class Configuration
3
- attr_accessor :width, :height, :font_size, :len, :implode
3
+ # Image width, default 150
4
+ attr_accessor :width
5
+ # Image height, default 48
6
+ attr_accessor :height
7
+ # Number of chars, default 4
8
+ attr_accessor :len
9
+ # implode, default 0.4
10
+ attr_accessor :implode
11
+ # Number of Captcha codes limit
12
+ # set 0 to disable limit and file cache, default: 100
13
+ attr_accessor :cache_limit
4
14
  end
5
15
  end
@@ -7,14 +7,8 @@ module RuCaptcha
7
7
  end
8
8
 
9
9
  def generate_rucaptcha
10
- session[:_rucaptcha] = random_rucaptcha_chars
11
- return RuCaptcha::Captcha.create(session[:_rucaptcha])
12
- end
13
-
14
- def random_rucaptcha_chars
15
- chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
16
- chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s)
17
- chars
10
+ session[:_rucaptcha] = RuCaptcha::Captcha.random_chars
11
+ RuCaptcha::Captcha.create(session[:_rucaptcha])
18
12
  end
19
13
 
20
14
  def verify_rucaptcha?(resource = nil)
@@ -1,3 +1,3 @@
1
1
  module RuCaptcha
2
- VERSION = '0.1.4'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rucaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-28 00:00:00.000000000 Z
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: posix-spawn
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,7 @@ files:
66
80
  - config/locales/rucaptcha.zh-TW.yml
67
81
  - config/routes.rb
68
82
  - lib/rucaptcha.rb
83
+ - lib/rucaptcha/cache.rb
69
84
  - lib/rucaptcha/captcha.rb
70
85
  - lib/rucaptcha/configuration.rb
71
86
  - lib/rucaptcha/controller_helpers.rb