Sha256: e1bb7ca8533e1575f616cc9b1641014e972904b61b24e19c7fc2a1609c85fcfc

Contents?: true

Size: 1.61 KB

Versions: 29

Compression:

Stored size: 1.61 KB

Contents

module Ramaze
  module Helper
    # Produce very simple question/answer pairs.
    #
    # The default is a trivial mathematical problem.
    #
    # Usage (trait is optional):
    #
    #   class RegisterController < Ramaze::Controller
    #     trait :captcha => lambda{
    #       ["the answer to everything", "42"]
    #     }
    #
    #     def index
    #       %(
    #         <form action="#{r(:answer}">
    #           What is #{simple_captcha}?
    #           <input type="text" name="answer" />"
    #           <input type="submit" />
    #         </form>
    #       ).strip
    #     end
    #
    #     def answer
    #       check_captcha(request[:answer])
    #     end
    #   end
    module SimpleCaptcha
      include Ramaze::Traited

      NUMBERS = [5, 10, 15, 20]

      # lambda should return question and answer in [question, answer] form
      trait :captcha => lambda{
        ns = Array.new(2){ NUMBERS.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]
      }

      # Call the trait[:captcha] and store question/answer in session
      def simple_captcha
        question, answer = ancestral_trait[:captcha].call

        session[:CAPTCHA] = { :question => question, :answer => answer.to_s }

        question
      end

      # check the given +answer+ against the answer stored in the session.
      def check_captcha(answer)
        return false unless captcha = session[:CAPTCHA]

        answer.to_s.strip == captcha[:answer].to_s
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 4 rubygems

Version Path
Pistos-ramaze-2009.04.08 lib/ramaze/helper/simple_captcha.rb
Pistos-ramaze-2009.06.12 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.04.01 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.04.08 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.04.18 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.04.22 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.04 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.05.08 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.05 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.06.04 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.06.12 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.06 lib/ramaze/helper/simple_captcha.rb
manveru-ramaze-2009.07 lib/ramaze/helper/simple_captcha.rb
rjspotter-ramaze-2009.06.29 lib/ramaze/helper/simple_captcha.rb
rjspotter-ramaze-2009.06.31 lib/ramaze/helper/simple_captcha.rb
ramaze-2011.01.30 lib/ramaze/helper/simple_captcha.rb
ramaze-2011.01 lib/ramaze/helper/simple_captcha.rb
ramaze-2010.06.18 lib/ramaze/helper/simple_captcha.rb
ramaze-2010.04.04 lib/ramaze/helper/simple_captcha.rb
ramaze-2010.04 lib/ramaze/helper/simple_captcha.rb