Class: RightScale::CloudApi::HTTPHeaders

Inherits:
BlankSlate show all
Defined in:
lib/base/helpers/http_headers.rb

Overview

HTTP Headers container.

The class makes it so that the headers always point to an array or values. It is a wrapper around Hash class where all the keys are always arrays.

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}) ⇒ HTTPHeaders

Initializer

are the headers values.

Examples:

# no example


44
45
46
# File 'lib/base/helpers/http_headers.rb', line 44

def initialize(headers={})
  @headers = normalize(headers)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Feeds all the unknown methods to the underlaying hash object

Examples:

# no example


175
176
177
# File 'lib/base/helpers/http_headers.rb', line 175

def method_missing(method_name, *args, &block)
  @headers.__send__(method_name, *args, &block)
end

Instance Method Details

#[](header) ⇒ Array

Retrieves the given headers values by the header name

Examples:

request[:agent] #=> ['something']


57
58
59
# File 'lib/base/helpers/http_headers.rb', line 57

def [](header)
  @headers[normalize_header(header)] || []
end

#[]=(header, value) ⇒ void

This method returns an undefined value.

Sets a header

Examples:

# Add a header
request[:agent] = 'something'
# Remove a header
request[:agent] = nil


74
75
76
77
78
79
80
81
# File 'lib/base/helpers/http_headers.rb', line 74

def []=(header, value)
  value = normalize_value(value)
  if value.nil?
    delete(header)
  else
    @headers[normalize_header(header)] = value
  end
end

#delete(header) ⇒ void

This method returns an undefined value.

Deletes the given header

Examples:

# Delete the header
request.delete(:agent)


93
94
95
# File 'lib/base/helpers/http_headers.rb', line 93

def delete(header)
  @headers.delete(normalize_header(header))
end

#merge(headers) ⇒ Hash

Merges the new headers into the existent list

Examples:

# Delete the header
request.merge(:agent => 'foobar')


108
109
110
# File 'lib/base/helpers/http_headers.rb', line 108

def merge(headers)
  @headers.merge(normalize(headers))
end

#merge!(headers) ⇒ Hash

Merges the new headers into the current hash

Examples:

# Delete the header
request.merge!(:agent => 'foobar')


123
124
125
# File 'lib/base/helpers/http_headers.rb', line 123

def merge!(headers)
  @headers.merge!(normalize(headers))
end

#set_if_blank(header, value) ⇒ void

This method returns an undefined value.

Set the given header unless it is set

If the curent headers list already has any value for the given header the method does nothing.

Examples:

request.set_if_blank(:agent, 'something')


139
140
141
# File 'lib/base/helpers/http_headers.rb', line 139

def set_if_blank(header, value)
  self[header] = value if self[header].first._blank?
end

#to_hashHash

Returns a new Hash instance with all the current headers

Examples:

request.to_hash #=> A hash with headers


151
152
153
# File 'lib/base/helpers/http_headers.rb', line 151

def to_hash
  @headers.dup
end

#to_sString

Displays the headers in a nice way

Examples:

ec2.response.headers.to_s #=>
 'content-type: "text/xml;charset=UTF-8", server: "AmazonEC2", something: ["a", "b"]'


164
165
166
# File 'lib/base/helpers/http_headers.rb', line 164

def to_s
  @headers.to_a.map { |header, value| "#{header}: #{(value.size == 1 ? value.first : value).inspect}" } * ', '
end