Sha256: 72c1627002585113a99fe3f1d7c55176231193c5f5043b9bd51d0b65aad16e05

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-3.0.20 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-3.0.19 lib/eco/api/common/people/default_parsers/csv_parser.rb