Module: VanillaUtilityReader::VanillaFileReader::ConfigReader

Defined in:
lib/vanilla_utility_reader/file_utility/config_reader.rb

Instance Method Summary collapse

Instance Method Details

#configuration_readerObject

configuration_reader

It reads the config file from the project defined under configuration directory

$test = VanillaUtilityReader::VanillaFileReader::ConfigReader $test.configuration_reader config file has to be defined defined as config.yml or config.json or config.csv



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
# File 'lib/vanilla_utility_reader/file_utility/config_reader.rb', line 9

def configuration_reader
  begin

    get_dir_path = Dir.pwd
    dir_path = File.expand_path("configuration/", get_dir_path)

    config_set = Dir.entries(dir_path)

    if config_set.any? { |s| s.include?('config') }
      if (config_set.index('config.json') != nil)
        file_path = dir_path+"/config.json"
        begin
          json = File.read(file_path)
          $config = JSON.parse(json)
          $config
        rescue Exception => e
          raise "Json file is not in defined format #{file_path} \n error : #{e.message}"
        end
        $config
      elsif(config_set.index('config.yml') != nil)
        file_path = dir_path+"/config.yml"
        begin
          $config = YAML::load(File.open file_path)
          $config
        rescue Exception => e
          raise "yml file is not in defined format #{file_path} \n error : #{e.message}"
        end
        $config
      elsif(config_set.index('config.csv') != nil)
        file_path = dir_path+"/config.csv"
        begin
          $config =  {}
          CSV.foreach(file_path) do |row|
            row = row[0].split('=')
            $config[row[0]] = row[1]
          end
          $config
        rescue Exception => e
          raise "csv file is not in defined format #{file_path} \n error : #{e.message}"
        end
        $config
      end
    end
  rescue Exception=>e
    dir_path = Dir.pwd + "/configuration"
    raise "path :: #{dir_path} \n Configuration Directory Not Exist Error #{e.message}"
  end
end