class Unity::Captcha::Captcha

Attributes

operand1[RW]
operand2[RW]
operator[RW]

Public Class Methods

decrypt(secret) click to toggle source
# File lib/unity/captcha.rb, line 28
def self.decrypt(secret)
  result = new
  result.initialize_from secret
  result
end
new() click to toggle source
# File lib/unity/captcha.rb, line 9
def initialize
  @operand1 = (1..10).to_a.sample
  @operand2 = (1..10).to_a.sample
  @operator = [:+, :*].sample  
end

Public Instance Methods

correct?(value) click to toggle source
# File lib/unity/captcha.rb, line 20
def correct?(value)
  result == value.to_i
end
encrypt() click to toggle source
# File lib/unity/captcha.rb, line 24
def encrypt
  Base64.encode64 to_yaml
end
initialize_from(secret) click to toggle source
# File lib/unity/captcha.rb, line 15
def initialize_from(secret)
  yml = YAML.load(Base64.decode64(secret))
  @operand1, @operand2, @operator = yml[:operand1], yml[:operand2], yml[:operator]
end
question() click to toggle source
# File lib/unity/captcha.rb, line 42
def question
  "What is #{@operand1} #{@operator.to_s} #{@operand2} = ?"
end
to_yaml() click to toggle source
# File lib/unity/captcha.rb, line 34
def to_yaml
  YAML::dump({
    :operand1 => @operand1,
    :operand2 => @operand2,
    :operator => @operator
  })
end

Private Instance Methods

result() click to toggle source
# File lib/unity/captcha.rb, line 48
def result
  @operand1.send @operator, @operand2
end