Class: Shai::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/shai/credentials.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Credentials

Returns a new instance of Credentials.



10
11
12
# File 'lib/shai/credentials.rb', line 10

def initialize
  load_credentials
end

Instance Attribute Details

#expires_at ⇒ Object (readonly)

Returns the value of attribute expires_at.



8
9
10
# File 'lib/shai/credentials.rb', line 8

def expires_at
  @expires_at
end

#token ⇒ Object (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/shai/credentials.rb', line 8

def token
  @token
end

#user ⇒ Object (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/shai/credentials.rb', line 8

def user
  @user
end

Instance Method Details

#authenticated? ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/shai/credentials.rb', line 14

def authenticated?
  !!(token && !expired?)
end

#clear ⇒ Object



34
35
36
37
38
39
40
# File 'lib/shai/credentials.rb', line 34

def clear
  @token = nil
  @expires_at = nil
  @user = nil

  FileUtils.rm_f(credentials_path)
end

#display_name ⇒ Object



46
47
48
# File 'lib/shai/credentials.rb', line 46

def display_name
  user&.dig("display_name")
end

#expired? ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/shai/credentials.rb', line 18

def expired?
  return false unless expires_at

  Time.parse(expires_at) < Time.now
end

#save(token:, expires_at:, user:) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/shai/credentials.rb', line 24

def save(token:, expires_at:, user:)
  @token = token
  @expires_at = expires_at
  @user = user

  ensure_config_dir
  File.write(credentials_path, credentials_json)
  File.chmod(0o600, credentials_path)
end

#username ⇒ Object



42
43
44
# File 'lib/shai/credentials.rb', line 42

def username
  user&.dig("username")
end