Class: Mapi::Helper
- Inherits:
-
Object
- Object
- Mapi::Helper
- Defined in:
- lib/mapi/helper.rb
Overview
Instance Attribute Summary collapse
-
#ansi_encoding ⇒ String?
readonly
Encoding name of ANSI string we assume.
-
#to_unicode ⇒ Boolean
readonly
Convert all ANSI string to UTF-8.
Instance Method Summary collapse
-
#convert_ansi_str(str) ⇒ Object
Convert ‘ASCII_8BIT` string.
-
#initialize(ansi_encoding = nil, to_unicode = false) ⇒ Helper
constructor
A new instance of Helper.
Constructor Details
#initialize(ansi_encoding = nil, to_unicode = false) ⇒ Helper
Returns a new instance of Helper.
12 13 14 15 |
# File 'lib/mapi/helper.rb', line 12 def initialize ansi_encoding=nil, to_unicode=false @ansi_encoding = ansi_encoding || "BINARY" @to_unicode = to_unicode end |
Instance Attribute Details
#ansi_encoding ⇒ String? (readonly)
Returns Encoding name of ANSI string we assume.
5 6 7 |
# File 'lib/mapi/helper.rb', line 5 def ansi_encoding @ansi_encoding end |
#to_unicode ⇒ Boolean (readonly)
Returns Convert all ANSI string to UTF-8.
8 9 10 |
# File 'lib/mapi/helper.rb', line 8 def to_unicode @to_unicode end |
Instance Method Details
#convert_ansi_str(str) ⇒ Object
Convert ‘ASCII_8BIT` string. Maybe produce UTF_8 string, or arbitrary object
Use cases:
-
Decode PT_STRING8 in Pst
-
Decode ‘0x001e` in Msg
-
Decode body (rtf, text) in PropertySet
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mapi/helper.rb', line 27 def convert_ansi_str str return nil unless str if @ansi_encoding if @to_unicode # assume we can convert this text to UTF-8 begin str.force_encoding(@ansi_encoding).encode("UTF-8") rescue Encoding::UndefinedConversionError => ex # some text are already UTF-8 due to unknown reason str.force_encoding("UTF-8").encode("UTF-8") end else str.force_encoding(@ansi_encoding) end else str end end |