Class: Eco::API::Common::People::DefaultParsers::CSVParser

Inherits:
Loaders::Parser show all
Defined in:
lib/eco/api/common/people/default_parsers/csv_parser.rb

Instance Method Summary collapse

Methods inherited from Loaders::Parser

active_when_all, active_when_any, #attribute, attribute, data_keys, dependencies, #initialize, parsing_phase, #seralizer, serializing_phase

Methods inherited from BaseLoader

<=>, created_at, #initialize, #name, name_only_once!, set_created_at!

Methods included from ClassHelpers

#class_resolver, #descendants, #descendants?, #new_class, #resolve_class, #to_constant

Constructor Details

This class inherits a constructor from Eco::API::Common::Loaders::Parser

Instance Method Details

#parser(data, deps) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/eco/api/common/people/default_parsers/csv_parser.rb', line 4

def parser(data, deps)
  Eco::CSV.parse(data, headers: true, skip_blanks: true).each_with_object([]) do |row, arr_hash|
    row_hash = row.headers.uniq.each_with_object({}) do |attr, hash|
      next if attr.to_s.strip.empty?
      value            = row[attr]
      hash[attr.strip] = value.to_s.empty?? nil : value
    end
    arr_hash.push(row_hash)
  end
end

#serializer(array_hash, deps) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/eco/api/common/people/default_parsers/csv_parser.rb', line 15

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