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.
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
#operand1 ⇒ Object
Returns the value of attribute operand1.
7 8 9 |
# File 'lib/unity/captcha.rb', line 7 def operand1 @operand1 end |
#operand2 ⇒ Object
Returns the value of attribute operand2.
7 8 9 |
# File 'lib/unity/captcha.rb', line 7 def operand2 @operand2 end |
#operator ⇒ Object
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
20 21 22 |
# File 'lib/unity/captcha.rb', line 20 def correct?(value) result == value.to_i end |
#encrypt ⇒ Object
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 |
#question ⇒ Object
42 43 44 |
# File 'lib/unity/captcha.rb', line 42 def question "What is #{@operand1} #{@operator.to_s} #{@operand2} = ?" end |
#to_yaml ⇒ Object
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 |