Sha256: c650a4dce08680cb2b906e39b529214c68a6a8159eb3cb625b395e61842cfac3

Contents?: true

Size: 1020 Bytes

Versions: 27

Compression:

Stored size: 1020 Bytes

Contents

class Eco::API::Common::People::DefaultParsers::CSVParser < Eco::API::Common::Loaders::Parser
  attribute :csv

  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?
        hash[attr.strip] = parse_string(row[attr])
      end
      arr_hash.push(row_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 parse_string(value)
    return nil if value.to_s.empty?
    return nil if null?(value)
    value
  end

  def null?(value)
    return true if !value
    str = value.strip.upcase
    ["NULL"].any? {|token| str == token}
  end

end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
eco-helpers-2.0.25 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.24 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.23 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.22 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.21 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.19 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.18 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.17 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.16 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.15 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.14 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.13 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.12 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.11 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.10 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.9 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.8 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.7 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.6 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-2.0.5 lib/eco/api/common/people/default_parsers/csv_parser.rb