Module: Waves::Config

Defined in:
lib/waves/config.rb

Class Method Summary collapse

Class Method Details

.clear ⇒ Object



33
34
35
# File 'lib/waves/config.rb', line 33

def clear
  File.delete(config_path) if File.exist?(config_path)
end

.config_dir ⇒ Object



9
10
11
# File 'lib/waves/config.rb', line 9

def config_dir
  File.join(Dir.home, ".config", "waves")
end

.config_path ⇒ Object



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

def config_path
  File.join(config_dir, "config.json")
end

.load ⇒ Object



37
38
39
40
41
42
43
# File 'lib/waves/config.rb', line 37

def load
  return {} unless File.exist?(config_path)

  JSON.parse(File.read(config_path))
rescue JSON::ParserError
  raise InvalidConfigError, "Invalid config file at #{config_path}"
end

.save_token(token) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
# File 'lib/waves/config.rb', line 25

def save_token(token)
  raise ArgumentError, "token is required" if token.to_s.empty?

  FileUtils.mkdir_p(config_dir)
  File.write(config_path, JSON.pretty_generate("token" => token))
  File.chmod(0o600, config_path)
end

.stored_token ⇒ Object



21
22
23
# File 'lib/waves/config.rb', line 21

def stored_token
  load["token"]
end

.token ⇒ Object



17
18
19
# File 'lib/waves/config.rb', line 17

def token
  ENV["WAVES_API_TOKEN"] || stored_token
end