Class: StompParser::Frame
- Inherits:
-
Object
- Object
- StompParser::Frame
- 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
- #body ⇒ String readonly
- #command ⇒ String readonly
- #headers ⇒ Hash<String, String> readonly
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#content_encoding ⇒ Encoding
Body encoding, according to headers.
-
#content_length ⇒ Integer?
Content length of this frame, according to headers.
- #content_type ⇒ Object
- #destination ⇒ Object
-
#initialize(command, headers = {}, body) ⇒ Frame
constructor
Construct a frame from a command, optional headers, and a body.
-
#to_str ⇒ String
(also: #to_s)
A string-representation of this frame.
-
#write_body(body) ⇒ Object
Write the body to this frame.
-
#write_command(command) ⇒ Object
Change the command of this frame.
-
#write_header(key, value) ⇒ Object
Write a single header to this frame.
Constructor Details
Instance Attribute Details
#body ⇒ String (readonly)
21 22 23 |
# File 'lib/stomp_parser/frame.rb', line 21 def body @body end |
#command ⇒ String (readonly)
15 16 17 |
# File 'lib/stomp_parser/frame.rb', line 15 def command @command end |
#headers ⇒ Hash<String, String> (readonly)
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_encoding ⇒ Encoding
Returns body encoding, according to headers.
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_length ⇒ Integer?
Content length of this frame, according to headers.
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_type ⇒ Object
48 49 50 |
# File 'lib/stomp_parser/frame.rb', line 48 def content_type headers["content-type"] end |
#destination ⇒ Object
117 118 119 |
# File 'lib/stomp_parser/frame.rb', line 117 def destination self["destination"] end |
#to_str ⇒ String Also known as: to_s
Returns 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.
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.
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.
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 |