Class: MixinBot::UUID

Inherits:
Object
  • Object
show all
Defined in:
lib/mixin_bot/uuid.rb

Overview

Utility class for handling Mixin Network UUID format conversions.

Mixin Network uses UUIDs extensively for identifying:

  • Users and bots

  • Assets

  • Transactions and traces

  • Conversations

Format Conversions

This class handles conversions between:

  • Standard UUID format: “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx” (36 chars)

  • Packed binary format: 16 bytes

  • Hex format without dashes: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx” (32 chars)

Usage

# From hex format to UUID
uuid = MixinBot::UUID.new(hex: '965e5c6e434c3fa9b780c50f43cd955c')
uuid.unpacked
# => "965e5c6e-434c-3fa9-b780-c50f43cd955c"

# From UUID to packed binary
uuid = MixinBot::UUID.new(hex: '965e5c6e-434c-3fa9-b780-c50f43cd955c')
uuid.packed
# => "\x96^\\nC<?\xA9\xB7\x80\xC5\x0FC\xCD\x95\\"

# From packed binary to UUID
uuid = MixinBot::UUID.new(raw: binary_data)
uuid.unpacked
# => "965e5c6e-434c-3fa9-b780-c50f43cd955c"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ UUID

Initializes a new UUID instance.

Provide either :hex or :raw parameter. The other format can be obtained via #packed or #unpacked methods.

Examples:

From hex

uuid = MixinBot::UUID.new(hex: '965e5c6e-434c-3fa9-b780-c50f43cd955c')

From raw binary

uuid = MixinBot::UUID.new(raw: binary_string)

Parameters:

  • args (Hash)

    initialization options

Options Hash (**args):

  • :hex (String)

    the UUID in hex format (with or without dashes)

  • :raw (String)

    the UUID in packed binary format (16 bytes)

Raises:



64
65
66
67
68
69
70
71
72
# File 'lib/mixin_bot/uuid.rb', line 64

def initialize(**args)
  args = args.with_indifferent_access

  @hex = args[:hex]
  @raw = args[:raw]

  raise MixinBot::InvalidUuidFormatError if raw.present? && raw.size != 16
  raise MixinBot::InvalidUuidFormatError if hex.present? && hex.gsub('-', '').size != 32
end

Instance Attribute Details

#hexString

Returns the UUID in hex format (with or without dashes).

Returns:

  • (String)

    the UUID in hex format (with or without dashes)



40
41
42
# File 'lib/mixin_bot/uuid.rb', line 40

def hex
  @hex
end

#rawString

Returns the UUID in packed binary format (16 bytes).

Returns:

  • (String)

    the UUID in packed binary format (16 bytes)



44
45
46
# File 'lib/mixin_bot/uuid.rb', line 44

def raw
  @raw
end

Instance Method Details

#packedString

Returns the UUID in packed binary format (16 bytes).

Examples:

uuid = MixinBot::UUID.new(hex: '965e5c6e-434c-3fa9-b780-c50f43cd955c')
uuid.packed
# => 16-byte binary string

Returns:

  • (String)

    16-byte binary string



84
85
86
87
88
89
90
# File 'lib/mixin_bot/uuid.rb', line 84

def packed
  if raw.present?
    raw
  elsif hex.present?
    [hex.gsub('-', '')].pack('H*')
  end
end

#unpackedString

Returns the UUID in standard format with dashes.

Examples:

uuid = MixinBot::UUID.new(raw: binary_string)
uuid.unpacked
# => "965e5c6e-434c-3fa9-b780-c50f43cd955c"

Returns:

  • (String)

    UUID in format “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mixin_bot/uuid.rb', line 102

def unpacked
  _hex =
    if hex.present?
      hex.gsub('-', '')
    elsif raw.present?
      _hex = raw.unpack1('H*')
    end

  format(
    '%<first>s-%<second>s-%<third>s-%<forth>s-%<fifth>s',
    first: _hex[0..7],
    second: _hex[8..11],
    third: _hex[12..15],
    forth: _hex[16..19],
    fifth: _hex[20..]
  )
end