Class: RedisCaptcha::KeyHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_captcha/key_handler.rb

Instance Method Summary collapse

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.options

  @string_key = "#{@options[:redis_scope]}:#{@captcha_key}:string"
  @locked_times_key = "#{@options[:redis_scope]}:#{@captcha_key}:locked_times"
end

Instance Method Details

#deleteObject



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

Returns:

  • (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

Returns:

  • (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