Sha256: a956cf67413af14a54437cd58d9dba47dd77287a7061e1b796d688ce10f0e9fd

Contents?: true

Size: 730 Bytes

Versions: 17

Compression:

Stored size: 730 Bytes

Contents

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

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

      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
    end
  end
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
Pistos-ramaze-2008.09 lib/ramaze/helper/simple_captcha.rb
Pistos-ramaze-2008.12 lib/ramaze/helper/simple_captcha.rb
Pistos-ramaze-2009.01 lib/ramaze/helper/simple_captcha.rb
Pistos-ramaze-2009.02 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2008.07 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2008.08 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2008.09 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2008.10 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2008.12 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.01 lib/ramaze/helper/simple_captcha.rb
ptomato-ramaze-2009.02.1 lib/ramaze/helper/simple_captcha.rb
ptomato-ramaze-2009.02 lib/ramaze/helper/simple_captcha.rb
ramaze-2008.06 lib/ramaze/helper/simple_captcha.rb
ramaze-2009.01 lib/ramaze/helper/simple_captcha.rb
ramaze-2008.11 lib/ramaze/helper/simple_captcha.rb
ramaze-2009.03 lib/ramaze/helper/simple_captcha.rb
ramaze-2009.02 lib/ramaze/helper/simple_captcha.rb