Class: Revent::CaptchaFactory

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/revent/captcha.rb

Constant Summary collapse

JIGGLE =
15
WOBBLE =
20

Instance Method Summary collapse

Constructor Details

#initialize(key, length, valid_period) ⇒ CaptchaFactory

valid_period: [sec]



22
23
24
25
26
# File 'lib/revent/captcha.rb', line 22

def initialize(key, length, valid_period)
  @b = Crypt::Blowfish.new(key)
  @length = length
  @valid_period = valid_period
end

Instance Method Details

#correct?(code, encrypted_code_with_timestamp) ⇒ Boolean

Returns true if the code matches and within the valid period.

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/revent/captcha.rb', line 37

def correct?(code, encrypted_code_with_timestamp)
  code_with_timestamp = @b.decrypt_string(encrypted_code_with_timestamp)
  code0 = code_with_timestamp.slice!(0, @length)
  timestamp0 = code_with_timestamp.to_i
  code.upcase == code0 and Time.now.to_i - timestamp0 < @valid_period
end

#newObject

Returns [encrypted_code_with_timestamp, img].



29
30
31
32
33
34
# File 'lib/revent/captcha.rb', line 29

def new
  code, img = real_new
  code_with_timestamp = code + Time.now.to_i.to_s
  encrypted_code_with_timestamp = @b.encrypt_string(code_with_timestamp)
  Captcha.new(encrypted_code_with_timestamp, img)
end