Class: Waivers::ExcelFileReader

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

Class Method Summary collapse

Class Method Details

.fetch_data(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/inspec/utils/waivers/excel_file_reader.rb', line 13

def self.fetch_data(path)
  waiver_data_hash = {}
  file_extension = File.extname(path) == ".xlsx" ? :xlsx : :xls
  excel_file = Roo::Spreadsheet.open(path, extension: file_extension)
  excel_file.sheet(0).parse(headers: true).each_with_index do |row, index|
    if index == 0
      @headers = row.keys
    else
      row_hash = row
      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? }
    end

    waiver_data_hash[control_id] = row_hash if control_id && !(row_hash.nil? || row_hash.empty?)
  end
  waiver_data_hash
rescue Exception => e
  raise "Error reading InSpec waivers in Excel: #{e}"
end

.headersObject



35
36
37
# File 'lib/inspec/utils/waivers/excel_file_reader.rb', line 35

def self.headers
  @headers
end

.resolve(path) ⇒ Object



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

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

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