lib/eco/api/common/people/entry_factory.rb in eco-helpers-2.0.21 vs lib/eco/api/common/people/entry_factory.rb in eco-helpers-2.0.22
- old
+ new
@@ -99,36 +99,50 @@
def to_array_of_hashes(**kargs)
data = []
content, file, encoding, format = kargs.values_at(:content, :file, :encoding, :format)
- content = get_file_content(file, format, encoding) if file
+ # Support for multiple file
+ if file.is_a?(Array)
+ return file.each_with_object([]) do |f, out|
+ logger.info("Parsing file '#{f}'")
+ curr = to_array_of_hashes(**kargs.merge(file: f))
+ out.concat(curr)
+ end
+ end
+ # Get content only when it's not :xls
+ # note: even if content was provided, file takes precedence
+ content = get_file_content(file, format, encoding) if (format != :xls) && file
case content
- when !content
- logger.error("Could not obtain any data out of these: #{kargs}")
- exit(1)
when Hash
logger.error("Input data as 'Hash' not supported. Expecting 'Enumerable' or 'String'")
exit(1)
when String
- data = person_parser.parse(format, content).map.each_with_index do |entry_hash, i|
- j = (format == :csv)? i + 2 : i + 1
- entry_hash.tap {|hash| hash["idx"] = j}
- end
- to_array_of_hashes(content: data)
+ to_array_of_hashes(content: person_parser.parse(format, content))
when Enumerable
sample = content.to_a.first
case sample
when Hash, Array, ::CSV::Row
Eco::CSV::Table.new(content).to_array_of_hashes
else
- logger.error("Input 'Array' of '#{sample.class}' is not supported.")
+ logger.error("Input content 'Array' of '#{sample.class}' is not supported.")
end
else
- logger.error("Could not obtain any data out of content: '#{content.class}'")
- exit(1)
+ if file && format == :xls
+ person_parser.parse(format, file)
+ else
+ logger.error("Could not obtain any data out of these: #{kargs}. Given content: '#{content.class}'")
+ exit(1)
+ end
+ end.tap do |out_array|
+ start_from_two = (format == :csv) || format == :xls
+ out_array.each_with_index do |entry_hash, i|
+ entry_hash["idx"] = start_from_two ? i + 2 : i + 1
+ entry_hash["source_file"] = file
+ end
end
+
end
# Helper that generates a file out of `data:`.
# @raise Exception