Class: Kamerling::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/kamerling/message.rb

Constant Summary collapse

KNOWN_TYPES =
%i(DATA PING RGST RSLT)
UnknownType =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Message

Returns a new instance of Message.



20
21
22
23
# File 'lib/kamerling/message.rb', line 20

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

Class Method Details

.build(client:, payload:, project:, task:, type:) ⇒ Object



11
12
13
14
# File 'lib/kamerling/message.rb', line 11

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

.parse(raw) ⇒ Object



16
17
18
# File 'lib/kamerling/message.rb', line 16

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

Instance Method Details

#client_typeObject



25
26
27
# File 'lib/kamerling/message.rb', line 25

def client_type
  raw[4..7].to_sym
end

#client_uuidObject



29
30
31
# File 'lib/kamerling/message.rb', line 29

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

#payloadObject



33
34
35
# File 'lib/kamerling/message.rb', line 33

def payload
  raw[64..-1]
end

#project_uuidObject



37
38
39
# File 'lib/kamerling/message.rb', line 37

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

#task_uuidObject



41
42
43
# File 'lib/kamerling/message.rb', line 41

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

#to_hexObject



45
46
47
# File 'lib/kamerling/message.rb', line 45

def to_hex
  raw.unpack('H*').first.scan(/../).join(' ')
end

#to_sObject



49
50
51
# File 'lib/kamerling/message.rb', line 49

def to_s
  raw
end

#typeObject



53
54
55
# File 'lib/kamerling/message.rb', line 53

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