Class: Traptcha::Captcha

Inherits:
Object
  • Object
show all
Defined in:
lib/traptcha/captcha.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = "") ⇒ Captcha

Returns a new instance of Captcha.



5
6
7
8
9
# File 'lib/traptcha/captcha.rb', line 5

def initialize(text = "")
  @text            = text
  @image_generator = Traptcha.default_image_generator
  @encryptor       = Traptcha.default_encryptor
end

Instance Attribute Details

#encryptorObject

Returns the value of attribute encryptor.



3
4
5
# File 'lib/traptcha/captcha.rb', line 3

def encryptor
  @encryptor
end

#image_generatorObject

Returns the value of attribute image_generator.



3
4
5
# File 'lib/traptcha/captcha.rb', line 3

def image_generator
  @image_generator
end

#textObject

Returns the value of attribute text.



3
4
5
# File 'lib/traptcha/captcha.rb', line 3

def text
  @text
end

Class Method Details

.generate(options = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/traptcha/captcha.rb', line 20

def generate(options = {})
  options.reverse_merge! :valid_chars => Traptcha.valid_chars - Traptcha.ignored_chars,
                         :length => Traptcha.default_length

  new generate_random_captcha(options)
end

.generate_random_captcha(options = {}) ⇒ Object



27
28
29
# File 'lib/traptcha/captcha.rb', line 27

def generate_random_captcha(options = {})
  options[:length].times.map { options[:valid_chars][rand(options[:valid_chars].length)] }.to_s
end

Instance Method Details

#to_pngObject



15
16
17
# File 'lib/traptcha/captcha.rb', line 15

def to_png
  @image_generator.new(text).output.to_blob
end

#valueObject



11
12
13
# File 'lib/traptcha/captcha.rb', line 11

def value
  @value ||= @encryptor.digest(text)
end