Class: Waivers::CSVFileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/utils/waivers/csv_file_reader.rb

Class Method Summary collapse

Class Method Details

.fetch_data(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/inspec/utils/waivers/csv_file_reader.rb', line 12

def self.fetch_data(path)
  waiver_data_hash = {}
  CSV.foreach(path, headers: true) do |row|
    row_hash = row.to_hash
    @headers = row_hash.keys if @headers.empty?
    control_id = row_hash["control_id"]
    # delete keys and values not required in final hash
    row_hash.delete("control_id")
    row_hash.delete_if { |k, v| k.nil? || v.nil? }

    waiver_data_hash[control_id] = row_hash if control_id && !(row_hash.nil? || row_hash.empty?)
  end

  waiver_data_hash
rescue CSV::MalformedCSVError => e
  raise "Error reading InSpec waivers in CSV: #{e}"
end

.headersObject



30
31
32
# File 'lib/inspec/utils/waivers/csv_file_reader.rb', line 30

def self.headers
  @headers
end

.resolve(path) ⇒ Object



5
6
7
8
9
10
# File 'lib/inspec/utils/waivers/csv_file_reader.rb', line 5

def self.resolve(path)
  return nil unless File.file?(path)

  @headers ||= []
  fetch_data(path)
end