Sha256: d3830d6f700791ee94381d8731b92d4ace674d5b4e1c8834de769e4059315fdd

Contents?: true

Size: 851 Bytes

Versions: 1

Compression:

Stored size: 851 Bytes

Contents

module Kamerling class Message
  KnownTypes  = %w[DATA PING RGST RSLT]
  UnknownType = Class.new RuntimeError

  def self.[](client: req(:client), payload: req(:payload),
              project: req(:project), task: req(:task), type: req(:type))
    new "#{type}\0\0\0\0\0\0\0\0\0\0\0\0" + UUID.bin(client.uuid) +
      UUID.bin(project.uuid) + UUID.bin(task.uuid) + payload
  end

  def initialize raw
    @raw = raw
    type = raw[0..3]
    raise UnknownType, type unless KnownTypes.include? type or type.empty?
  end

  def == other
    raw == other.raw
  end

  def client_uuid
    UUID[raw[16..31]]
  end

  def payload
    raw[64..-1]
  end

  def project_uuid
    UUID[raw[32..47]]
  end

  def task_uuid
    UUID[raw[48..63]]
  end

  def to_s
    raw
  end

  def type
    raw[0..3].to_sym
  end

  attr_reader :raw
  private     :raw
end end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kamerling-0.0.1 lib/kamerling/message.rb