Class: FileReader

Inherits:
Object show all
Defined in:
lib/file_reader.rb

Class Method Summary collapse

Class Method Details

.read(file) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/file_reader.rb', line 2

def self.read(file)
  str = File.read(file)

  # Try it as UTF-8 directly
  cleaned = str.dup.force_encoding('UTF-8')
  unless cleaned.valid_encoding?
    # Some of it might be old Windows code page
    cleaned = str.encode( 'UTF-8', 'Windows-1252' )
  end
  str = cleaned
rescue EncodingError
  # Force it to UTF-8, throwing out invalid bits
  str.encode!('UTF-8', invalid: :replace, undef: :replace)
end