module LegacyFacter::Util::Normalization

Constants

VALID_TYPES

Public Instance Methods

normalize(value) click to toggle source

Recursively normalize the given data structure

@api public @raise [NormalizationError] If the data structure contained an invalid element. @return [void]

# File lib/facter/custom_facts/util/normalization.rb, line 16
def normalize(value)
  case value
  when Integer, Float, TrueClass, FalseClass, NilClass, Symbol, Date
    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) click to toggle source

Validate all elements of the array.

@api public @raise [NormalizationError] If one of the elements failed validation @param value [Array] @return [void]

# File lib/facter/custom_facts/util/normalization.rb, line 77
def normalize_array(value)
  value.collect do |elem|
    normalize(elem)
  end
end
normalize_hash(value) click to toggle source

Validate all keys and values of the hash.

@api public @raise [NormalizationError] If one of the keys or values failed normalization @param value [Hash] @return [void]

# File lib/facter/custom_facts/util/normalization.rb, line 89
def normalize_hash(value)
  Hash[value.collect { |k, v| [normalize(k), normalize(v)] }]
end
normalize_string(value) click to toggle source
# File lib/facter/custom_facts/util/normalization.rb, line 51
def normalize_string(value)
  converted = Iconv.conv('UTF-8//IGNORE', 'UTF-8', value)
  raise NormalizationError, "String #{value.inspect} is not valid UTF-8" if converted != value

  value
end