Module: RFacter::Util::Normalization Private

Defined in:
lib/rfacter/util/normalization.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.

Routines for normalizing fact return values

Since:

  • 0.1.0

Defined Under Namespace

Classes: NormalizationError

Constant Summary collapse

VALID_TYPES =

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

Since:

  • 0.1.0

[Integer, Float, TrueClass, FalseClass, NilClass, String, Array, Hash]

Class Method Summary collapse

Class Method Details

.normalize(value) ⇒ void

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.

This method returns an undefined value.

Recursively normalize the given data structure

Raises:

Since:

  • 0.1.0



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rfacter/util/normalization.rb', line 20

def normalize(value)
  case value
  when Integer, Float, TrueClass, FalseClass, NilClass
    value
  when String
    normalize_string(value)
  when Array
    normalize_array(value)
  when Hash
    normalize_hash(value)
  else
    raise NormalizationError, "Expected #{value} to be one of #{VALID_TYPES.inspect}, but was #{value.class}"
  end
end

.normalize_array(value) ⇒ void

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.

This method returns an undefined value.

Validate all elements of the array.

Parameters:

  • value (Array)

Raises:

Since:

  • 0.1.0



63
64
65
66
67
# File 'lib/rfacter/util/normalization.rb', line 63

def normalize_array(value)
  value.collect do |elem|
    normalize(elem)
  end
end

.normalize_hash(value) ⇒ void

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.

This method returns an undefined value.

Validate all keys and values of the hash.

Parameters:

  • value (Hash)

Raises:

Since:

  • 0.1.0



74
75
76
# File 'lib/rfacter/util/normalization.rb', line 74

def normalize_hash(value)
  Hash[value.collect { |k, v| [ normalize(k), normalize(v) ] } ]
end

.normalize_string(value) ⇒ void

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.

This method returns an undefined value.

Attempt to normalize and validate the given string.

The string is validate by checking that the string encoding is UTF-8 and that the string content matches the encoding. If the string is not an expected encoding then it is converted to UTF-8.

Parameters:

  • value (String)

Raises:

  • (NormalizationError)

    If the string used an unsupported encoding or did not match its encoding

Since:

  • 0.1.0



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rfacter/util/normalization.rb', line 46

def normalize_string(value)
  value = value.encode(Encoding::UTF_8)

  unless value.valid_encoding?
    raise NormalizationError, "String #{value.inspect} doesn't match the reported encoding #{value.encoding}"
  end

  value
rescue EncodingError
  raise NormalizationError, "String encoding #{value.encoding} is not UTF-8 and could not be converted to UTF-8"
end