Class: At_email::Config::Config_File

Inherits:
Object
  • Object
show all
Defined in:
lib/at_email/config/config_file.rb

Instance Method Summary collapse

Instance Method Details

#get_configObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/at_email/config/config_file.rb', line 6

def get_config()

  if File.exist?($options.config_file)
    contents = File.read($options.config_file)
    config = JSON.parse(contents, symbolize_names: true)
  else
    raise "Config file does not exist: " + $options.config_file
  end

  default_config_file_path = $app_path + '/' + At_email::DEFAULT_CONFIG_FILE
  if File.exist?(default_config_file_path)
    default_config_contents = File.read(default_config_file_path)
    default_config = JSON.parse(default_config_contents, symbolize_names: true)
  else
    raise "Default config file does not exist: " + default_config_file_path
  end

  config.each do |key, value|
      if !default_config[key]
        raise 'Config Error: Property: ' + key.to_s + ' is not a valid configuration option for this system'
      end
  end

  default_config.each do |key, data|
      if !config[key] && data[:value]
        config[key] = data[:value]
      end
  end

  default_config.each do |key, data|
    if config[key]
      if data[:type]
        if data[:type] === 'boolean'
          if ![true, false].include? config[key]
            raise 'Config Error: Property: ' + key.to_s + ' is not type: boolean - config value: ' + config[key].to_s
          end
        end
        if data[:type] === 'numeric'
          if !config[key].is_a? Numeric
            raise 'Config Error: Property: ' + key.to_s + ' is not type: numeric - config value: ' + config[key].to_s
          end
          if data[:min_value]
            if config[key] < data[:min_value]
              raise 'Config Error: Property: ' + key.to_s + ' is below minimum value: ' + data[:min_value].to_s + ' - config value: ' + config[key].to_s
            end
          end
          if data[:max_value]
            if config[key] > data[:max_value]
              raise 'Config Error: Property: ' + key.to_s + ' is above maximum value: ' + data[:max_value].to_s + ' - config value: ' + config[key].to_s
            end
          end
        end
        if data[:type] === 'string'
          if !config[key].is_a? String
            raise 'Config Error: Property: ' + key.to_s + ' is not type: string - value: ' + config[key].to_s
          end
          if data[:min_length]
            if config[key].length < data[:min_length]
              raise 'Config Error: Property: ' + key.to_s + ' is below minimum lenth: ' + data[:min_length].to_s + ' - config value: ' + config[key].to_s
            end
          end
          if data[:max_length]
            if config[key].length > data[:max_length]
              raise 'Config Error: Property: ' + key.to_s + ' is above minimum lenth: ' + data[:max_length].to_s + ' - config value: ' + config[key].to_s
            end
          end
        end
        if data[:type] === 'path'
          if data[:path_type]
            if data[:path_type] === 'dir'
              if !Dir.exists?(config[key].to_s)
                raise 'Config Error: Property: ' + key.to_s + ' is not a directory path - config value: ' + config[key].to_s
              end
            end
            if data[:path_type] === 'file'
              if !File.exists?(config[key].to_s)
                raise 'Config Error: Property: ' + key.to_s + ' is not a file path - config value: ' + config[key].to_s
              end
            end
          end
        end
      end
    end
  end

  output = {}
  config.sort_by { |k, v| k }.each do |key, value|
    output[key] = value
  end

  return output

end