Class: RedisCaptcha::KeyHandler
- Inherits:
-
Object
- Object
- RedisCaptcha::KeyHandler
- Defined in:
- lib/redis_captcha/key_handler.rb
Instance Method Summary collapse
- #delete ⇒ Object
-
#initialize(captcha_key) ⇒ KeyHandler
constructor
A new instance of KeyHandler.
- #locked? ⇒ Boolean
- #set(string) ⇒ Object
- #valid?(captcha) ⇒ Boolean
Constructor Details
#initialize(captcha_key) ⇒ KeyHandler
Returns a new instance of KeyHandler.
3 4 5 6 7 8 9 10 |
# File 'lib/redis_captcha/key_handler.rb', line 3 def initialize(captcha_key) @redis = RedisCaptcha.redis @captcha_key = captcha_key @options = RedisCaptcha. @string_key = "#{@options[:redis_scope]}:#{@captcha_key}:string" @locked_times_key = "#{@options[:redis_scope]}:#{@captcha_key}:locked_times" end |
Instance Method Details
#delete ⇒ Object
23 24 25 26 |
# File 'lib/redis_captcha/key_handler.rb', line 23 def delete @redis.del(@string_key) #@redis.del(@locked_times_key) end |
#locked? ⇒ Boolean
28 29 30 31 |
# File 'lib/redis_captcha/key_handler.rb', line 28 def locked? locked_times = @redis.get(@locked_times_key).to_i @captcha_key.blank? || (locked_times >= @options[:locked_times]) end |
#set(string) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/redis_captcha/key_handler.rb', line 12 def set(string) string = @options[:case_sensitive] ? string : string.downcase @redis.set(@string_key, string) @redis.expire(@string_key, @options[:expired_time]) locked_times = @redis.get(@locked_times_key).to_i @redis.incrby(@locked_times_key, 1) @redis.expire(@locked_times_key, @options[:locked_time]) if locked_times == 0 end |
#valid?(captcha) ⇒ Boolean
33 34 35 36 37 38 39 40 |
# File 'lib/redis_captcha/key_handler.rb', line 33 def valid?(captcha) string = @redis.get(@string_key) if captcha.blank? || string.blank? return false else string == (@options[:case_sensitive] ? captcha : captcha.downcase) end end |