Class: Omaship::Credentials
- Inherits:
-
Object
- Object
- Omaship::Credentials
- Defined in:
- lib/omaship/credentials.rb
Constant Summary collapse
- DEFAULT_PATH =
File.join(Dir.home, ".config", "omaship", "credentials.json")
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #clear ⇒ Object
- #clear_default_ship ⇒ Object
- #default_ship ⇒ Object
- #host ⇒ Object
-
#initialize(path: DEFAULT_PATH) ⇒ Credentials
constructor
A new instance of Credentials.
- #read ⇒ Object
- #token ⇒ Object
- #write(host:, token:, user:) ⇒ Object
- #write_default_ship(ship_reference) ⇒ Object
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
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/omaship/credentials.rb', line 12 def path @path end |
Instance Method Details
#clear ⇒ Object
58 59 60 |
# File 'lib/omaship/credentials.rb', line 58 def clear File.delete(@path) if File.exist?(@path) end |
#clear_default_ship ⇒ Object
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_ship ⇒ Object
40 41 42 |
# File 'lib/omaship/credentials.rb', line 40 def default_ship read["default_ship"] end |
#host ⇒ Object
36 37 38 |
# File 'lib/omaship/credentials.rb', line 36 def host read["host"] end |
#read ⇒ Object
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 |
#token ⇒ Object
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 |