lib/eco/api/common/people/entry_factory.rb in eco-helpers-3.0.18 vs lib/eco/api/common/people/entry_factory.rb in eco-helpers-3.0.19

- old
+ new

@@ -26,10 +26,11 @@ # set of attribute, type and format parsers/serializers. # @param attr_map [nil, Eco::Data::Mapper] attribute names mapper # to translate external names into internal ones and _vice versa_. def initialize(e, schema:, person_parser: nil, default_parser: nil, attr_map: nil) super(e) + msg = "Constructor needs a PersonSchema. Given: #{schema.class}" fatal msg unless schema.is_a?(Ecoportal::API::V1::PersonSchema) msg = "Expecting PersonParser. Given: #{person_parser.class}" fatal msg if person_parser && !person_parser.is_a?(Eco::API::Common::People::PersonParser) @@ -131,13 +132,14 @@ log(: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 + + # Get content only when it's not :xls, nor :json # note: even if content was provided, file takes precedence - if (format != :xls) && file # rubocop:disable Style/IfUnlessModifier + if get_content?(format) && file # rubocop:disable Style/IfUnlessModifier content = get_file_content(file, encoding: encoding) end case content when Hash @@ -164,12 +166,14 @@ else abort("Could not obtain any data out of these: #{kargs}. Given content: '#{content.class}'") 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 + first_idx = start_from_two ? 2 : 1 + + out_array.each.with_index(first_idx) do |entry_hash, idx| + entry_hash["idx"] = idx entry_hash["source_file"] = file end end end @@ -219,9 +223,16 @@ fd.write(person_parser.serialize(format, data_entries)) end end private + + def get_content?(format) + return false if format == :xls + return false if format == :json + + true + end def abort(message) log(:error) { message } exit(1) end