Class: Miu::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/miu/packet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, data) ⇒ Packet

Returns a new instance of Packet.



8
9
10
11
# File 'lib/miu/packet.rb', line 8

def initialize(tag, data)
  @tag = tag
  @data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/miu/packet.rb', line 6

def data
  @data
end

#tagObject

Returns the value of attribute tag.



6
7
8
# File 'lib/miu/packet.rb', line 6

def tag
  @tag
end

Class Method Details

.load(parts) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/miu/packet.rb', line 17

def self.load(parts)
  tag = parts[0]
  data = MessagePack.unpack(parts[1])
  new tag, data
rescue => e
  raise PacketLoadError, e
end

Instance Method Details

#dumpObject



13
14
15
# File 'lib/miu/packet.rb', line 13

def dump
  [@tag.to_s, @data.to_msgpack]
end

#inspectObject



29
30
31
32
33
34
# File 'lib/miu/packet.rb', line 29

def inspect
  inspection = [:tag, :data].map do |name|
    "#{name}: #{__send__(name).inspect}"
  end.join(', ')
  "#<#{self.class} #{inspection}>"
end

#to_sObject



25
26
27
# File 'lib/miu/packet.rb', line 25

def to_s
  "<#{tag}> #{data.to_h}"
end