Class: StompOut::Frame

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

Overview

Container for STOMP frame

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = nil, headers = nil, body = nil) ⇒ Frame

Create Stomp frame

Parameters:

  • command (String, NilClass) (defaults to: nil)

    name in upper case

  • headers (Hash, NilClass) (defaults to: nil)

    with string header name as key

  • body (String) (defaults to: nil)


34
35
36
37
38
# File 'lib/stomp_out/frame.rb', line 34

def initialize(command = nil, headers = nil, body = nil)
  @command = command
  @headers = headers || {}
  @body = body || ""
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



27
28
29
# File 'lib/stomp_out/frame.rb', line 27

def body
  @body
end

#commandObject

Returns the value of attribute command.



27
28
29
# File 'lib/stomp_out/frame.rb', line 27

def command
  @command
end

#headersObject

Returns the value of attribute headers.



27
28
29
# File 'lib/stomp_out/frame.rb', line 27

def headers
  @headers
end

Instance Method Details

#require(version, required) ⇒ Array, Object

Verify that required headers are present and then return their values

Parameters:

  • version (String)

    of STOMP in use

  • required (Hash)

    headers with name as key and list of STOMP versions to be excluded from the verification as value

Returns:

  • (Array, Object)

    values of selected headers in header name sorted order, or individual header value if only one header required

Raises:



58
59
60
61
62
63
64
65
66
67
# File 'lib/stomp_out/frame.rb', line 58

def require(version, required)
  values = []
  required.keys.sort.each do |header|
    exclude = required[header]
    value = @headers[header]
    raise ProtocolError.new("Missing '#{header}' header", self) if value.nil? && !exclude.include?(version)
    values << value
  end
  values.size > 1 ? values : values.first
end

#to_sString

Serialize frame for transmission on wire

Returns:

  • (String)

    serialized frame



43
44
45
46
# File 'lib/stomp_out/frame.rb', line 43

def to_s
  @headers["content-length"] = @body.size.to_s if @body.include?(NULL)
  @headers.keys.sort.inject("#{@command}\n") { |r, key| r << "#{key}:#{@headers[key]}\n" } + "\n#{@body}#{NULL}\n"
end