class StickyElephant::Payload

Constants

CLAIMED_TYPES
VALID_TYPES

Attributes

bytes[R]

Public Class Methods

new(bytes = []) click to toggle source
# File lib/sticky_elephant/payload.rb, line 5
def initialize(bytes = [])
  @bytes = bytes.dup.freeze
end

Public Instance Methods

==(arr) click to toggle source
# File lib/sticky_elephant/payload.rb, line 14
def ==(arr)
  bytes == arr
end
raw() click to toggle source
# File lib/sticky_elephant/payload.rb, line 37
def raw
  bytes
end
to_s(with_type: true) click to toggle source
# File lib/sticky_elephant/payload.rb, line 26
def to_s(with_type: true)
  message = without_length.
    select {|byte| byte != 0 }.
    pack("C*")
  if with_type
    "#{type.to_s.upcase}: '#{message}'"
  else
    message
  end
end
valid?() click to toggle source
# File lib/sticky_elephant/payload.rb, line 10
def valid?
  VALID_TYPES.include? type
end
valid_length?() click to toggle source
# File lib/sticky_elephant/payload.rb, line 18
def valid_length?
  if has_claimed_type?
    bytes[1..4].pack("C*").unpack("N").first == bytes.size - 1
  else
    bytes[0..3].pack("C*").unpack("N").first == bytes.size
  end
end

Private Instance Methods

has_claimed_type?() click to toggle source
# File lib/sticky_elephant/payload.rb, line 52
def has_claimed_type?
  CLAIMED_TYPES.include? bytes.first.chr
end
without_length() click to toggle source
# File lib/sticky_elephant/payload.rb, line 43
def without_length
  if has_claimed_type?
    bytes[5..-1]
  else
    bytes[4..-1]
  end
end