Class: Kamcaptcha::Generator::DictionaryWordGenerator
Class Attribute Summary collapse
-
.dict ⇒ Object
Use this to set a custom dictionary or dictionary generating function.
#max, #min
Instance Method Summary
collapse
#initialize
Class Attribute Details
.dict ⇒ Object
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_dict ⇒ Object
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
|
#generate ⇒ Object
41
42
43
|
# File 'lib/kamcaptcha/generator.rb', line 41
def generate
words[rand(words.size)].split("").join(" ")
end
|
#words ⇒ Object
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
|