Sha256: 6427fcfc30beaaa066f054797162d0050b2b2b56a5c8e0f0e568be01df1fe9bb

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module DalliCaptcha
  class KeyHandler
    def initialize(captcha_key)
      @dalli = DalliCaptcha.dalli
      @captcha_key = captcha_key
      @options = DalliCaptcha.options

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

    #set string in memcache
    def set(string)
      string = @options[:case_sensitive] ? string : string.downcase
      @dalli.set(@string_key,string,@options[:expired_time])

      #locked_times = @dalli.get(@locked_times_key).to_i
      #@dalli.incrby(@locked_times_key, 1)
      #@dalli.expire(@locked_times_key, @options[:locked_time]) if locked_times == 0
    end

    def delete
      @dalli.delete(@string_key)
      #@dalli.del(@locked_times_key)
    end

    def locked?
      locked_times = @dalli.get(@locked_times_key).to_i
      @captcha_key.blank? || (locked_times >= @options[:locked_times])
    end

    def valid?(captcha)
      string = @dalli.get(@string_key)
      if captcha.blank? || string.blank?
        return false
      else
        string == (@options[:case_sensitive] ? captcha : captcha.downcase)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dalli_captcha-0.0.2 lib/dalli_captcha/key_handler.rb
dalli_captcha-0.0.1 lib/dalli_captcha/key_handler.rb