racaptcha 0.2.2 → 0.3.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
  SHA256:
3
- metadata.gz: bd350dc677de5fe1ce13a3eba19cd6fc13c30a9c1b44bc637159052048ad7546
4
- data.tar.gz: 9d96dfa982a4ad938527503b7d016d0676dca65b549130a49386f14e680590ec
3
+ metadata.gz: 44a2c6f49f4b55bcb3c88f3fc6f0e6693f61fee8d392a6cedb921524641bd5e9
4
+ data.tar.gz: 5620531b8a5e2924a87a751f6a282360f410342ca3d1f87dc7021a4ba3c83571
5
5
  SHA512:
6
- metadata.gz: eb0b70aa4e1eba0163423978537baa80db9b454240181b12b6d281a5049b7d5cb5e96bb526c6433784e79ce4981c259f307d02ce8a1de4260d6e50db47cdd13c
7
- data.tar.gz: de457a2212c8ea03e03cf16a571be3746dbe7b57c2d09915135c84ee3d244fa34a262d6d11fb313bbbe44336cbd9ebdf77cd19c1459416440502e133ad691d04
6
+ metadata.gz: 31273910d07ab1a8b9e54520072df00862e10a9707e7b0af0ea8b6a4c369e16ef7bca5f3f7fdaf590b838573cbf5d8d628d5b7f711263158a4c6df6606203599
7
+ data.tar.gz: e3ff4cc023e22b70aef4bf0e79f870b983f66e8729701db0cbca595368d92da5004c647a448bfbd6d297e035c0e349dd4af856c4671b3dc29d41de1d779e6fbe
@@ -0,0 +1,12 @@
1
+ require "fileutils"
2
+
3
+ module RaCaptcha
4
+ class << self
5
+ def cache
6
+ return @cache if defined? @cache
7
+
8
+ @cache = ActiveSupport::Cache.lookup_store(RaCaptcha.config.cache_store)
9
+ @cache
10
+ end
11
+ end
12
+ end
@@ -15,25 +15,5 @@ module RaCaptcha
15
15
  attr_accessor :outline
16
16
  # skip_cache_store_check, default: false
17
17
  attr_accessor :skip_cache_store_check
18
-
19
- def initialize
20
- @cache_store = :file_store
21
- @expires_in = 2.minutes
22
- @style = :colorful
23
- @length = 5
24
- @strikethrough = true
25
- @outline = false
26
- @skip_cache_store_check = false
27
- end
28
- end
29
-
30
- class << self
31
- def setup
32
- yield config
33
- end
34
-
35
- def config
36
- @config ||= Configuration.new
37
- end
38
18
  end
39
19
  end
@@ -1,3 +1,3 @@
1
1
  module RaCaptcha
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/racaptcha.rb CHANGED
@@ -1,17 +1,27 @@
1
1
  require "active_support/all"
2
- require "racaptcha/configuration"
3
2
  require "racaptcha/version"
3
+ require "racaptcha/configuration"
4
+ require "racaptcha/cache"
4
5
  require "racaptcha/errors/configuration"
5
6
  require "racaptcha/racaptcha"
6
7
 
7
8
  module RaCaptcha
8
9
  class << self
10
+ def config
11
+ return @config if defined?(@config)
12
+
13
+ @config = Configuration.new
14
+ @config.style = :colorful
15
+ @config.length = 4
16
+ @config.strikethrough = true
17
+ @config.outline = false
18
+ @config.expires_in = 2.minutes
19
+ @config.skip_cache_store_check = false
20
+ @config
21
+ end
9
22
 
10
- def cache
11
- return @cache if defined? @cache
12
-
13
- @cache = ActiveSupport::Cache.lookup_store(config.cache_store)
14
- @cache
23
+ def configure(&block)
24
+ config.instance_exec(&block)
15
25
  end
16
26
 
17
27
  # generate new captcha without cache
@@ -35,8 +45,9 @@ module RaCaptcha
35
45
  code: res[0],
36
46
  time: Time.now.to_i
37
47
  }
38
- cache.write(generate_cache_key(cache_key), _val, expires_in: config.expires_in)
39
- res[1]
48
+ RaCaptcha.cache.write(generate_cache_key(cache_key), _val, expires_in: config.expires_in)
49
+ # 验证码图片,验证码,缓存key
50
+ [res[1], res[0], cache_key]
40
51
  end
41
52
 
42
53
  # verify captcha
@@ -50,23 +61,23 @@ module RaCaptcha
50
61
  options ||= {}
51
62
 
52
63
  _key = generate_cache_key(options[:cache_key])
53
- store_info = cache.read(_key)
64
+ store_info = RaCaptcha.cache.read(_key)
54
65
  # Make sure move used key
55
- cache.delete(_key) unless options[:keep_cache]
66
+ RaCaptcha.cache.delete(_key) unless options[:keep_cache]
56
67
 
57
68
  # Make sure session exist
58
- return false if store_info.blank?
69
+ return [false, "未找到存储的验证码"] if store_info.blank?
59
70
 
60
71
  # Make sure not expire
61
- return false if (Time.now.to_i - store_info[:time]) > config.expires_in
72
+ return [false, "验证码已过期"] if (Time.now.to_i - store_info[:time]) > config.expires_in
62
73
 
63
74
  # Make sure have captcha
64
- captcha = (options[:captcha] || "").downcase.strip
65
- return false if captcha.blank?
75
+ captcha = options[:captcha].to_s.downcase.strip
76
+ return [false, "请传递用户输入的验证码"] if captcha.blank?
66
77
 
67
- return false if captcha != store_info[:code]
78
+ return [false, "验证码无效"] if captcha != store_info[:code]
68
79
 
69
- true
80
+ return [true, "验证码正确"]
70
81
  end
71
82
 
72
83
  private
data/racaptcha.gemspec CHANGED
@@ -38,6 +38,5 @@ Gem::Specification.new do |spec|
38
38
  spec.required_ruby_version = ">= 2.0.0"
39
39
  spec.extensions = %w[ext/racaptcha/extconf.rb]
40
40
 
41
- spec.add_runtime_dependency 'activesupport', '>= 5.2.4.3'
42
41
  spec.add_development_dependency 'rake-compiler', '~> 1'
43
42
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: racaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - wangrui
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-26 00:00:00.000000000 Z
11
+ date: 2022-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activesupport
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 5.2.4.3
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: 5.2.4.3
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake-compiler
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -62,6 +48,7 @@ files:
62
48
  - ext/racaptcha/font.h
63
49
  - ext/racaptcha/racaptcha.c
64
50
  - lib/racaptcha.rb
51
+ - lib/racaptcha/cache.rb
65
52
  - lib/racaptcha/configuration.rb
66
53
  - lib/racaptcha/errors/configuration.rb
67
54
  - lib/racaptcha/version.rb