Class: AbideDevUtils::Files::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/files.rb

Class Method Summary collapse

Class Method Details

.read(path, raw: false, safe: true, opts: {}) ⇒ Object



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
# File 'lib/abide_dev_utils/files.rb', line 8

def self.read(path, raw: false, safe: true, opts: {})
  AbideDevUtils::Validate.file(path)
  return File.read(path) if raw

  extension = File.extname(path)
  case extension
  when /\.yaml|\.yml/
    require 'yaml'
    if safe
      YAML.safe_load(File.read(path))
    else
      YAML.load_file(path)
    end
  when '.json'
    require 'json'
    return JSON.parse(File.read(path), opts) if safe

    JSON.parse!(File.read(path), opts)
  when '.xml'
    require 'nokogiri'
    File.open(path, 'r') do |file|
      Nokogiri::XML.parse(file) do |config|
        config.strict.noblanks.norecover
      end
    end
  else
    File.read(path)
  end
end