Class: RightScale::CloudApi::HTTPHeaders
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- RightScale::CloudApi::HTTPHeaders
- 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
-
#[](header) ⇒ Array
Retrieves the given headers values by the header name.
-
#[]=(header, value) ⇒ void
Sets a header.
-
#delete(header) ⇒ void
Deletes the given header.
-
#initialize(headers = {}) ⇒ HTTPHeaders
constructor
Initializer.
-
#merge(headers) ⇒ Hash
Merges the new headers into the existent list.
-
#merge!(headers) ⇒ Hash
Merges the new headers into the current hash.
-
#method_missing(method_name, *args, &block) ⇒ Object
Feeds all the unknown methods to the underlaying hash object.
-
#set_if_blank(header, value) ⇒ void
Set the given header unless it is set.
-
#to_hash ⇒ Hash
Returns a new Hash instance with all the current headers.
-
#to_s ⇒ String
Displays the headers in a nice way.
Constructor Details
#initialize(headers = {}) ⇒ HTTPHeaders
Initializer
are the headers values.
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
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
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
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
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
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
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.
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_hash ⇒ Hash
Returns a new Hash instance with all the current headers
151 152 153 |
# File 'lib/base/helpers/http_headers.rb', line 151 def to_hash @headers.dup end |
#to_s ⇒ String
Displays the headers in a nice way
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 |