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

Instance Method Summary collapse

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

#bodyObject

Returns the value of attribute body.



12
13
14
# File 'lib/onstomp/components/frame.rb', line 12

def body
  @body
end

#commandObject

Returns the value of attribute command.



12
13
14
# File 'lib/onstomp/components/frame.rb', line 12

def command
  @command
end

#headersObject (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`.

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:

  • 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`.

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:

  • Headers#[]=


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.

Returns:

  • (Boolean)


74
75
76
# File 'lib/onstomp/components/frame.rb', line 74

def all_headers? *names
  names.all? { |name| @headers.present?(name) }
end

#body_lengthFixnum

Returns the byte-length of this frame’s body

Returns:

  • (Fixnum)


102
# File 'lib/onstomp/components/frame.rb', line 102

def body_length; body.bytesize; end

#content_lengthFixnum?

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

#content_typeArray<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.

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

#force_content_lengthFixnum?

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

#header?(name) ⇒ true, false

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

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

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