Module: Stomper::Support::Ruby1_9::Headers

Defined in:
lib/stomper/support/1.9/headers.rb

Overview

The implementation of the Headers class for Ruby 1.9

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ String?

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 (Object)

    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



108
109
110
111
# File 'lib/stomper/support/1.9/headers.rb', line 108

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

#[]=(name, val) ⇒ String

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 (Object)

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

  • val (Object)

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

Returns:

  • (String)

    the supplied value as a string.



125
126
127
128
129
130
# File 'lib/stomper/support/1.9/headers.rb', line 125

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

#all_values(name) ⇒ Array Also known as: all

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(:repeated_header) #=> [ 'principle value', '13', 'other value']
headers['name'] == headers.all(:name).first #=> true

Parameters:

  • name (Object)

    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.



58
59
60
# File 'lib/stomper/support/1.9/headers.rb', line 58

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

#append(name, val) ⇒ String

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 (Object)

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

  • val (Object)

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

Returns:

  • (String)

    the supplied value as a string.



90
91
92
93
94
95
96
# File 'lib/stomper/support/1.9/headers.rb', line 90

def append(name, val)
  name = name.to_sym
  val = val.to_s
  @values[name] ||= []
  @values[name] << val
  val
end

#delete(name) ⇒ Array

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 (Object)

    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



72
73
74
# File 'lib/stomper/support/1.9/headers.rb', line 72

def delete(name)
  @values.delete name.to_sym
end

#each {|name, value| ... } ⇒ Headers, Enumerator

Iterates over each header name / value pair in the order in which the headers names were set. If this collection contains repeated header names, the supplied block will receive those header names repeatedly, once for each value. If no block is supplied, then an Enumerator is returned. All header names yielded through this method will be Strings and not the Symbol keys used internally.

Examples:

headers['name 1'] = 'value 1'
headers.append('name_2', 'value 2')
headers.append(:name_2, 42)
headers.each { |name, val| p "#{name} - #{v}" }
# name 1 - value 1
# name_2 - value 2
# name_2 - 42
#=> #<Stomper::Components::Headers:0x0000010289d6d8>

Yields:

  • (name, value)

    a header name and an associated value

Yield Parameters:

  • name (String)

    a header name

  • value (String)

    a value associated with the header

Returns:

  • (Headers)

    self if a block was supplied

  • (Enumerator)

    a collection enumerator if no block was supplied



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/stomper/support/1.9/headers.rb', line 153

def each(&block)
  if block_given?
    @values.each do |name, vals|
      name_str = name.to_s
      vals.each do |val|
        yield [name_str, val]
      end
    end
    self
  else
    ::Enumerator.new(self)
  end
end

#has?(name) ⇒ Boolean Also known as: key?, include?

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

Examples:

header.has? 'content-type' #=> true
header.key? 'unset header' #=> false
header.include? 'content-length' #=> true

Parameters:

  • name (Object)

    the header name to test (will be converted using to_sym)

Returns:

  • (Boolean)

    true if the specified header name has been set, otherwise false.



39
40
41
# File 'lib/stomper/support/1.9/headers.rb', line 39

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

#initialize(headers = {}) ⇒ Object

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

Parameters:

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

See Also:



9
10
11
12
# File 'lib/stomper/support/1.9/headers.rb', line 9

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

#merge!(hash) ⇒ Object

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)


17
18
19
# File 'lib/stomper/support/1.9/headers.rb', line 17

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

#namesArray<String>

An array of header names ordered by when they were set.

Returns:

  • (Array<String>)


169
# File 'lib/stomper/support/1.9/headers.rb', line 169

def names; @values.keys; end

#reverse_merge!(hash) ⇒ Object

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)


26
27
28
29
30
# File 'lib/stomper/support/1.9/headers.rb', line 26

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