Class: Logi::Config

Inherits:
Object
  • Object
show all
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 =

Production default. The shipped gem must point at prod out of the box — otherwise logi login issues the authorize request to start.1pass.dev but POSTs the token exchange to localhost, which 404s for every user. Local development overrides this with LOGI_API_URL=http://localhost:3000.

"https://api.1pass.dev"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, api_url:, source:, name: nil) ⇒ Config

Returns a new instance of Config.



45
46
47
48
49
50
# File 'lib/logi/config.rb', line 45

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_keyObject (readonly)

Returns the value of attribute api_key.



43
44
45
# File 'lib/logi/config.rb', line 43

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



43
44
45
# File 'lib/logi/config.rb', line 43

def api_url
  @api_url
end

#nameObject (readonly)

Returns the value of attribute name.



43
44
45
# File 'lib/logi/config.rb', line 43

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



43
44
45
# File 'lib/logi/config.rb', line 43

def source
  @source
end

Class Method Details

.clearObject



39
40
41
# File 'lib/logi/config.rb', line 39

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

.loadObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/logi/config.rb', line 17

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

.pathObject



13
14
15
# File 'lib/logi/config.rb', line 13

def self.path
  File.expand_path(ENV.fetch("LOGI_CONFIG_PATH", "~/.config/logi/credentials.json"))
end

.save(api_key:, api_url:, name:) ⇒ Object



33
34
35
36
37
# File 'lib/logi/config.rb', line 33

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/logi/config.rb', line 52

def authenticated?
  !api_key.nil? && !api_key.empty?
end