Module: Thrift::UUID

Defined in:
lib/thrift/uuid.rb

Constant Summary collapse

UUID_REGEX =
/\A[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\z/.freeze

Class Method Summary collapse

Class Method Details

.uuid_bytes(uuid) ⇒ Object



36
37
38
# File 'lib/thrift/uuid.rb', line 36

def self.uuid_bytes(uuid)
  [uuid.delete('-')].pack('H*')
end

.uuid_from_bytes(bytes) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/thrift/uuid.rb', line 40

def self.uuid_from_bytes(bytes)
  unless bytes.bytesize == 16
    raise ProtocolException.new(ProtocolException::INVALID_DATA, 'Invalid UUID data length')
  end

  hex = bytes.unpack('H*').first
  "#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}"
end

.validate_uuid!(uuid) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/thrift/uuid.rb', line 26

def self.validate_uuid!(uuid)
  unless uuid.is_a?(String)
    raise ProtocolException.new(ProtocolException::INVALID_DATA, 'UUID must be a string')
  end

  unless uuid =~ UUID_REGEX
    raise ProtocolException.new(ProtocolException::INVALID_DATA, 'Invalid UUID format')
  end
end