Module: ConfigFileReader

Defined in:
lib/teuton/utils/configfile_reader.rb

Overview

Functions that read data from ConfigFile using YAML or JSON formats

Class Method Summary collapse

Class Method Details

.read(filepath) ⇒ Object

Read config file

Parameters:

  • filepath (String)

    Path to config file

Returns:

  • Hash with config data



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/teuton/utils/configfile_reader.rb', line 12

def self.read(filepath)
  unless File.exist?(filepath)
    data = {}
    data[:global] = {}
    data[:alias] = {}
    data[:cases] = [{ tt_members: 'anonymous' }]
    return data
  end
  return read_yaml(filepath) if File.extname(filepath) == '.yaml'

  return read_json(filepath) if File.extname(filepath) == '.json'

  raise "[ERROR] ConfigFileReader: #{filepath}"
end

.read_json(filepath) ⇒ Object

Read JSON config file

Parameters:

  • filepath (String)

    Path to JSON config file

Returns:

  • Hash with config data



53
54
55
56
57
58
59
60
61
# File 'lib/teuton/utils/configfile_reader.rb', line 53

def self.read_json(filepath)
  data = JSON.parse(File.read(filepath), symbolize_names: true)
  data = convert_string_keys_to_symbol(data)
  data[:global] = data[:global] || {}
  data[:alias] = data[:alias] || {}
  data[:cases] = data[:cases] || []
  read_included_files!(filepath, data)
  data
end

.read_yaml(filepath) ⇒ Object

Read YAML config file

Parameters:

  • filepath (String)

    Path to YAML config file

Returns:

  • Hash with config data



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/teuton/utils/configfile_reader.rb', line 31

def self.read_yaml(filepath)
  begin
    data = YAML.load(File.open(filepath))
  rescue StandardError => e
    puts "\n" + ('=' * 80)
    puts "[ERROR] ConfigFileReader#read <#{filepath}>"
    puts '        I suggest to revise file format!'
    puts "        #{e.message}\n" + ('=' * 80)
    raise "[ERROR] ConfigFileReader <#{e}>"
  end
  data = convert_string_keys_to_symbol(data)
  data[:global] = data[:global] || {}
  data[:alias] = data[:alias] || {}
  data[:cases] = data[:cases] || []
  read_included_files!(filepath, data)
  data
end