Class: EncodingManager

Inherits:
Object
  • Object
show all
Defined in:
lib/encoding_manager.rb

Overview

Class to manage changes in text encoding.

Class Method Summary collapse

Class Method Details

.fix_encoding(text) ⇒ Object

Fix problems with encoding in a string. If the string is valid UTF-8, force it to UTF-8 encoding. Otherwise, convert from ISO-8859-1 to UTF-8 if necessary.

Receives as argument a string.


13
14
15
16
17
18
# File 'lib/encoding_manager.rb', line 13

def self.fix_encoding(text)
  fixed_text = text
  fixed_text = fix_utf8 fixed_text
  fixed_text = convert_to_utf8 fixed_text
  return fixed_text
end

.set_encoding(text, encoding) ⇒ Object

Check if the passed text is valid for its current encoding. If not, the passed encoding is forced. This is intended for the case in which Feedjira is passed a non-utf8 string but the outputs are in utf-8.


25
26
27
28
29
30
31
32
33
# File 'lib/encoding_manager.rb', line 25

def self.set_encoding(text, encoding)
  text_encoded = text
  unless text_encoded.nil?
    unless text_encoded.valid_encoding?
      text_encoded.force_encoding encoding
    end
  end
  return text_encoded
end