Module: RaCaptcha
- Defined in:
- lib/racaptcha.rb,
lib/racaptcha/cache.rb,
lib/racaptcha/version.rb,
lib/racaptcha/configuration.rb,
lib/racaptcha/errors/configuration.rb,
ext/racaptcha/racaptcha.c
Defined Under Namespace
Modules: Errors Classes: Configuration
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
- .cache ⇒ Object
- .config ⇒ Object
- .configure(&block) ⇒ Object
- .create ⇒ Object
-
.generate ⇒ Object
generate new captcha without cache.
-
.generate_captcha(cache_key = nil) ⇒ Object
generate new captcha with cache.
-
.verify_racaptcha?(options = {}) ⇒ Boolean
verify captcha.
Class Method Details
.cache ⇒ Object
5 6 7 8 9 10 |
# File 'lib/racaptcha/cache.rb', line 5 def cache return @cache if defined? @cache @cache = ActiveSupport::Cache.lookup_store(RaCaptcha.config.cache_store) @cache end |
.config ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/racaptcha.rb', line 10 def config return @config if defined?(@config) @config = Configuration.new @config.style = :colorful @config.length = 4 @config.strikethrough = true @config.outline = false @config.expires_in = 2.minutes @config.skip_cache_store_check = false @config end |
.configure(&block) ⇒ Object
23 24 25 |
# File 'lib/racaptcha.rb', line 23 def configure(&block) config.instance_exec(&block) end |
.create ⇒ Object
275 |
# File 'ext/racaptcha/racaptcha.c', line 275
VALUE create(VALUE self, VALUE style, VALUE length, VALUE line, VALUE filter);
|
.generate ⇒ Object
generate new captcha without cache
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/racaptcha.rb', line 28 def generate style = config.style == :colorful ? 1 : 0 length = config.length unless length.in?(3..7) raise RaCaptcha::Errors::Configuration, 'length config error, value must in 3..7' end strikethrough = config.strikethrough ? 1 : 0 outline = config.outline ? 1 : 0 create(style, length, strikethrough, outline) end |
.generate_captcha(cache_key = nil) ⇒ Object
generate new captcha with cache
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/racaptcha.rb', line 42 def generate_captcha(cache_key = nil) res = generate _val = { code: res[0], time: Time.now.to_i } RaCaptcha.cache.write(generate_cache_key(cache_key), _val, expires_in: config.expires_in) # 验证码图片,验证码,缓存key [res[1], res[0], cache_key] end |
.verify_racaptcha?(options = {}) ⇒ Boolean
verify captcha
exmaple:
verify_racaptcha?(cache_key: 'xxx', captcha: 'xxx')
verify_racaptcha?(cache_key: 'xxx', captcha: 'xxx', keep_cache: true)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/racaptcha.rb', line 60 def verify_racaptcha?( = {}) ||= {} _key = generate_cache_key([:cache_key]) store_info = RaCaptcha.cache.read(_key) # Make sure move used key RaCaptcha.cache.delete(_key) unless [:keep_cache] # Make sure session exist return [false, "未找到存储的验证码"] if store_info.blank? # Make sure not expire return [false, "验证码已过期"] if (Time.now.to_i - store_info[:time]) > config.expires_in # Make sure have captcha captcha = [:captcha].to_s.downcase.strip return [false, "请传递用户输入的验证码"] if captcha.blank? return [false, "验证码无效"] if captcha != store_info[:code] return [true, "验证码正确"] end |