Class: Seahorse::Client::Http::Headers
- Inherits:
-
Object
- Object
- Seahorse::Client::Http::Headers
- Includes:
- Enumerable
- Defined in:
- lib/seahorse/client/http/headers.rb
Overview
Provides a Hash-like interface for HTTP headers. Header names are treated indifferently as lower-cased strings. Header values are cast to strings.
headers = Http::Headers.new
headers['Content-Length'] = 100
headers[:Authorization] = 'Abc'
headers.keys
#=> ['content-length', 'authorization']
headers.values
#=> ['100', 'Abc']
You can get the header values as a vanilla hash by calling #to_h:
headers.to_h
#=> { 'content-length' => '100', 'authorization' => 'Abc' }
Instance Method Summary collapse
- #[](key) ⇒ String
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #each {|key, value| ... } ⇒ nil (also: #each_pair)
-
#initialize(headers = {}) ⇒ Headers
constructor
private
A new instance of Headers.
- #inspect ⇒ Object private
-
#key?(key) ⇒ Boolean
(also: #has_key?, #include?)
Returns ‘true` if the header is set.
- #keys ⇒ Array<String>
- #to_hash ⇒ Hash (also: #to_h)
- #update(headers) ⇒ Headers
- #values ⇒ Array<String>
- #values_at(*keys) ⇒ Array<String>
Constructor Details
#initialize(headers = {}) ⇒ Headers
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Headers.
29 30 31 32 33 34 |
# File 'lib/seahorse/client/http/headers.rb', line 29 def initialize(headers = {}) @data = {} headers.each_pair do |key, value| self[key] = value end end |
Instance Method Details
#[](key) ⇒ String
38 39 40 |
# File 'lib/seahorse/client/http/headers.rb', line 38 def [](key) @data[key.to_s.downcase] end |
#[]=(key, value) ⇒ Object
44 45 46 |
# File 'lib/seahorse/client/http/headers.rb', line 44 def []=(key, value) @data[key.to_s.downcase] = value.to_s end |
#clear ⇒ Object
62 63 64 |
# File 'lib/seahorse/client/http/headers.rb', line 62 def clear @data = {} end |
#delete(key) ⇒ Object
58 59 60 |
# File 'lib/seahorse/client/http/headers.rb', line 58 def delete(key) @data.delete(key.to_s.downcase) end |
#each {|key, value| ... } ⇒ nil Also known as: each_pair
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/seahorse/client/http/headers.rb', line 85 def each(&block) if block_given? @data.each_pair do |key, value| yield(key, value) end nil else @data.enum_for(:each) end end |
#inspect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
111 112 113 |
# File 'lib/seahorse/client/http/headers.rb', line 111 def inspect @data.inspect end |
#key?(key) ⇒ Boolean Also known as: has_key?, include?
Returns ‘true` if the header is set.
98 99 100 |
# File 'lib/seahorse/client/http/headers.rb', line 98 def key?(key) @data.key?(key.to_s.downcase) end |
#keys ⇒ Array<String>
67 68 69 |
# File 'lib/seahorse/client/http/headers.rb', line 67 def keys @data.keys end |
#to_hash ⇒ Hash Also known as: to_h
105 106 107 |
# File 'lib/seahorse/client/http/headers.rb', line 105 def to_hash @data.dup end |
#update(headers) ⇒ Headers
50 51 52 53 54 55 |
# File 'lib/seahorse/client/http/headers.rb', line 50 def update(headers) headers.each_pair do |k, v| self[k] = v end self end |
#values ⇒ Array<String>
72 73 74 |
# File 'lib/seahorse/client/http/headers.rb', line 72 def values @data.values end |
#values_at(*keys) ⇒ Array<String>
77 78 79 |
# File 'lib/seahorse/client/http/headers.rb', line 77 def values_at(*keys) @data.values_at(*keys.map{ |key| key.to_s.downcase }) end |