Class: Zarby::Normalize

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
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)


13
14
15
# File 'lib/zarby/normalize.rb', line 13

def initialize(input:)
  @input = input&.force_encoding(Encoding::UTF_8) || ''
end

Class Method Details

.utf8(input) ⇒ String

Parameters:

  • input (String)

Returns:

  • (String)


41
42
43
# File 'lib/zarby/normalize.rb', line 41

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

Instance Method Details

#callString

Returns:

  • (String)


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

def call
  output = @input if valid?


  output ||= @input.force_encoding(Encoding::ISO_8859_1).encode!(Encoding::UTF_8)
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
  nil
end

#utf8String

Returns:

  • (String)


28
29
30
31
32
33
34
35
# File 'lib/zarby/normalize.rb', line 28

def utf8
  output = @input if valid?


  output ||= @input.force_encoding(Encoding::ISO_8859_1).encode!(Encoding::UTF_8)
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
  nil
end