lib/eco/api/common/people/entry_factory.rb in eco-helpers-2.4.4 vs lib/eco/api/common/people/entry_factory.rb in eco-helpers-2.4.5

- old
+ new

@@ -7,10 +7,11 @@ # => This way, Entries and PersonEntry will be able to refer to attr_map and person_parser linked to schema_id # => "schema_id" should be an optional column in the input file, or parsable via a custom parser to scope the schema # Helper factory class to generate entries (input entries). # @attr_reader schema [Ecoportal::API::V1::PersonSchema] person schema to be used in this entry factory class EntryFactory < Eco::API::Common::Session::BaseSession + include Eco::Data::Files attr_reader :schema, :person_parser # @param e [Eco::API::Common::Session::Environment] requires a session environment, as any child of `Eco::API::Common::Session::BaseSession` # @param schema [Ecoportal::API::V1::PersonSchema] schema of person details that the parser will be based upon. @@ -109,11 +110,14 @@ 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 + if (format != :xls) && file + content = get_file_content(file, encoding) + end + #content = get_file_content(file, format, encoding) if (format != :xls) && file case content when Hash logger.error("Input data as 'Hash' not supported. Expecting 'Enumerable' or 'String'") exit(1) @@ -140,14 +144,12 @@ 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 # - if you try to provide `data:` in the wrong format. # - if you `file:` is empty. # - if the `format:` you provide is not a `Symbol`. @@ -162,11 +164,11 @@ fatal("A file should be specified.") unless !file.to_s.strip.empty? fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol) fatal("There is no parser/serializer for format ':#{format.to_s}'") unless @person_parser.defined?(format) run = true - if Eco::API::Common::Session::FileManager.file_exists?(file) + if self.class.file_exists?(file) prompt_user("Do you want to overwrite it? (Y/n):", explanation: "The file '#{file}' already exists.", default: "Y") do |response| run = (response == "") || response.upcase.start_with?("Y") end end @@ -181,43 +183,12 @@ File.open(file, "w", enconding: encoding) do |fd| fd.write(person_parser.serialize(format, data_entries)) end end - end private - - def get_file_content(file, format, encoding) - unless Eco::API::Common::Session::FileManager.file_exists?(file) - logger.error("File does not exist: #{file}") - exit(1) - end - #ext = File.extname(file) - encoding ||= Eco::API::Common::Session::FileManager.encoding(file) - encoding = (encoding == "bom") ? "#{encoding}|utf-8": encoding - puts "File encoding: '#{encoding}'" unless !encoding || encoding == 'utf-8' - read_with_tolerance(file, encoding: encoding) - end - - def read_with_tolerance(file, encoding:) - if content = File.read(file, encoding: encoding) - content = content.encode("utf-8") unless encoding.include?('utf-8') - tolerance = 5 - content.scrub do |bytes| - replacement = '<' + bytes.unpack('H*')[0] + '>' - if tolerance <= 0 - logger.error("There were more than 5 encoding errors in the file '#{file}'.") - return content - else - tolerance -= 1 - logger.error("Encoding problem in file '#{file}': '#{replacement}'.") - replacement - end - end - end - end def fatal(msg) logger.fatal(msg) raise msg end