Class: StompPublisher::Frame

Inherits:
Object
  • Object
show all
Defined in:
lib/stomp_publisher/frame.rb

Defined Under Namespace

Classes: InvalidFrame

Constant Summary collapse

COMMANDS =
%w(
  SEND
  SUBSCRIBE
  UNSUBSCRIBE
  BEGIN
  COMMIT
  ABORT
  ACK
  NACK
  DISCONNECT
  CONNECT
  STOMP
  CONNECTED
  MESSAGE
  RECEIPT
  ERROR
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, header, body = "") ⇒ Frame

Returns a new instance of Frame.



52
53
54
55
56
# File 'lib/stomp_publisher/frame.rb', line 52

def initialize(command, header, body = "")
  self.command = command
  self.header = header
  self.body = body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



24
25
26
# File 'lib/stomp_publisher/frame.rb', line 24

def body
  @body
end

#commandObject

Returns the value of attribute command.



24
25
26
# File 'lib/stomp_publisher/frame.rb', line 24

def command
  @command
end

#headerObject

Returns the value of attribute header.



24
25
26
# File 'lib/stomp_publisher/frame.rb', line 24

def header
  @header
end

Class Method Details

.parse(frame) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stomp_publisher/frame.rb', line 26

def self.parse(frame)
  command = COMMANDS.detect { |cmd| frame =~ /^(#{cmd}\r?\n)/ } or
      raise InvalidFrame.new("invalid or missing command")
  header_start_ndx = $1.length + 1

  header_separator_ndx = frame.index(/(\r?\n\r?\n)/) or
      raise InvalidFrame.new("missing headers")
  body_start_ndx = header_separator_ndx + $1.length

  body_terminator_ndx = frame.rindex(/\0(\r?\n)*/) or
      raise InvalidFrame.new("missing end of body")

  header = Header.parse(frame[(command.length + 1)..(header_separator_ndx - 1)])
  body = frame[body_start_ndx..(body_terminator_ndx - 1)]

  if (content_length = header["content-length"])
    content_length = Integer(content_length) or
        raise InvalidFrame("invalid content-length")

    body.bytesize == content_length or
        raise InvalidFrame("content-length was #{body.bytesize}, expected: #{content_length}")
  end

  Frame.new(command, header, body)
end

Instance Method Details

#to_sObject



63
64
65
# File 'lib/stomp_publisher/frame.rb', line 63

def to_s
  "#{command}\n#{header}\n\n#{body}\0"
end