lib/eco/data/files/helpers.rb in eco-helpers-2.4.9 vs lib/eco/data/files/helpers.rb in eco-helpers-2.5.1

- old
+ new

@@ -10,48 +10,38 @@ end end module InstanceMethods include Eco::Language::AuxiliarLogger + include Eco::Data::Files::Encoding # It offers a resilient way to read content from a file # @tolerance [Integer] the number of allowed encoding errors. # @return [String] the content of the file - def get_file_content(file, encoding, tolerance: 5) - unless self.class.file_exists?(file) - logger.error("File does not exist: #{file}") - exit(1) - end - encoding ||= self.class.encoding(file) - encoding = (encoding == "bom") ? "#{encoding}|utf-8": encoding - unless !encoding || encoding == 'utf-8' - msg = "File encoding: '#{encoding}'" - logger.debug(msg) - puts msg - end + def get_file_content(file, encoding: nil, tolerance: 5) read_with_tolerance(file, encoding: encoding, tolerance: tolerance) end def read_with_tolerance(file, encoding:, tolerance: 5) - if content = File.read(file, encoding: encoding) - content = content.encode("utf-8") unless encoding.include?('utf-8') - 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 + return nil unless content = get_file_content_with_encoding(file, encoding: encoding) + 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 module ClassMethods + include Eco::Data::Files::Encoding + def create_directory(path, includes_file: false) Directory.create(path, includes_file: includes_file) end def split(path) @@ -102,22 +92,10 @@ def file_empty?(path) return true if !File.file?(path) File.zero?(path) end - def has_bom?(path) - return false if !path || file_empty?(path) - File.open(path, "rb") do |f| - bytes = f.read(3) - return bytes.unpack("C*") == [239, 187, 191] - end - end - - def encoding(path) - has_bom?(path) ? "bom" : "utf-8" - end - def script_subfolder basename = File.basename($0, File.extname($0)) path = File.dirname($0) File.join(path, basename) end @@ -135,10 +113,10 @@ end.sort end def csv_files(folder = ".", regexp: nil, older_than: nil) folder_files(folder, "*.csv", regexp: regexp, older_than: older_than).sort - end + end end class << self include Files::ClassMethods end