Class: Logi::Config
- Inherits:
-
Object
- Object
- Logi::Config
- Defined in:
- lib/logi/config.rb
Overview
Load/save credentials.json. Default path: ~/.config/logi/credentials.json Environment variables LOGI_API_KEY / LOGI_API_URL take precedence when set.
Constant Summary collapse
- DEFAULT_API_URL =
"http://localhost:3000"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #authenticated? ⇒ Boolean
-
#initialize(api_key:, api_url:, source:, name: nil) ⇒ Config
constructor
A new instance of Config.
Constructor Details
#initialize(api_key:, api_url:, source:, name: nil) ⇒ Config
Returns a new instance of Config.
41 42 43 44 45 46 |
# File 'lib/logi/config.rb', line 41 def initialize(api_key:, api_url:, source:, name: nil) @api_key = api_key @api_url = api_url @source = source @name = name end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
39 40 41 |
# File 'lib/logi/config.rb', line 39 def api_key @api_key end |
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
39 40 41 |
# File 'lib/logi/config.rb', line 39 def api_url @api_url end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
39 40 41 |
# File 'lib/logi/config.rb', line 39 def name @name end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
39 40 41 |
# File 'lib/logi/config.rb', line 39 def source @source end |
Class Method Details
.clear ⇒ Object
35 36 37 |
# File 'lib/logi/config.rb', line 35 def self.clear File.delete(path) if File.exist?(path) end |
.load ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/logi/config.rb', line 13 def self.load env_key = ENV["LOGI_API_KEY"] env_url = ENV["LOGI_API_URL"] if env_key return new(api_key: env_key, api_url: env_url || DEFAULT_API_URL, source: :env) end if File.exist?(path) data = JSON.parse(File.read(path)) return new(api_key: data["api_key"], api_url: data["api_url"] || DEFAULT_API_URL, source: :file, name: data["name"]) end new(api_key: nil, api_url: env_url || DEFAULT_API_URL, source: :none) end |
.path ⇒ Object
9 10 11 |
# File 'lib/logi/config.rb', line 9 def self.path File.(ENV.fetch("LOGI_CONFIG_PATH", "~/.config/logi/credentials.json")) end |
.save(api_key:, api_url:, name:) ⇒ Object
29 30 31 32 33 |
# File 'lib/logi/config.rb', line 29 def self.save(api_key:, api_url:, name:) FileUtils.mkdir_p(File.dirname(path)) File.write(path, JSON.pretty_generate({ api_key: api_key, api_url: api_url, name: name })) File.chmod(0o600, path) end |
Instance Method Details
#authenticated? ⇒ Boolean
48 49 50 |
# File 'lib/logi/config.rb', line 48 def authenticated? !api_key.nil? && !api_key.empty? end |