Module: StupidCaptcha

Included in:
Captcha
Defined in:
lib/stupid_captcha.rb,
lib/stupid_captcha/version.rb

Defined Under Namespace

Classes: Captcha

Constant Summary collapse

GEM_ROOT =
File.join(File.dirname(__FILE__), '..')
VERSION =
"0.0.2"
@@fonts_path =
File.join(GEM_ROOT, 'assets/fonts').to_s
@@fonts =
nil
@@backgrounds_path =
File.join(GEM_ROOT, 'assets/backgrounds').to_s
@@backgrounds =
nil
@@colors =
%w(black)
@@salt =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (StupidCaptcha)

    the object that the method was called on



22
23
24
# File 'lib/stupid_captcha.rb', line 22

def self.setup
  yield self
end

Instance Method Details

#backgroundsObject



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

def backgrounds
  @@backgrounds ||= files(backgrounds_path)
end

#check(hash, text) ⇒ Object



61
62
63
# File 'lib/stupid_captcha.rb', line 61

def check(hash, text)
  hash.eql?(encrypt(text, salt))
end

#encrypt(text, salt) ⇒ Object



33
34
35
# File 'lib/stupid_captcha.rb', line 33

def encrypt(text, salt)
  Digest::SHA1.hexdigest(text+salt)
end

#files(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/stupid_captcha.rb', line 50

def files(path)
  array = []
  Dir.foreach(path) do |x|
    file = path+"/"+x
    if File.file?(file)
      array << file
    end
  end
  return array
end

#fontsObject



37
38
39
# File 'lib/stupid_captcha.rb', line 37

def fonts
  @@fonts ||= files(fonts_path)
end

#random_string(len = 12) ⇒ Object



26
27
28
29
30
31
# File 'lib/stupid_captcha.rb', line 26

def random_string( len = 12)
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  str = ""
  1.upto(len) { |i| str << chars[rand(chars.size-1)] }
  return str
end

#saltObject



45
46
47
48
# File 'lib/stupid_captcha.rb', line 45

def salt
  raise "StupidCaptcha.salt is blank" if @@salt.blank?
  @@salt
end