Class: Agentd::Config
- Inherits:
-
Object
- Object
- Agentd::Config
- Defined in:
- lib/agentd/config.rb
Overview
Loads config from ~/.agentd/config.json Expected format: { “api_key”: “…”, “endpoint”: “…” }
Constant Summary collapse
- CONFIG_PATH =
File.("~/.agentd/config.json")
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data = {}) ⇒ Config
constructor
A new instance of Config.
Constructor Details
#initialize(data = {}) ⇒ Config
Returns a new instance of Config.
11 12 13 14 |
# File 'lib/agentd/config.rb', line 11 def initialize(data = {}) @api_key = data["api_key"] @endpoint = data["endpoint"] end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/agentd/config.rb', line 9 def api_key @api_key end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
9 10 11 |
# File 'lib/agentd/config.rb', line 9 def endpoint @endpoint end |
Class Method Details
.load ⇒ Object
16 17 18 19 20 21 |
# File 'lib/agentd/config.rb', line 16 def self.load return new unless File.exist?(CONFIG_PATH) new(JSON.parse(File.read(CONFIG_PATH))) rescue JSON::ParserError new end |
.save(api_key:, endpoint: nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/agentd/config.rb', line 23 def self.save(api_key:, endpoint: nil) dir = File.dirname(CONFIG_PATH) FileUtils.mkdir_p(dir) existing = load data = { "api_key" => api_key || existing.api_key, "endpoint" => endpoint || existing.endpoint }.compact File.write(CONFIG_PATH, JSON.pretty_generate(data)) end |