Sha256: 5cb6c2dd5eb940dbb6d57777fa02908f4b469a9b4e3c81a446566458bcad8f9d

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'equalizer'
require_relative 'uuid'

module Kamerling
  class Message
    KNOWN_TYPES = %i(DATA PING RGST RSLT)
    UnknownType = Class.new(RuntimeError)

    include Equalizer.new(:raw)

    def self.build(client:, payload:, project:, task:, 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].join)
    end

    def self.parse(raw)
      new(raw)
    end

    def initialize(raw)
      @raw = raw
      fail UnknownType, type unless KNOWN_TYPES.include?(type) or type.empty?
    end

    def client_type
      raw[4..7].to_sym
    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_hex
      raw.unpack('H*').first.scan(/../).join(' ')
    end

    def to_s
      raw
    end

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

    attr_reader :raw
    protected   :raw
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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