Sha256: 5f532627d33359bea37416d244a8ab12e46d026d9a490bc43f6f328da4f2d1e2

Contents?: true

Size: 842 Bytes

Versions: 1

Compression:

Stored size: 842 Bytes

Contents

require 'miu'
require 'securerandom'
require 'msgpack'

module Miu
  class Packet
    attr_accessor :tag, :body, :id, :time

    def initialize(tag, body, options = {})
      @tag = tag
      @body = body
      @id = options[:id] || SecureRandom.uuid
      @time = options[:time] || Time.now.to_i
    end

    def dump
      data = {
        'body' => @body,
        'id' => @id,
        'time' => @time,
      }
      [@tag, data.to_msgpack]
    end

    def self.load(parts)
      tag = parts.shift
      data = MessagePack.unpack(parts.shift)
      body = data.delete('body')
      new tag, body, Miu::Utility.symbolized_keys(data)
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
miu-0.1.0 lib/miu/packet.rb