Class: StompParser::Frame

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

Constant Summary collapse

HEADER_TRANSLATIONS =
{
  '\\r' => "\r",
  '\\n' => "\n",
  '\\c' => ":",
  '\\\\' => '\\',
}.freeze
HEADER_TRANSLATIONS_KEYS =
Regexp.union(HEADER_TRANSLATIONS.keys).freeze
HEADER_REVERSE_TRANSLATIONS =
HEADER_TRANSLATIONS.invert
HEADER_REVERSE_TRANSLATIONS_KEYS =
Regexp.union(HEADER_REVERSE_TRANSLATIONS.keys).freeze
EMPTY =
"".force_encoding("UTF-8").freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, headers = {}, body) ⇒ Frame

Construct a frame from a command, optional headers, and a body.

Parameters:

  • command (String)
  • headers (Hash<String, String>) (defaults to: {})
  • body (String)


28
29
30
31
32
# File 'lib/stomp_parser/frame.rb', line 28

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

Instance Attribute Details

#bodyString (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/stomp_parser/frame.rb', line 21

def body
  @body
end

#commandString (readonly)

Returns:

  • (String)


15
16
17
# File 'lib/stomp_parser/frame.rb', line 15

def command
  @command
end

#headersHash<String, String> (readonly)

Returns:

  • (Hash<String, String>)


18
19
20
# File 'lib/stomp_parser/frame.rb', line 18

def headers
  @headers
end

Instance Method Details

#[](key) ⇒ Object



113
114
115
# File 'lib/stomp_parser/frame.rb', line 113

def [](key)
  @headers[key]
end

#content_encodingEncoding

Returns body encoding, according to headers.

Returns:

  • (Encoding)

    body encoding, according to headers.

Raises:

  • (ArgumentError)

    if encoding does not exist



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stomp_parser/frame.rb', line 54

def content_encoding
  if content_type
    mime_type, charset = content_type.to_s.split(";")
    mime_type = mime_type.to_s
    charset = charset.to_s[/\Acharset=(.*)/, 1].to_s

    if charset.empty? and mime_type.to_s.start_with?("text/")
      Encoding::UTF_8
    elsif charset.empty?
      Encoding::BINARY
    else
      Encoding.find(charset)
    end
  else
    Encoding::BINARY
  end
end

#content_lengthInteger?

Content length of this frame, according to headers.

Returns:

  • (Integer, nil)

Raises:

  • (ArgumentError)

    if content-length is not a valid integer



38
39
40
41
42
43
44
45
46
# File 'lib/stomp_parser/frame.rb', line 38

def content_length
  if headers.has_key?("content-length")
    begin
      Integer(headers["content-length"])
    rescue ArgumentError
      raise Error, "invalid content length #{headers["content-length"].inspect}"
    end
  end
end

#content_typeObject



48
49
50
# File 'lib/stomp_parser/frame.rb', line 48

def content_type
  headers["content-type"]
end

#destinationObject



117
118
119
# File 'lib/stomp_parser/frame.rb', line 117

def destination
  self["destination"]
end

#to_strString Also known as: to_s

Returns a string-representation of this frame.

Returns:

  • (String)

    a string-representation of this frame.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/stomp_parser/frame.rb', line 97

def to_str
  frame = "".force_encoding("UTF-8")
  frame << command << "\n"

  outgoing_headers = headers.dup
  outgoing_headers["content-length"] = body.bytesize
  outgoing_headers.each do |key, value|
    frame << serialize_header(key) << ":" << serialize_header(value) << "\n"
  end
  frame << "\n"

  frame << body << "\x00"
  frame
end

#write_body(body) ⇒ Object

Write the body to this frame.

Parameters:

  • body (String)


92
93
94
# File 'lib/stomp_parser/frame.rb', line 92

def write_body(body)
  @body = body.force_encoding(content_encoding)
end

#write_command(command) ⇒ Object

Change the command of this frame.

Parameters:

  • command (String)


75
76
77
# File 'lib/stomp_parser/frame.rb', line 75

def write_command(command)
  @command = command
end

#write_header(key, value) ⇒ Object

Write a single header to this frame.

Parameters:

  • key (String)
  • value (String)


83
84
85
86
87
# File 'lib/stomp_parser/frame.rb', line 83

def write_header(key, value)
  # @see http://stomp.github.io/stomp-specification-1.2.html#Repeated_Header_Entries
  key = translate_header(key)
  @headers[key] = translate_header(value) unless @headers.has_key?(key)
end