Class: Gringotts::Vault

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gringotts/vault.rb

Constant Summary collapse

SESSION_FRESHNESS_KEY =
:gringotts_expires_at

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_owner(obj) ⇒ Object



15
16
17
18
19
# File 'app/models/gringotts/vault.rb', line 15

def self.for_owner(obj)
  # this is not quite as pretty as the new rails4 way, but we want it to be backwards compatible without a separate branch
  conditions = { owner_id: obj.id, owner_type: obj.class.name }
  return Gringotts::Vault.where(conditions).first || Gringotts::Vault.create(conditions)
end

Instance Method Details

#confirm!Object



41
42
43
# File 'app/models/gringotts/vault.rb', line 41

def confirm!
  self.update_attributes!(confirmed_at: Time.now) unless self.confirmed?
end

#confirmed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/gringotts/vault.rb', line 37

def confirmed?
  self.confirmed_at.present?
end

#deliver_new_code!Object



68
69
70
71
72
73
74
75
76
# File 'app/models/gringotts/vault.rb', line 68

def deliver_new_code!
  code = self.new_code
  # I'm not proud of this but...
  # You have to 'create' here, not 'new'
  # Because delivery uses before_validation callbacks, instead of after_initialize
  # That's a whole 'nother can of worms...
  # ...just FYI
  return Delivery.create(vault: self).deliver!
end

#last_4Object



82
83
84
# File 'app/models/gringotts/vault.rb', line 82

def last_4
  self.phone_number.present? ? self.phone_number[-4..-1] : nil
end

#lock!Object



91
92
93
# File 'app/models/gringotts/vault.rb', line 91

def lock!
  self.update_attributes(locked_at: Time.now)
end

#locked?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/gringotts/vault.rb', line 95

def locked?
  return self.locked_at.present? && self.locked_at > Gringotts::AttemptValidator::LOCKOUT_PERIOD.ago
end

#new_codeObject



63
64
65
66
# File 'app/models/gringotts/vault.rb', line 63

def new_code
  self.codes << Gringotts::Code.create({vault: self})
  return self.recent_code
end

#opted_in?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/gringotts/vault.rb', line 25

def opted_in?
  return self.signed_up? && self.confirmed?
end

#phone_numberObject



78
79
80
# File 'app/models/gringotts/vault.rb', line 78

def phone_number
  return self.settings.present? ? self.settings.phone_number : nil
end

#prompt_shown!Object



33
34
35
# File 'app/models/gringotts/vault.rb', line 33

def prompt_shown!
  self.update_attributes!(prompt_seen_at: Time.now)
end

#recent_codeObject



57
58
59
60
61
# File 'app/models/gringotts/vault.rb', line 57

def recent_code
  # generate a new code if there is no previous code!
  # aka, should never have a null code
  return self.codes.last || self.new_code
end

#should_lock?Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'app/models/gringotts/vault.rb', line 86

def should_lock?
  return false unless self.confirmed?
  self.attempts.unsuccessful.since(Time.now - Gringotts::AttemptValidator::LOCKOUT_PERIOD).count  >= Gringotts::AttemptValidator::MAX_UNSUCCESSFUL_ATTEMPTS
end

#show_prompt?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/gringotts/vault.rb', line 29

def show_prompt?
  return self.prompt_seen_at.nil?
end

#signed_up?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/models/gringotts/vault.rb', line 21

def signed_up?
  return self.settings.present? && self.settings.phone_number.present?
end

#unconfirm!Object



45
46
47
# File 'app/models/gringotts/vault.rb', line 45

def unconfirm!
  self.update_attributes!(confirmed_at: nil)
end

#unlock!Object



99
100
101
# File 'app/models/gringotts/vault.rb', line 99

def unlock!
  self.update_attributes(locked_at: nil)
end

#verified?(session) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/gringotts/vault.rb', line 49

def verified?(session)
  return session[SESSION_FRESHNESS_KEY].present? && session[SESSION_FRESHNESS_KEY] >= Time.now
end

#verify!(session) ⇒ Object



53
54
55
# File 'app/models/gringotts/vault.rb', line 53

def verify!(session)
  session[SESSION_FRESHNESS_KEY] = (Time.now + 30.days)
end