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



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

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



56
57
58
59
60
61
62
63
64
# File 'lib/teuton/utils/configfile_reader.rb', line 56

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



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

def self.read_yaml(filepath)
  begin
    data = YAML.load(File.open(filepath))
    # data = YAML.safe_load(
    #   File.open(filepath),
    #   permitted_classes: [Array, Date, Hash, Symbol]
    # )
  rescue => e
    warn "\n" + ("=" * 80)
    warn "[ERROR] ConfigFileReader#read <#{filepath}>"
    warn "        I suggest to revise file format!"
    warn "        #{e.message}\n" + ("=" * 80)
    exit 1
  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