Sha256: c6eecf9b707123dc37b3bbeb86e9e92c28fd759353aef003eb4d72b20286e184

Contents?: true

Size: 1.23 KB

Versions: 14

Compression:

Stored size: 1.23 KB

Contents

module Eco
  module API
    module Common
      module People
        class DefaultParsers
          class CSVParser < People::BaseParser

            def process
              @parsers.define_attribute(:csv, dependencies: @options) do |parser|
                parser.def_parser  do |data, deps|
                  arr_hash = []
                  CSV.parse(data, headers: true).each do |row|
                    row_hash = row.headers.uniq.each_with_object({}) do |attr, hash|
                      value      = row[attr]
                      hash[attr] = value.to_s.empty?? nil : value
                    end
                    arr_hash.push(row_hash)
                  end
                  arr_hash
                end.def_serializer do |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
              end
            end

          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
eco-helpers-1.3.1 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.2.2 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.2.1 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.1.8 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.1.7 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.1.6 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.1.5 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.1.4 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.1.3 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.1.2 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.1.1 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.0.14 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.0.13 lib/eco/api/common/people/default_parsers/csv_parser.rb
eco-helpers-1.0.12 lib/eco/api/common/people/default_parsers/csv_parser.rb