Module: CodewarsCli::Configuration

Includes:
GenericHelpers
Defined in:
lib/codewars_cli/configuration.rb

Constant Summary collapse

CONFIG_FILE =
'.codewars.rc.yml'
CONFIG_CONTENTS =
{
  api_key: '',
  language: '',
  folder: '',
}

Class Method Summary collapse

Methods included from GenericHelpers

#check_for_api_key, #client, #error, #extend_object, included, #info, #presenter

Class Method Details

.api_keyObject



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

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

.config_dataObject



51
52
53
# File 'lib/codewars_cli/configuration.rb', line 51

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

.config_fileObject



47
48
49
# File 'lib/codewars_cli/configuration.rb', line 47

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

.create_config_fileObject



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

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

.folderObject



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

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

.languageObject



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

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

.update_value(value, key) ⇒ Object



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

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



12
13
14
15
16
17
18
19
20
21
# File 'lib/codewars_cli/configuration.rb', line 12

def self.write(value, key, options)
  create_config_file unless File.exists? config_file
  update_value(value, key) && return if options['update'] && !send(key).empty?
  if !send(key).empty?
    info "Do you want to overwrite #{key} provide --update option"
  else
    info "Updating config file with #{key}: #{value}"
    update_value(value, key)
  end
end