Class: Unity::Captcha::Captcha
- Inherits:
-
Object
- Object
- Unity::Captcha::Captcha
- Defined in:
- lib/unity/captcha.rb
Instance Attribute Summary collapse
-
#operand1 ⇒ Object
Returns the value of attribute operand1.
-
#operand2 ⇒ Object
Returns the value of attribute operand2.
-
#operator ⇒ Object
Returns the value of attribute operator.
Class Method Summary collapse
Instance Method Summary collapse
- #correct?(value) ⇒ Boolean
- #encrypt ⇒ Object
-
#initialize ⇒ Captcha
constructor
A new instance of Captcha.
- #initialize_from(secret) ⇒ Object
- #question ⇒ Object
- #to_yaml ⇒ Object
Constructor Details
#initialize ⇒ Captcha
Returns a new instance of Captcha.
11 12 13 14 15 |
# File 'lib/unity/captcha.rb', line 11 def initialize @operand1 = (1..10).to_a.sample @operand2 = (1..10).to_a.sample @operator = [:+, :*].sample end |
Instance Attribute Details
#operand1 ⇒ Object
Returns the value of attribute operand1.
9 10 11 |
# File 'lib/unity/captcha.rb', line 9 def operand1 @operand1 end |
#operand2 ⇒ Object
Returns the value of attribute operand2.
9 10 11 |
# File 'lib/unity/captcha.rb', line 9 def operand2 @operand2 end |
#operator ⇒ Object
Returns the value of attribute operator.
9 10 11 |
# File 'lib/unity/captcha.rb', line 9 def operator @operator end |
Class Method Details
.decrypt(secret) ⇒ Object
30 31 32 33 34 |
# File 'lib/unity/captcha.rb', line 30 def self.decrypt(secret) result = new result.initialize_from secret result end |
Instance Method Details
#correct?(value) ⇒ Boolean
22 23 24 |
# File 'lib/unity/captcha.rb', line 22 def correct?(value) result == value.to_i end |
#encrypt ⇒ Object
26 27 28 |
# File 'lib/unity/captcha.rb', line 26 def encrypt Base64.encode64 to_yaml end |
#initialize_from(secret) ⇒ Object
17 18 19 20 |
# File 'lib/unity/captcha.rb', line 17 def initialize_from(secret) yml = YAML.load(Base64.decode64(secret)) @operand1, @operand2, @operator = yml[:operand1], yml[:operand2], yml[:operator] end |
#question ⇒ Object
44 45 46 |
# File 'lib/unity/captcha.rb', line 44 def question "What is #{@operand1} #{@operator.to_s} #{@operand2} = ?" end |
#to_yaml ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/unity/captcha.rb', line 36 def to_yaml YAML::dump({ :operand1 => @operand1, :operand2 => @operand2, :operator => @operator }) end |