class Eco::API::Common::People::DefaultParsers::CSVParser < Eco::API::Common::Loaders::Parser attribute :csv include Eco::API::Common::People::DefaultParsers::Helpers::ExpectedHeaders include Eco::API::Common::People::DefaultParsers::Helpers::NullParsing def parser(data, deps) Eco::CSV.parse(data, headers: true, skip_blanks: true).tap do |table| require_headers!(table.headers) next unless deps[:check_headers] next unless check_headers? check_headers!( table.headers, order_check: options.dig(:input, :header_check, :order) ) end.each_with_object([]) do |item, arr_hash| item_hash = item.headers.uniq.each_with_object({}) do |attr, hash| next if attr.to_s.strip.empty? hash[attr.strip] = parse_null(item[attr]) end arr_hash.push(item_hash) end end def serializer(array_hash, _deps) arr_rows = [] unless array_hash.empty? header = array_hash.first.keys arr_rows = array_hash.map do |csv_row| CSV::Row.new(header, csv_row.values_at(*header)) end end CSV::Table.new(arr_rows).to_csv end private def check_headers? !options.dig(:input, :header_check, :skip) end end