Class: LightTr::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/light_tr/config.rb

Defined Under Namespace

Classes: SetConfigError

Class Method Summary collapse

Class Method Details

.api_keyObject



20
21
22
23
24
# File 'lib/light_tr/config.rb', line 20

def self.api_key
  get['api_key'] || raise('Google Translation API key missing. Try to update configuration via `trans -config`')
  master_key = get['master_key'] || raise('Config file corrupted. Try to update configuration via `trans -config`')
  Lockbox.new(key: master_key).decrypt(get['api_key'])
end

.clearObject



63
64
65
# File 'lib/light_tr/config.rb', line 63

def self.clear
  File.write(config_file, {}.to_yaml)
end

.config_exists?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/light_tr/config.rb', line 38

def self.config_exists?
  File.exist?(config_file)
end

.config_fileObject



46
47
48
# File 'lib/light_tr/config.rb', line 46

def self.config_file
  File.expand_path('.config', config_path)
end

.config_missing?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/light_tr/config.rb', line 42

def self.config_missing?
  !config_exists?
end

.config_pathObject



34
35
36
# File 'lib/light_tr/config.rb', line 34

def self.config_path
  File.join(Dir.home, '.light_tr')
end

.getObject



50
51
52
53
54
# File 'lib/light_tr/config.rb', line 50

def self.get
  return load_configuration if config_exists?

  {}
end

.languagesObject



10
11
12
# File 'lib/light_tr/config.rb', line 10

def self.languages
  get['languages']
end

.modelObject



30
31
32
# File 'lib/light_tr/config.rb', line 30

def self.model
  get['model'] || 'gpt-3.5-turbo'
end

.open_ai_keyObject



14
15
16
17
18
# File 'lib/light_tr/config.rb', line 14

def self.open_ai_key
  get['open_ai_key'] || raise('OpenAI API key missing. Try to update configuration via `trans -config`')
  master_key = get['master_key'] || raise('Config file corrupted. Try to update configuration via `trans -config`')
  Lockbox.new(key: master_key).decrypt(get['open_ai_key'])
end

.providerObject



26
27
28
# File 'lib/light_tr/config.rb', line 26

def self.provider
  get['provider'] || 'google'
end

.set(hash) ⇒ Object

Raises:



56
57
58
59
60
61
# File 'lib/light_tr/config.rb', line 56

def self.set(hash)
  raise SetConfigError, 'argument is not kind of hash' unless hash.kind_of?(Hash)

  updated_config = get.merge(hash)
  save_configuration(updated_config)
end