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
- 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)
-
- (Object) body
Returns the value of attribute body.
-
- (Object) command
Returns the value of attribute command.
-
- (Object) headers
readonly
Returns the value of attribute headers.
Instance Method Summary (collapse)
-
- (String) [](name)
Gets the header value paired with the supplied name.
-
- (String) []=(name, val)
Sets the header value paired with the supplied name.
-
- (Boolean) all_headers?(*names)
(also: #headers?)
Returns true if all given header names exist and none of their values are empty strings.
-
- (Fixnum) body_length
Returns the byte-length of this frame's body.
-
- (Fixnum?) content_length
If a
content-length
header is set, returns it after converting it to an integer. -
- (Array<String,nil>) content_type
If a
content-type
header is set, splits it into three parts: type, subtype and charset. -
- (Fixnum?) force_content_length
Sets this frame's
content-length
header to match the byte-length of its body, if the body has been set. -
- (true, false) header?(name)
Returns true if the given header name exists and its value is not an empty string.
-
- ([Fixnum,Fixnum]) heart_beat
Returns the heart-beat configuration specified in this frame's headers.
-
- (Frame) initialize(command = nil, headers = {}, body = nil)
constructor
Creates a new frame.
Constructor Details
- (Frame) initialize(command = nil, headers = {}, body = nil)
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
- (Object) body
Returns the value of attribute body
12 13 14 |
# File 'lib/onstomp/components/frame.rb', line 12 def body @body end |
- (Object) command
Returns the value of attribute command
12 13 14 |
# File 'lib/onstomp/components/frame.rb', line 12 def command @command end |
- (Object) headers (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
- (String) [](name)
Gets the header value paired with the supplied name. This is a convenient
shortcut for frame.headers[name]
.
32 |
# File 'lib/onstomp/components/frame.rb', line 32 def [](name); @headers[name]; end |
- (String) []=(name, val)
Sets the header value paired with the supplied name. This is a convenient
shortcut for frame.headers[name] = val
.
44 |
# File 'lib/onstomp/components/frame.rb', line 44 def []=(name, val); @headers[name] = val; end |
- (Boolean) all_headers?(*names) 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 |
- (Fixnum) body_length
Returns the byte-length of this frame's body
102 |
# File 'lib/onstomp/components/frame.rb', line 102 def body_length; body.length; end |
- (Fixnum?) content_length
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 |
- (Array<String,nil>) content_type
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 |
- (Fixnum?) force_content_length
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 |
- (true, false) header?(name)
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 |
- ([Fixnum,Fixnum]) heart_beat
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 |