Method: Inspec::Resources::CsvConfig#parse
- Defined in:
- lib/resources/csv.rb
#parse(content) ⇒ Object
override the parse method from JsonConfig Assuming a header row of name,col1,col2, it will output an array of hashes like so:
[
{ 'name' => 'row1', 'col1' => 'value1', 'col2' => 'value2' },
{ 'name' => 'row2', 'col1' => 'value3', 'col2' => 'value4' }
]
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/resources/csv.rb', line 22 def parse(content) require 'csv' # convert empty field to nil CSV::Converters[:blank_to_nil] = lambda do |field| field && field.empty? ? nil : field end # implicit conversion of values csv = CSV.new(content, headers: true, converters: [:all, :blank_to_nil]) # convert to hash csv.to_a.map(&:to_hash) rescue => e raise Inspec::Exceptions::ResourceFailed, "Unable to parse CSV: #{e.message}" end |