Module: Eco::Data::Files::Content
- Includes:
- Encoding, Language::AuxiliarLogger
- Included in:
- InstanceMethods
- Defined in:
- lib/eco/data/files/content.rb
Constant Summary
Constants included from Encoding
Instance Attribute Summary
Attributes included from Language::AuxiliarLogger
Instance Method Summary collapse
-
#get_file_content(file, encoding: nil, 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 Encoding
#bom?, #encoding, #get_file_content_with_encoding, #remove_bom, #scoped_encoding
Methods included from Language::AuxiliarLogger
Instance Method Details
#get_file_content(file, encoding: nil, tolerance: 5) ⇒ String
It offers a resilient way to read content from a file
11 12 13 |
# File 'lib/eco/data/files/content.rb', line 11 def get_file_content(file, encoding: nil, tolerance: 5) read_with_tolerance(file, encoding: encoding, tolerance: tolerance) end |
#read_with_tolerance(file, encoding:, tolerance: 5) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/eco/data/files/content.rb', line 15 def read_with_tolerance(file, encoding:, tolerance: 5) content = get_file_content_with_encoding(file, encoding: encoding) return unless content content.scrub do |bytes| replacement = "<#{bytes.unpack1('H*')}>" if tolerance <= 0 log(:error) { "There were more than 5 encoding errors in the file '#{file}'." } return content else tolerance -= 1 log(:error) { "Encoding problem in file '#{file}': '#{replacement}'." } replacement end end end |