Module: Ensure::Encoding::String

Included in:
String
Defined in:
lib/ensure/encoding.rb

Instance Method Summary collapse

Instance Method Details

#ensure_encoding(target_encoding, options = {}) ⇒ Object

Ensures the character encoding in a string. It employs a number of techniques to detect and transcode characters to make sure they end up in a usuable form in the encoding you need.

Arguments

target_encoding

The character encoding you want to ensure; this is usually the
internal encoding of your application. Accepts both string
constants and encoding constants. (ie. 'UTF-8' or Encoding::UTF_8)

options

Options to trigger activate certain algorithms.

Options

:external_encoding

Specifies both your certainty about the external encoding and what
you think it might be. Valid options are :sniff, an array of
encodings, or a single encoding. When you specify :sniff, we will
sniff around in the data to guess which encoding it is. When you
supply a list of possible encodings we will check them from begin
to end if one of them matches the data. Finally, when you specify
a specific encoding we assume you know which it is and we will use
that. By default we use :external_encoding => [target_encoding,
self.encoding].

:invalid_characters

Specifies what to do with invalid characters. There are three valid
values: :raise, :drop, and :transcode. The first raises and exception
on an invalid character. The second will strip all invalid characters
the last will try to transcode them to the wanted encoding. By default
we transcode.

Example

response = REST.get('http://www.google.com')

if match = /charset=([^;]*)/.match(response.content_type)
  encoding = match[1]
else
  encoding = 'UTF-8'
end

body = response.body.ensure_encoding('UTF-8',
  :external_encoding => encoding,
  :invalid_characters => :drop)


117
118
119
# File 'lib/ensure/encoding.rb', line 117

def ensure_encoding(target_encoding, options={})
  Ensure::Encoding.force_encoding(self, target_encoding, options)
end

#ensure_encoding!(target_encoding, options = {}) ⇒ Object

Performs just like String#ensure_encoding, only it changes the string in place instead of returning it.



123
124
125
# File 'lib/ensure/encoding.rb', line 123

def ensure_encoding!(target_encoding, options={})
  Ensure::Encoding.force_encoding!(self, target_encoding, options)
end