Class: InjalidDejice::Encoder

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

Overview

Encodes a UTF-8 string to the KOI-7 encoding.

Constant Summary collapse

OUTPUT_ENCODING =
"US-ASCII"

Instance Method Summary collapse

Constructor Details

#initialize(forced_latin: [], unknown_char_rep: DEF_UNKNOWN_CHAR_REP) ⇒ Encoder

Returns a new instance of Encoder.



10
11
12
13
# File 'lib/injalid_dejice/encoder.rb', line 10

def initialize(forced_latin: [], unknown_char_rep: DEF_UNKNOWN_CHAR_REP)
  @forced_latin = forced_latin
  @unknown_char_rep = unknown_char_rep
end

Instance Method Details

#call(input_string) ⇒ String

Encode ‘input_string’ to the KOI-7 encoding.

Parameters:

  • input_string (String)

    A UTF-8 string.

Returns:

  • (String)

    An ASCII string, KOI-7-compatible.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/injalid_dejice/encoder.rb', line 24

def call(input_string)
  non_latin_indexes = LocaleResolver.new(input_string, @forced_latin).call

  result = if non_latin_indexes.empty?
             # No non-Latin characters were found, so no futher action needed.
             input_string.dup
           else
             _insert_shift_chars(
               _handle_replacements(input_string),
               non_latin_indexes
             )
           end

  result.force_encoding(OUTPUT_ENCODING)
end