Class: Zarby::Normalize

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

Overview

this class is used to normalize the input string to UTF-8

Constant Summary collapse

COMMON_ENCODINGS =

utf-8 converting from the string's given encoding

%w[UTF-8 Windows-1252 ASCII-8BIT ISO-8859-1 US-ASCII].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:) ⇒ String

Parameters:

  • input (String)


11
12
13
14
15
16
17
# File 'lib/zarby/normalize.rb', line 11

def initialize(input:)
  unless input.is_a?(String)
    raise Zarby::ArgumentZarby, "input to normalise utf8 must be of type String, #{input.class} given"
  end

  @input = input&.force_encoding(Encoding::UTF_8) || ""
end

Class Method Details

.utf8(input) ⇒ String

Parameters:

  • input (String)

Returns:

  • (String)


29
30
31
# File 'lib/zarby/normalize.rb', line 29

def utf8(input)
  new(input: input).utf8
end

Instance Method Details

#callString

Returns:

  • (String)


20
21
22
23
24
# File 'lib/zarby/normalize.rb', line 20

def call
  return @input if valid?

  @input.dup.force_encoding(Encoding::ISO_8859_1).encode!(Encoding::UTF_8)
end