Module: Eco::Data::Files::InstanceMethods
- Includes:
- Language::AuxiliarLogger
- Defined in:
- lib/eco/data/files/helpers.rb
Instance Method Summary collapse
-
#get_file_content(file, encoding, tolerance: 5) ⇒ String
It offers a resilient way to read content from a file.
- #read_with_tolerance(file, encoding:, tolerance: 5) ⇒ Object
Methods included from Language::AuxiliarLogger
Instance Method Details
#get_file_content(file, encoding, tolerance: 5) ⇒ String
It offers a resilient way to read content from a file
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/eco/data/files/helpers.rb', line 19 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 read_with_tolerance(file, encoding: encoding, tolerance: tolerance) end |
#read_with_tolerance(file, encoding:, tolerance: 5) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/eco/data/files/helpers.rb', line 34 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 end end end |