Class: Gringotts::AttemptValidator
- Inherits:
-
Object
- Object
- Gringotts::AttemptValidator
- Defined in:
- app/models/gringotts/attempt_validator.rb
Constant Summary collapse
- CODE_FRESHNESS_LIMIT =
30.minutes
- LOCKOUT_PERIOD =
30.minutes
- MAX_UNSUCCESSFUL_ATTEMPTS =
3
Instance Attribute Summary collapse
-
#attempt ⇒ Object
readonly
Returns the value of attribute attempt.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attempt, code) ⇒ AttemptValidator
constructor
A new instance of AttemptValidator.
- #mark_successful ⇒ Object
- #mark_unsuccessful(msg) ⇒ Object
- #matches? ⇒ Boolean
- #stale? ⇒ Boolean
- #successful? ⇒ Boolean
- #used? ⇒ Boolean
Constructor Details
#initialize(attempt, code) ⇒ AttemptValidator
Returns a new instance of AttemptValidator.
12 13 14 15 |
# File 'app/models/gringotts/attempt_validator.rb', line 12 def initialize(attempt, code) @attempt = attempt @code = code end |
Instance Attribute Details
#attempt ⇒ Object (readonly)
Returns the value of attribute attempt.
17 18 19 |
# File 'app/models/gringotts/attempt_validator.rb', line 17 def attempt @attempt end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
17 18 19 |
# File 'app/models/gringotts/attempt_validator.rb', line 17 def code @code end |
Class Method Details
.valid?(attempt) ⇒ Boolean
8 9 10 |
# File 'app/models/gringotts/attempt_validator.rb', line 8 def self.valid?(attempt) return AttemptValidator.new(attempt, attempt.vault.recent_code).successful? end |
Instance Method Details
#mark_successful ⇒ Object
41 42 43 |
# File 'app/models/gringotts/attempt_validator.rb', line 41 def mark_successful @attempt.successful = 1 end |
#mark_unsuccessful(msg) ⇒ Object
36 37 38 39 |
# File 'app/models/gringotts/attempt_validator.rb', line 36 def mark_unsuccessful(msg) @attempt.successful = 0 @attempt.errors[:validator] << msg end |
#matches? ⇒ Boolean
53 54 55 |
# File 'app/models/gringotts/attempt_validator.rb', line 53 def matches? return @attempt.code_received == @code.value end |
#stale? ⇒ Boolean
45 46 47 |
# File 'app/models/gringotts/attempt_validator.rb', line 45 def stale? return @code.expires_at < (Time.now - CODE_FRESHNESS_LIMIT) end |
#successful? ⇒ Boolean
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/gringotts/attempt_validator.rb', line 19 def successful? if self.matches? if self.stale? self.mark_unsuccessful("Code expired") elsif self.used? self.mark_unsuccessful("Code already used") else self.mark_successful end else self.mark_unsuccessful("Invalid code") end return @attempt.successful? end |