Class: Captcha::Generator

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

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



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

def initialize
  generate
end

Instance Method Details

#generateObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/captcha/generator.rb', line 7

def generate
  return unless Config.options
  return if Config.last_modified && Config.last_modified > Time.now - Config.options[:generate_every]
  path = Config.options[:destination]
  Config.captchas.each do |captcha|
    FileUtils.rm_f captcha
  end
  FileUtils.mkdir_p path
  (1..Config.options[:count]).each do |x|
    image = Image.new Config.options
    path = "#{Config.options[:destination]}/#{Cipher.encrypt(image.code)}.jpg"
    next if File.exists?(path)
    File.open(path, 'w') do |f|
      f << image.data
    end
  end
  GC.start
end