Class: SdrClient::Credentials

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

Overview

The stored credentials

Defined Under Namespace

Classes: NoCredentialsError

Class Method Summary collapse

Class Method Details

.credentials_fileObject



31
32
33
# File 'lib/sdr_client/credentials.rb', line 31

def self.credentials_file
  File.join(credentials_path, 'credentials')
end

.credentials_pathObject



27
28
29
# File 'lib/sdr_client/credentials.rb', line 27

def self.credentials_path
  @credentials_path ||= File.join(Dir.home, '.sdr')
end

.readObject

Raises:



18
19
20
21
22
23
24
25
# File 'lib/sdr_client/credentials.rb', line 18

def self.read
  raise NoCredentialsError unless ::File.exist?(credentials_file)

  creds = IO.readlines(credentials_file, chomp: true).first if ::File.exist?(credentials_file)
  raise NoCredentialsError if creds.nil?

  creds
end

.write(body) ⇒ Object

Parameters:

  • a (String)

    json string that contains a field ‘token’



9
10
11
12
13
14
15
16
# File 'lib/sdr_client/credentials.rb', line 9

def self.write(body)
  json = JSON.parse(body)
  Dir.mkdir(credentials_path, 0o700) unless Dir.exist?(credentials_path)
  File.atomic_write(credentials_file) do |file|
    file.write(json.fetch('token'))
  end
  File.chmod(0o600, credentials_file)
end