Module: LegacyFacter::Util::Normalization Private
- Defined in:
- lib/facter/custom_facts/util/normalization.rb
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.
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.
[Integer, Float, TrueClass, FalseClass, NilClass, Symbol, String, Array, Hash, Date, Time].freeze
Class Method Summary collapse
-
.normalize(value) ⇒ void
Recursively normalize the given data structure.
-
.normalize_array(value) ⇒ void
Validate all elements of the array.
-
.normalize_hash(value) ⇒ void
Validate all keys and values of the hash.
-
.normalize_string(value) ⇒ void
Attempt to normalize and validate the given string.
Class Method Details
.normalize(value) ⇒ void
This method returns an undefined value.
Recursively normalize the given data structure
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/facter/custom_facts/util/normalization.rb', line 20 def normalize(value) case value when Integer, Float, TrueClass, FalseClass, NilClass, Symbol value when Date, Time value.iso8601 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 returns an undefined value.
Validate all elements of the array.
72 73 74 75 76 |
# File 'lib/facter/custom_facts/util/normalization.rb', line 72 def normalize_array(value) value.collect do |elem| normalize(elem) end end |
.normalize_hash(value) ⇒ void
This method returns an undefined value.
Validate all keys and values of the hash.
84 85 86 |
# File 'lib/facter/custom_facts/util/normalization.rb', line 84 def normalize_hash(value) Hash[value.collect { |k, v| [normalize(k), normalize(v)] }] end |
.normalize_string(value) ⇒ void
This method returns an undefined value.
Attempt to normalize and validate the given string.
The string is validated 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/facter/custom_facts/util/normalization.rb', line 50 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 if value.codepoints.include?(0) raise NormalizationError, "String #{value.inspect} contains a null byte reference; unsupported for all facts." end value rescue EncodingError raise NormalizationError, "String encoding #{value.encoding} is not UTF-8 and could not be converted to UTF-8" end |