Class: StickyElephant::Payload

Inherits:
Object
  • Object
show all
Includes:
PayloadTypes
Defined in:
lib/sticky_elephant/payload.rb

Constant Summary collapse

VALID_TYPES =
%i(ssl_request handshake quit query).freeze

Constants included from PayloadTypes

StickyElephant::PayloadTypes::TYPES

Instance Method Summary collapse

Methods included from PayloadTypes

#handler, #type

Constructor Details

#initialize(bytes = []) ⇒ Payload

Returns a new instance of Payload.



5
6
7
# File 'lib/sticky_elephant/payload.rb', line 5

def initialize(bytes = [])
  @bytes = bytes.dup.freeze
end

Instance Method Details

#==(arr) ⇒ Object



14
15
16
# File 'lib/sticky_elephant/payload.rb', line 14

def ==(arr)
  bytes == arr
end

#rawObject



37
38
39
# File 'lib/sticky_elephant/payload.rb', line 37

def raw
  bytes
end

#to_s(with_type: true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/sticky_elephant/payload.rb', line 10

def valid?
  VALID_TYPES.include? type
end

#valid_length?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# 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