Class: Omaship::Credentials

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

Constant Summary collapse

DEFAULT_PATH =
File.join(Dir.home, ".config", "omaship", "credentials.json")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: DEFAULT_PATH) ⇒ Credentials

Returns a new instance of Credentials.



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

def initialize(path: DEFAULT_PATH)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/omaship/credentials.rb', line 12

def path
  @path
end

Instance Method Details

#clearObject



58
59
60
# File 'lib/omaship/credentials.rb', line 58

def clear
  File.delete(@path) if File.exist?(@path)
end

#clear_default_shipObject



52
53
54
55
56
# File 'lib/omaship/credentials.rb', line 52

def clear_default_ship
  payload = read
  payload.delete("default_ship")
  write_payload(payload)
end

#default_shipObject



40
41
42
# File 'lib/omaship/credentials.rb', line 40

def default_ship
  read["default_ship"]
end

#hostObject



36
37
38
# File 'lib/omaship/credentials.rb', line 36

def host
  read["host"]
end

#readObject



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

def read
  return {} unless File.exist?(@path)

  JSON.parse(File.read(@path))
rescue JSON::ParserError
  {}
end

#tokenObject



32
33
34
# File 'lib/omaship/credentials.rb', line 32

def token
  read["token"]
end

#write(host:, token:, user:) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/omaship/credentials.rb', line 14

def write(host:, token:, user:)
  payload = read
  payload["version"] = 1
  payload["host"] = host
  payload["token"] = token
  payload["user"] = user

  write_payload(payload)
end

#write_default_ship(ship_reference) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/omaship/credentials.rb', line 44

def write_default_ship(ship_reference)
  payload = read
  payload["version"] = 1
  payload["default_ship"] = ship_reference

  write_payload(payload)
end