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

Inherits:
Loaders::Parser show all
Includes:
Helpers::ExpectedHeaders, Helpers::NullParsing
Defined in:
lib/eco/api/common/people/default_parsers/csv_parser.rb

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Methods inherited from Loaders::Parser

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

Methods inherited from Loaders::CaseBase

#name, name_only_once!

Methods inherited from Loaders::Base

<=>, created_at, #initialize, set_created_at!

Methods included from ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/eco/api/common/people/default_parsers/csv_parser.rb', line 7

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

#serializer(array_hash, _deps) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eco/api/common/people/default_parsers/csv_parser.rb', line 29

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