Sha256: d732ac95954ca1a40152f64aa3f1226fd724e41f07e7e52cc5fea8467b84e646

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require "unity/captcha/version"
require "unity/captcha/engine"

module Unity
  module Captcha
    class Captcha 
      attr_accessor :operand1, :operand2, :operator

      def initialize
        @operand1 = (1..10).to_a.sample
        @operand2 = (1..10).to_a.sample
        @operator = [:+, :*].sample  
      end

      def initialize_from(secret)
        yml = YAML.load(Base64.decode64(secret))
        @operand1, @operand2, @operator = yml[:operand1], yml[:operand2], yml[:operator]
      end

      def correct?(value)
        result == value.to_i
      end

      def encrypt
      	Base64.encode64 to_yaml
      end

      def self.decrypt(secret)
        result = new
        result.initialize_from secret
        result
      end

      def to_yaml
        YAML::dump({
          :operand1 => @operand1,
          :operand2 => @operand2,
          :operator => @operator
        })
      end

      def question
        "What is #{@operand1} #{@operator.to_s} #{@operand2} = ?"
      end

      private

      def result
        @operand1.send @operator, @operand2
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unity-captcha-1.0.1 lib/unity/captcha.rb