Module: Cinch::Utilities::Encoding Private

Defined in:
lib/cinch/utilities/encoding.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

Class Method Summary collapse

Class Method Details

.encode_incoming(string, encoding) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cinch/utilities/encoding.rb', line 8

def self.encode_incoming(string, encoding)
  string = string.dup
  if encoding == :irc
    # If incoming text is valid UTF-8, it will be interpreted as
    # such. If it fails validation, a CP1252 -> UTF-8 conversion
    # is performed. This allows you to see non-ASCII from mIRC
    # users (non-UTF-8) and other users sending you UTF-8.
    #
    # (from http://xchat.org/encoding/#hybrid)
    string.force_encoding("UTF-8")
    unless string.valid_encoding?
      string.force_encoding("CP1252").encode!("UTF-8", invalid: :replace, undef: :replace)
    end
  else
    string.force_encoding(encoding).encode!(invalid: :replace, undef: :replace)
    string = string.chars.select(&:valid_encoding?).join
  end

  string
end

.encode_outgoing(string, encoding) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



29
30
31
32
33
34
# File 'lib/cinch/utilities/encoding.rb', line 29

def self.encode_outgoing(string, encoding)
  string = string.dup
  encoding = "UTF-8" if encoding == :irc

  string.encode!(encoding, invalid: :replace, undef: :replace).force_encoding("ASCII-8BIT")
end