Module: Ramaze::Helper::SimpleCaptcha

Defined in:
lib/ramaze/helper/simple_captcha.rb

Instance Method Summary collapse

Instance Method Details

#check_captcha(answer) ⇒ Object



12
13
14
15
16
17
# File 'lib/ramaze/helper/simple_captcha.rb', line 12

def check_captcha(answer)
  if captcha = session[:CAPTCHA]
    should = captcha[:answer].to_s
    should == answer.to_s.strip
  end
end

#generate_captchaObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/ramaze/helper/simple_captcha.rb', line 19

def generate_captcha
  n = [5, 10, 15, 20]
  ns = Array.new(2){ n.sort_by{rand}.first }.sort
  op = rand > 0.42 ? [ns[0], :+, ns[1]] : [ns[1], :-, ns[0]]

  question = op.join(' ')
  answer = op[0].send(op[1], op[2])

  [question, answer]
end

#simple_captchaObject



4
5
6
7
8
9
10
# File 'lib/ramaze/helper/simple_captcha.rb', line 4

def simple_captcha
  question, answer = generate_captcha
  session[:CAPTCHA] = {
    :question => question, :answer => answer.to_s
  }
  question
end