Module: Digest::UUID

Defined in:
lib/more_core_extensions/core_ext/digest/uuid.rb

Constant Summary collapse

UUID_REGEX_FORMAT =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.freeze

Class Method Summary collapse

Class Method Details

.clean(guid) ⇒ String

Takes a UUID string of varying formats and cleans it. It will strip invalid characters, such as leading and trailing brackets as well as whitespace. The result is a lowercased, canonical UUID string.

If the guid argument is nil or blank, then nil is returned. If the guid is already clean, then no additional cleaning occurs, and it is returned as-is.

Parameters:

  • guid (String)

    A string that should more or less represent a UUID.

Returns:

  • (String)

    A lowercase v4 UUID string stripped of any extraneous characters.



17
18
19
20
21
22
23
24
# File 'lib/more_core_extensions/core_ext/digest/uuid.rb', line 17

def self.clean(guid)
  return nil if guid.nil?
  g = guid.to_s.downcase
  return nil if g.strip.empty?
  return g if g.length == 36 && g =~ UUID_REGEX_FORMAT
  g.delete!('^0-9a-f')
  g.sub!(/^([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})$/, '\1-\2-\3-\4-\5')
end