Module: ConfigFileReader

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

Overview

Read config file content. Available file formats: YAML or JSON

Class Method Summary collapse

Class Method Details

.call(filepath) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/teuton/utils/config_file_reader.rb', line 7

def self.call(filepath)
  return minimum_configuration_with_one_case unless File.exist?(filepath)

  return read_yaml(filepath) if [".yaml", ".yml"].include? File.extname(filepath)

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

  raise "[ERROR] ConfigFileReader.call: <#{filepath}>. Unkown extension!"
end

.convert_string_keys_to_symbol(input) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/teuton/utils/config_file_reader.rb', line 17

def self.convert_string_keys_to_symbol(input)
  return input if input.class != Hash

  output = {}
  input.each_pair do |key, value|
    key2 = key
    key2 = key.to_sym if key.class
    value2 = value
    if value.instance_of? Hash
      value2 = convert_string_keys_to_symbol(value)
    elsif value.instance_of? Array
      value2 = []
      value.each { |i| value2 << convert_string_keys_to_symbol(i) }
    end
    output[key2] = value2
  end
  output
end

.is_json_file?(filepath) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/teuton/utils/config_file_reader.rb', line 52

def self.is_json_file?(filepath)
  extensions = [".json", ".JSON"]
  ext = File.extname(filepath)
  return true if File.file?(filepath) && extensions.include?(ext)

  false
end

.is_yaml_file?(filepath) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/teuton/utils/config_file_reader.rb', line 44

def self.is_yaml_file?(filepath)
  extensions = [".yaml", ".YAML", ".yml", ".YML"]
  ext = File.extname(filepath)
  return true if File.file?(filepath) && extensions.include?(ext)

  false
end

.minimum_configuration_with_one_caseObject



36
37
38
39
40
41
42
# File 'lib/teuton/utils/config_file_reader.rb', line 36

def self.minimum_configuration_with_one_case
  data = {}
  data[:global] = {}
  data[:alias] = {}
  data[:cases] = [{tt_members: "anonymous"}]
  data
end

.read_included_files!(filepath, data) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/teuton/utils/config_file_reader.rb', line 86

def self.read_included_files!(filepath, data)
  return if data[:global][:tt_include].nil?

  include_dir = data[:global][:tt_include]
  basedir = if include_dir == File.absolute_path(include_dir)
    include_dir
  else
    File.join(File.dirname(filepath), data[:global][:tt_include])
  end
  filepaths = Dir.glob(File.join(basedir, "**/*"))
  filepaths.each { |filepath|
    if File.directory?(filepath)
      next
    elsif is_yaml_file? filepath
      data[:cases] << read_included_yaml_file(filepath)
    elsif is_json_file? filepath
      data[:cases] << read_included_json_file(filepath)
    elsif File.file? filepath
      warn "[WARN] Ignore config file <#{filepath}>. No yaml or json extension!"
      next
    end
  }
end

.read_included_json_file(filepath) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/teuton/utils/config_file_reader.rb', line 122

def self.read_included_json_file(filepath)
  begin
    data = JSON.parse(File.read(filepath), symbolize_names: true)
  rescue => e
    warn "[ERROR] ConfigFileReader.read_included_json: #{e}"
    warn "[ERROR] Loading configuration file! <#{filename}>"
    exit 1
  end
  data[:tt_source_file] = relative_path(filepath)
  convert_string_keys_to_symbol(data)
end

.read_included_yaml_file(filepath) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/teuton/utils/config_file_reader.rb', line 110

def self.read_included_yaml_file(filepath)
  begin
    data = YAML.load(File.open(filepath))
  rescue => e
    warn "[ERROR] ConfigFileReader.read_included_yaml: #{e}"
    warn "[ERROR] Loading configuration file! <#{filename}>"
    exit 1
  end
  data[:tt_source_file] = relative_path(filepath)
  convert_string_keys_to_symbol(data)
end

.read_json(filepath) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/teuton/utils/config_file_reader.rb', line 76

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/teuton/utils/config_file_reader.rb', line 60

def self.read_yaml(filepath)
  begin
    data = YAML.load(File.open(filepath))
  rescue => e
    warn "[ERROR] ConfigFileReader.read_yaml: #{e}"
    warn "[ERROR] Revise file content! <#{filepath}>"
    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

.relative_path(filepath) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/teuton/utils/config_file_reader.rb', line 134

def self.relative_path(filepath)
  if filepath.start_with?(Dir.pwd)
    filepath[Dir.pwd.length + 1, filepath.length]
  else
    filepath
  end
end