Class: MixinBot::UUID
- Inherits:
-
Object
- Object
- MixinBot::UUID
- 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
-
#hex ⇒ String
The UUID in hex format (with or without dashes).
-
#raw ⇒ String
The UUID in packed binary format (16 bytes).
Instance Method Summary collapse
-
#initialize(**args) ⇒ UUID
constructor
Initializes a new UUID instance.
-
#packed ⇒ String
Returns the UUID in packed binary format (16 bytes).
-
#unpacked ⇒ String
Returns the UUID in standard format with dashes.
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.
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
#hex ⇒ String
Returns the UUID in hex format (with or without dashes).
40 41 42 |
# File 'lib/mixin_bot/uuid.rb', line 40 def hex @hex end |
#raw ⇒ String
Returns 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
#packed ⇒ String
Returns the UUID in packed binary format (16 bytes).
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 |
#unpacked ⇒ String
Returns the UUID in standard format with dashes.
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 |