Class: ValidatesCaptcha::StringGenerator::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_captcha/string_generator/simple.rb

Overview

This class is responsible for generating the codes that are displayed on the captcha images. It does so by randomly selecting a number of characters from a predefined alphabet constisting of visually distinguishable letters and digits.

The number of characters and the alphabet used when generating strings can be customized. See the #alphabet= and #length= methods for details.

You can implement your own string generator by creating a class that conforms to the method definitions of the example below and assign an instance of it to ValidatesCaptcha::Provider::DynamicImage#string_generator=.

Example for a custom string generator:

class DictionaryGenerator
  DICTIONARY = ['foo', 'bar', 'baz', ...]

  def generate
    return DICTIONARY[rand(DICTIONARY.size)]
  end
end

ValidatesCaptcha::Provider::DynamicImage.string_generator = DictionaryGenerator.new
ValidatesCaptcha.provider = ValidatesCaptcha::Provider::DynamicImage.new

You can also assign it to ValidatesCaptcha::Provider::StaticImage#string_generator=.

Constant Summary collapse

@@alphabet =
'abdefghjkmnqrtABDEFGHJKLMNQRT234678923467892346789'
@@length =
6

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.alphabetObject

Returns a string holding the chars used when randomly generating the text that is displayed on a captcha image. Defaults to a string of visually distinguishable letters and digits.



39
40
41
# File 'lib/validates_captcha/string_generator/simple.rb', line 39

def alphabet
  @@alphabet
end

.alphabet=(alphabet) ⇒ Object

Sets the string to use as alphabet when randomly generating the text displayed on a captcha image. To increase the probability of appearing in the image, some characters might appear more than once in the string.

You can set this to a custom alphabet within a Rails initializer:

ValidatesCaptcha::StringGenerator::Simple.alphabet = '01'


50
51
52
53
54
55
# File 'lib/validates_captcha/string_generator/simple.rb', line 50

def alphabet=(alphabet)
  alphabet = alphabet.to_s.gsub(/\s/, '')
  raise('alphabet cannot be blank') if alphabet.blank?

  @@alphabet = alphabet
end

.lengthObject

Returns the length to use when generating captcha codes. Defaults to 6.



58
59
60
# File 'lib/validates_captcha/string_generator/simple.rb', line 58

def length
  @@length
end

.length=(length) ⇒ Object

Sets the length to use when generating captcha codes.

You can set this to a custom length within a Rails initializer:

ValidatesCaptcha::StringGenerator::Simple.length = 8


67
68
69
# File 'lib/validates_captcha/string_generator/simple.rb', line 67

def length=(length)
  @@length = length.to_i
end

Instance Method Details

#generateObject

Randomly generates a string to be used as the code displayed on captcha images.



73
74
75
76
77
78
# File 'lib/validates_captcha/string_generator/simple.rb', line 73

def generate
  alphabet_chars = self.class.alphabet.split(//)
  code_chars = []
  self.class.length.times { code_chars << alphabet_chars[rand(alphabet_chars.size)] }
  code_chars.join
end