Class: Fetch::Headers
- Inherits:
-
Object
- Object
- Fetch::Headers
- Includes:
- Enumerable
- Defined in:
- lib/fetch/headers.rb
Instance Method Summary collapse
- #append(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #each(&block) ⇒ Object
- #entries ⇒ Object
- #get(key) ⇒ Object
- #has(key) ⇒ Object
-
#initialize(init = []) ⇒ Headers
constructor
A new instance of Headers.
- #keys ⇒ Object
- #set(key, value) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(init = []) ⇒ Headers
Returns a new instance of Headers.
5 6 7 8 9 10 11 |
# File 'lib/fetch/headers.rb', line 5 def initialize(init = []) @data = {} init.each do |k, v| append k, v end end |
Instance Method Details
#append(key, value) ⇒ Object
13 14 15 |
# File 'lib/fetch/headers.rb', line 13 def append(key, value) (@data[key.to_s.downcase] ||= []) << value.to_s end |
#delete(key) ⇒ Object
17 18 19 |
# File 'lib/fetch/headers.rb', line 17 def delete(key) @data.delete key.to_s.downcase end |
#each(&block) ⇒ Object
45 46 47 |
# File 'lib/fetch/headers.rb', line 45 def each(&block) block ? entries.each(&block) : entries.each end |
#entries ⇒ Object
21 22 23 |
# File 'lib/fetch/headers.rb', line 21 def entries @data.map {|k, vs| [k, vs.join(', ')] } end |
#get(key) ⇒ Object
25 26 27 |
# File 'lib/fetch/headers.rb', line 25 def get(key) @data[key.to_s.downcase]&.join(', ') end |
#has(key) ⇒ Object
29 30 31 |
# File 'lib/fetch/headers.rb', line 29 def has(key) @data.key?(key.to_s.downcase) end |
#keys ⇒ Object
33 34 35 |
# File 'lib/fetch/headers.rb', line 33 def keys @data.keys end |
#set(key, value) ⇒ Object
37 38 39 |
# File 'lib/fetch/headers.rb', line 37 def set(key, value) @data[key.to_s.downcase] = [value] end |
#values ⇒ Object
41 42 43 |
# File 'lib/fetch/headers.rb', line 41 def values @data.values.map { _1.join(', ') } end |