Class: Kamcaptcha::Generator::DictionaryWordGenerator

Inherits:
WordGenerator
  • Object
show all
Defined in:
lib/kamcaptcha/generator.rb

Class Attribute Summary collapse

Attributes inherited from WordGenerator

#max, #min

Instance Method Summary collapse

Methods inherited from WordGenerator

#initialize

Constructor Details

This class inherits a constructor from Kamcaptcha::Generator::WordGenerator

Class Attribute Details

.dictObject

Use this to set a custom dictionary or dictionary generating function



38
39
40
# File 'lib/kamcaptcha/generator.rb', line 38

def dict
  @dict
end

Instance Method Details

#default_dictObject

Raises:

  • (ArgumentError)


62
63
64
65
66
67
# File 'lib/kamcaptcha/generator.rb', line 62

def default_dict
  return File.read("/usr/share/dict/words").split("\n") if File.exists?("/usr/share/dict/words")
  return File.read("/usr/dict/words").split("\n") if File.exists?("/usr/dict/words")

  raise ArgumentError.new("Sorry, need a dictionary file")
end

#generateObject



41
42
43
# File 'lib/kamcaptcha/generator.rb', line 41

def generate
  words[rand(words.size)].split("").join(" ")
end

#wordsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kamcaptcha/generator.rb', line 45

def words
  @words ||= begin
    if self.class.dict
      if self.class.dict.respond_to?(:call)
        w = self.class.dict.call
      else
        w = self.class.dict
      end
    else
      w = default_dict
    end

    w.each_with_index { |word, i| w[i] = word.upcase }
    w.select { |w| w.size >= min && w.size <= max && w !~ /O/ }
  end
end