Class: Legion::Identity::Lease

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/identity/lease.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:, credential:, lease_id: nil, expires_at: nil, renewable: false, issued_at: nil, metadata: {}) ⇒ Lease

rubocop:disable Metrics/ParameterLists



8
9
10
11
12
13
14
15
16
# File 'lib/legion/identity/lease.rb', line 8

def initialize(provider:, credential:, lease_id: nil, expires_at: nil, renewable: false, issued_at: nil, metadata: {}) # rubocop:disable Metrics/ParameterLists
  @provider = provider
  @credential = credential
  @lease_id = lease_id
  @expires_at = expires_at
  @renewable = renewable
  @issued_at = issued_at || Time.now
  @metadata = .freeze
end

Instance Attribute Details

#credentialObject (readonly)

Returns the value of attribute credential.



6
7
8
# File 'lib/legion/identity/lease.rb', line 6

def credential
  @credential
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



6
7
8
# File 'lib/legion/identity/lease.rb', line 6

def expires_at
  @expires_at
end

#issued_atObject (readonly)

Returns the value of attribute issued_at.



6
7
8
# File 'lib/legion/identity/lease.rb', line 6

def issued_at
  @issued_at
end

#lease_idObject (readonly)

Returns the value of attribute lease_id.



6
7
8
# File 'lib/legion/identity/lease.rb', line 6

def lease_id
  @lease_id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



6
7
8
# File 'lib/legion/identity/lease.rb', line 6

def 
  @metadata
end

#providerObject (readonly)

Returns the value of attribute provider.



6
7
8
# File 'lib/legion/identity/lease.rb', line 6

def provider
  @provider
end

#renewableObject (readonly)

Returns the value of attribute renewable.



6
7
8
# File 'lib/legion/identity/lease.rb', line 6

def renewable
  @renewable
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/legion/identity/lease.rb', line 22

def expired?
  return false if expires_at.nil?

  Time.now >= expires_at
end

#stale?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/legion/identity/lease.rb', line 28

def stale?
  return false if expires_at.nil? || issued_at.nil?

  elapsed = Time.now - issued_at
  total = expires_at - issued_at
  return false if total <= 0

  elapsed >= (total * 0.5)
end

#to_hObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/identity/lease.rb', line 49

def to_h
  {
    provider:   provider,
    lease_id:   lease_id,
    expires_at: expires_at&.iso8601,
    renewable:  renewable,
    issued_at:  issued_at&.iso8601,
    ttl:        ttl_seconds,
    valid:      valid?,
    metadata:   
  }
end

#tokenObject



18
19
20
# File 'lib/legion/identity/lease.rb', line 18

def token
  credential
end

#ttl_secondsObject



38
39
40
41
42
43
# File 'lib/legion/identity/lease.rb', line 38

def ttl_seconds
  return nil if expires_at.nil?

  remaining = expires_at - Time.now
  remaining.negative? ? 0 : remaining.to_i
end

#valid?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/legion/identity/lease.rb', line 45

def valid?
  !credential.nil? && !expired?
end