Class: OnStomp::Components::Frame
- Inherits:
-
Object
- Object
- OnStomp::Components::Frame
- Defined in:
- lib/onstomp/components/frame.rb
Overview
A generic encapsulation of a frame as specified by the Stomp protocol.
Constant Summary collapse
- CONTENT_TYPE_REG =
Regex to match content-type header value. Eg: given “text/plain; … ;charset=ISO-8859-1 …”, then
-
$1 => type (‘text’)
-
$2 => subtype (‘plain’)
-
$3 => charset (‘ISO-8859-1’)
-
/^([a-z0-9!\#$&.+\-^_]+)\/([a-z0-9!\#$&.+\-^_]+)(?:.*;\s*charset=\"?([a-z0-9!\#$&.+\-^_]+)\"?)?/i
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#command ⇒ Object
Returns the value of attribute command.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
-
#[](name) ⇒ String
Gets the header value paired with the supplied name.
-
#[]=(name, val) ⇒ String
Sets the header value paired with the supplied name.
-
#all_headers?(*names) ⇒ Boolean
(also: #headers?)
Returns true if all given header names exist and none of their values are empty strings.
-
#body_length ⇒ Fixnum
Returns the byte-length of this frame’s body.
-
#content_length ⇒ Fixnum?
If a
content-length
header is set, returns it after converting it to an integer. -
#content_type ⇒ Array<String,nil>
If a
content-type
header is set, splits it into three parts: type, subtype and charset. -
#force_content_length ⇒ Fixnum?
Sets this frame’s
content-length
header to match the byte-length of its body, if the body has been set. -
#header?(name) ⇒ true, false
Returns true if the given header name exists and its value is not an empty string.
-
#heart_beat ⇒ [Fixnum,Fixnum]
Returns the heart-beat configuration specified in this frame’s headers.
-
#initialize(command = nil, headers = {}, body = nil) ⇒ Frame
constructor
Creates a new frame.
Constructor Details
#initialize(command = nil, headers = {}, body = nil) ⇒ Frame
Creates a new frame. The frame will be initialized with the optional command
name, a headers collection initialized with the optional headers
hash, and an optional body.
18 19 20 21 22 |
# File 'lib/onstomp/components/frame.rb', line 18 def initialize(command=nil, headers={}, body=nil) @command = command @headers = OnStomp::Components::FrameHeaders.new(headers) @body = body end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
12 13 14 |
# File 'lib/onstomp/components/frame.rb', line 12 def body @body end |
#command ⇒ Object
Returns the value of attribute command.
12 13 14 |
# File 'lib/onstomp/components/frame.rb', line 12 def command @command end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
13 14 15 |
# File 'lib/onstomp/components/frame.rb', line 13 def headers @headers end |
Instance Method Details
#[](name) ⇒ String
Gets the header value paired with the supplied name. This is a convenient shortcut for ‘frame.headers`.
32 |
# File 'lib/onstomp/components/frame.rb', line 32 def [](name); @headers[name]; end |
#[]=(name, val) ⇒ String
Sets the header value paired with the supplied name. This is a convenient shortcut for ‘frame.headers = val`.
44 |
# File 'lib/onstomp/components/frame.rb', line 44 def []=(name, val); @headers[name] = val; end |
#all_headers?(*names) ⇒ Boolean Also known as: headers?
Returns true if all given header names exist and none of their values are empty strings.
74 75 76 |
# File 'lib/onstomp/components/frame.rb', line 74 def all_headers? *names names.all? { |name| @headers.present?(name) } end |
#body_length ⇒ Fixnum
Returns the byte-length of this frame’s body
102 |
# File 'lib/onstomp/components/frame.rb', line 102 def body_length; body.bytesize; end |
#content_length ⇒ Fixnum?
If a content-length
header is set, returns it after converting it to an integer.
49 50 51 |
# File 'lib/onstomp/components/frame.rb', line 49 def content_length header?(:'content-length') ? @headers[:'content-length'].to_i : nil end |
#content_type ⇒ Array<String,nil>
If a content-type
header is set, splits it into three parts: type, subtype and charset. If any component of the content-type
is missing, its value will be nil
in the returned triple. If the content-type
header is not set or does not match CONTENT_TYPE_REG all values in the triple will be nil
.
59 60 61 |
# File 'lib/onstomp/components/frame.rb', line 59 def content_type @headers[:'content-type'] =~ CONTENT_TYPE_REG ? [$1, $2, $3] : [nil, nil, nil] end |
#force_content_length ⇒ Fixnum?
Sets this frame’s content-length
header to match the byte-length of its body, if the body has been set.
95 96 97 |
# File 'lib/onstomp/components/frame.rb', line 95 def force_content_length @headers[:'content-length'] = body_length if body end |
#header?(name) ⇒ true, false
Returns true if the given header name exists and its value is not an empty string.
68 69 70 |
# File 'lib/onstomp/components/frame.rb', line 68 def header? name @headers.present? name end |
#heart_beat ⇒ [Fixnum,Fixnum]
Returns the heart-beat configuration specified in this frame’s headers. If a heart-beat
header is not set, [0, 0] will be returned. Otherwise, the header value will be split on ‘,’ and each component will be converted to a non-negative integer.
85 86 87 88 89 90 |
# File 'lib/onstomp/components/frame.rb', line 85 def heart_beat (@headers[:'heart-beat'] || '0,0').split(',').map do |v| vi = v.to_i vi > 0 ? vi : 0 end end |