lib/eco/api/common/people/default_parsers/csv_parser.rb in eco-helpers-1.5.1 vs lib/eco/api/common/people/default_parsers/csv_parser.rb in eco-helpers-1.5.2
- old
+ new
@@ -1,41 +1,26 @@
-module Eco
- module API
- module Common
- module People
- class DefaultParsers
- class CSVParser < People::BaseParser
+class Eco::API::Common::People::DefaultParsers::CSVParser < Eco::API::Common::Loaders::Parser
+ attribute :csv
- def process
- @parsers.define_attribute(:csv, dependencies: @options) do |parser|
- parser.def_parser do |data, deps|
- arr_hash = []
- CSV.parse(data, headers: true, skip_blanks: true).reject do |row|
- values = row.to_hash.values
- values.all?(&:nil?) || values.map(&:to_s).all?(&:empty?)
- end.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
+ 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
- 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
+
end