Class: OnStomp::Components::Frame

Inherits:
Object
  • Object
show all
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)

Instance Method Summary (collapse)

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].

Examples:

frame['content-type'] #=> 'text/plain'

Parameters:

  • name (Object)

    the header name associated with the desired value

Returns:

  • (String)

    the value associated with the requested header name

See Also:



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.

Examples:

frame['content-type'] = 'text/plain' #=> 'text/plain'
frame['other header'] = 42 #=> '42'

Parameters:

  • name (Object)

    the header name to associate with the supplied value

  • val (Object)

    the value to associate with the supplied header name

Returns:

  • (String)

    the supplied value as a string, or nil if nil was supplied as the value.

See Also:



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.

Returns:

  • (Boolean)


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

Returns:

  • (Fixnum)


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.

Returns:

  • (Fixnum, nil)


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.

Returns:

  • (Array<String,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.

Returns:

  • (Fixnum, nil)


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.

Parameters:

  • name (#to_sym)

Returns:

  • (true, false)

See Also:



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.

Returns:

  • ([Fixnum,Fixnum])

    pair of non-negative integers that specify connection heart-beat settings



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