Class: OnStomp::Components::FrameHeaders

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/onstomp/components/frame_headers.rb

Overview

A specialized container for storing header name / value pairs for a frame. This container behaves much like a Hash, but is specialized for the Stomp protocol. Header names are always converted into Strings through the use of to_s and may have more than one value associated with them.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FrameHeaders) initialize(headers = {})

Creates a new headers collection, initialized with the optional hash parameter.

Parameters:

  • headers (Hash) (defaults to: {})

See Also:



15
16
17
18
19
# File 'lib/onstomp/components/frame_headers.rb', line 15

def initialize(headers={})
  @values = {}
  initialize_names
  merge! headers
end

Instance Attribute Details

- (Object) names (readonly)

Returns the value of attribute names



180
# File 'lib/onstomp/components/frame_headers.rb', line 180

def names; @values.keys; end

Instance Method Details

- (String?) [](name)

Gets the principle header value paired with the supplied header name. The name will be converted to a Symbol, so must respond to the to_sym method. The Stomp 1.1 protocol specifies that in the event of a repeated header name, the first value encountered serves as the principle value.

Examples:

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

Parameters:

  • name (#to_sym)

    the header name paired with the desired value (will be converted using to_sym)

Returns:

  • (String)

    the value associated with the requested header name

  • (nil)

    if no value has been set for the associated header name



133
134
135
136
# File 'lib/onstomp/components/frame_headers.rb', line 133

def [](name)
  name = name.to_sym
  @values[name] && @values[name].first
end

- (String) []=(name, val)

Sets the header value paired with the supplied header name. The name will be converted to a Symbol and must respond to to_sym; meanwhile, the value will be converted to a String so must respond to to_s. Setting a header value in this fashion will overwrite any repeated header values.

Examples:

headers['content-type'] = 'image/png' #=> 'image/png'
headers[:content-type'] = nil #=> ''
headers['content-type'] #=> ''

Parameters:

  • name (#to_sym)

    the header name to associate with the supplied value (will be converted using to_sym)

  • val (#to_s)

    the value to pair with the supplied name (will be converted using to_s)

Returns:

  • (String)

    the supplied value as a string.



150
151
152
153
154
155
156
# File 'lib/onstomp/components/frame_headers.rb', line 150

def []=(name, val)
  name = name.to_sym
  val = val.to_s
  add_name name
  @values[name] = [val]
  val
end

- (Array) all_values(name)

Retrieves all header values associated with the supplied header name. In general, this will be an array containing only the principle header value; however, in the event a frame contained repeated header names, this method will return all of the associated values. The first element of the array will be the principle value of the supplied header name.

Examples:

headers.all_values('content-type') #=> [ 'text/plain' ]
headers.all_values(:repeated_header) #=> [ 'principle value', '13', 'other value']
headers['name'] == headers.all_values(:name).first #=> true

Parameters:

  • name (#to_sym)

    the header name associated with the desired values (will be converted using to_sym)

Returns:

  • (Array)

    the array of values associated with the header name.



77
78
79
# File 'lib/onstomp/components/frame_headers.rb', line 77

def all_values(name)
  @values[name.to_sym] || []
end

- (String) append(name, val)

Appends a header value to the specified header name. If the specified header name is not known, the supplied value will also become the principle value. This method is used internally when constructing frames sent by the broker to capture repeated header names.

Examples:

headers.append(:new header', 'first value') #=> 'first value'
headers.append('new header', nil) #=> ''
headers.append('new header', 13) #=> '13'
headers['new header'] #=> 'first value'
headers.all('new header') #=> ['first value', '', '13']

Parameters:

  • name (#to_sym)

    the header name to associate with the supplied value (will be converted using to_s)

  • val (#to_s)

    the header value to associate with the supplied name (will be converted using to_s)

Returns:

  • (String)

    the supplied value as a string.



95
96
97
98
99
100
101
102
103
104
# File 'lib/onstomp/components/frame_headers.rb', line 95

def append(name, val)
  name = name.to_sym
  val = val.to_s
  if @values.key?(name)
    @values[name] << val
  else
    self[name]= val
  end
  val
end

- (Array) delete(name)

Deletes all of the header values associated with the header name and removes the header name itself. This is analogous to the delete method found in Hash objects.

Examples:

headers.delete(:content-type') #=> [ 'text/html' ]
headers.delete('no such header') #=> nil

Parameters:

  • name (#to_sym)

    the header name to remove from this collection (will be converted using to_sym)

Returns:

  • (Array)

    the array of values associated with the deleted header, or nil if the header name did not exist



115
116
117
118
119
120
121
# File 'lib/onstomp/components/frame_headers.rb', line 115

def delete(name)
  name = name.to_sym
  if @values.key? name
    delete_name name
    @values.delete name
  end
end

- (Object) each {|header_name, header_value| ... }

Iterates over header name / value pairs, yielding them as a pair of strings to the supplied block.

Yields:

  • (header_name, header_value)

Yield Parameters:

  • header_name (String)
  • header_value (String)


170
171
172
173
174
175
176
177
# File 'lib/onstomp/components/frame_headers.rb', line 170

def each &block
  if block_given?
    iterate_each &block
    self
  else
    OnStomp::ENUMERATOR_KLASS.new(self)
  end
end

- (Object) merge!(hash)

Note:

With Ruby 1.8.7, the order of hash keys may not be preserved

Merges a hash into this collection of headers. All of the keys used in the hash must be convertable to Symbols through to_sym.

Parameters:

  • hash (Hash)


25
26
27
# File 'lib/onstomp/components/frame_headers.rb', line 25

def merge!(hash)
  hash.each { |k, v| self[k]= v }
end

- (Boolean) present?(name)

Returns true if a header value has been set for the supplied header, and the value is neither nil nor an empty string.

Examples:

header[:test1] = 'set'
header[:test2] = ''
header.present? :test1 #=> true
header.present? :test2 #=> false

Parameters:

  • name (#to_sym)

    the header name to test

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/onstomp/components/frame_headers.rb', line 59

def present?(name)
  val = self[name]
  !(val.nil? || val.empty?)
end

- (Object) reverse_merge!(hash)

Note:

With Ruby 1.8.7, the order of hash keys may not be preserved

Reverse merges a hash into this collection of headers. The hash keys and values are included only if the headers collection does not already have a matching key. All of the keys used in the hash must be convertable to Symbols through to_sym.

Parameters:

  • hash (Hash)


35
36
37
38
39
# File 'lib/onstomp/components/frame_headers.rb', line 35

def reverse_merge!(hash)
  hash.each { |k, v|
    self[k]= v unless set?(k)
  }
end

- (Boolean) set?(name)

Returns true if a header value has been set for the supplied header name.

Examples:

header.set? 'content-type' #=> true

Parameters:

  • name (#to_sym)

    the header name to test

Returns:

  • (Boolean)


46
47
48
# File 'lib/onstomp/components/frame_headers.rb', line 46

def set?(name)
  @values.key?(name.to_sym)
end

- (Hash) to_hash

Returns a new Hash object associating symbolized header names and their principle values.

Returns:

  • (Hash)


161
162
163
# File 'lib/onstomp/components/frame_headers.rb', line 161

def to_hash
  @values.inject({}) { |h, (k,v)| h[k] = v.first; h }
end