Class: Webhookdb::Customer::ResetCode

Inherits:
Object
  • Object
show all
Defined in:
lib/webhookdb/customer/reset_code.rb

Defined Under Namespace

Classes: Unusable

Constant Summary collapse

TOKEN_LENGTH =
6

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResetCode

Returns a new instance of ResetCode.



37
38
39
40
41
# File 'lib/webhookdb/customer/reset_code.rb', line 37

def initialize(*)
  super
  self.token ||= Array.new(TOKEN_LENGTH) { rand(0..9) }.join
  self.expire_at ||= 15.minutes.from_now
end

Class Method Details

.use_code_with_token(token) ⇒ Object

Invoke the given block with the reset code referred to by token. Raise Unusable if code is unusable.

Raises:

  • (LocalJumpError)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/webhookdb/customer/reset_code.rb', line 25

def self.use_code_with_token(token)
  raise LocalJumpError unless block_given?

  code = self.usable[token:]
  raise Unusable unless code&.usable?

  code.db.transaction do
    code.use!
    yield(code)
  end
end

Instance Method Details

#expire!Object



43
44
45
46
# File 'lib/webhookdb/customer/reset_code.rb', line 43

def expire!
  self.update(expire_at: Time.now)
  return self
end

#expired?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/webhookdb/customer/reset_code.rb', line 48

def expired?
  return self.expire_at < Time.now
end

#usable?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/webhookdb/customer/reset_code.rb', line 63

def usable?
  return false if self.used?
  return !self.expired?
end

#use!Object



52
53
54
55
56
57
# File 'lib/webhookdb/customer/reset_code.rb', line 52

def use!
  now = Time.now
  self.customer.reset_codes_dataset.usable.update(expire_at: now)
  self.update(used: true, expire_at: now)
  return self
end

#used?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/webhookdb/customer/reset_code.rb', line 59

def used?
  return self.used
end

#validateObject

:section: Sequel Validation



72
73
74
75
# File 'lib/webhookdb/customer/reset_code.rb', line 72

def validate
  super
  self.validates_includes(["email"], :transport)
end