Module: FootballCli::Configuration

Defined in:
lib/football_cli/configuration.rb

Constant Summary collapse

CONFIG_FILE =
'.football_cli.yml'
CONFIG_CONTENTS =
{
  api_token: '',
}

Class Method Summary collapse

Class Method Details

.api_tokenObject



35
36
37
# File 'lib/football_cli/configuration.rb', line 35

def self.api_token
  config_data[:api_token] if config_data
end

.config_dataObject



43
44
45
# File 'lib/football_cli/configuration.rb', line 43

def self.config_data
  YAML::load_file(config_file) if File.exist? config_file
end

.config_fileObject



39
40
41
# File 'lib/football_cli/configuration.rb', line 39

def self.config_file
  File.join(ENV['HOME'], CONFIG_FILE)
end

.create_config_fileObject



31
32
33
# File 'lib/football_cli/configuration.rb', line 31

def self.create_config_file
  File.open(config_file, 'w+') { |f| f.write(CONFIG_CONTENTS.to_yaml) }
end

.update_value(value, key) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/football_cli/configuration.rb', line 22

def self.update_value(value, key)
  content = config_data
  content.send(:[]=, key, value)

  File.open(config_file, 'w') do |f|
    f.write(content.to_yaml)
  end
end

.write(value, key, options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/football_cli/configuration.rb', line 10

def self.write(value, key, options)
  create_config_file unless File.exist? config_file
  update_value(value, key) && return if options['update'] && !send(key).empty?

  if send(key).empty?
    puts "Updating config file with #{key}: #{value}"
    update_value(value, key)
  else
    puts "Do you want to overwrite #{key} provide --update option"
  end
end