Class: Unity::Captcha::Captcha

Inherits:
Object
  • Object
show all
Defined in:
lib/unity/captcha.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCaptcha

Returns a new instance of Captcha.



9
10
11
12
13
# File 'lib/unity/captcha.rb', line 9

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

Instance Attribute Details

#operand1Object

Returns the value of attribute operand1.



7
8
9
# File 'lib/unity/captcha.rb', line 7

def operand1
  @operand1
end

#operand2Object

Returns the value of attribute operand2.



7
8
9
# File 'lib/unity/captcha.rb', line 7

def operand2
  @operand2
end

#operatorObject

Returns the value of attribute operator.



7
8
9
# File 'lib/unity/captcha.rb', line 7

def operator
  @operator
end

Class Method Details

.decrypt(secret) ⇒ Object



28
29
30
31
32
# File 'lib/unity/captcha.rb', line 28

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

Instance Method Details

#correct?(value) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/unity/captcha.rb', line 20

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

#encryptObject



24
25
26
# File 'lib/unity/captcha.rb', line 24

def encrypt
	Base64.encode64 to_yaml
end

#initialize_from(secret) ⇒ Object



15
16
17
18
# 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

#questionObject



42
43
44
# File 'lib/unity/captcha.rb', line 42

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

#to_yamlObject



34
35
36
37
38
39
40
# File 'lib/unity/captcha.rb', line 34

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