Class: Async::HTTP::Headers
- Inherits:
-
Object
- Object
- Async::HTTP::Headers
- Defined in:
- lib/async/http/headers.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#delete(key) ⇒ Object
Delete all headers with the given key, and return the value of the last one, if any.
- #each(&block) ⇒ Object
- #freeze ⇒ Object
- #include?(key) ⇒ Boolean
-
#initialize(fields = []) ⇒ Headers
constructor
A new instance of Headers.
- #to_h ⇒ Object
Constructor Details
#initialize(fields = []) ⇒ Headers
Returns a new instance of Headers.
24 25 26 27 |
# File 'lib/async/http/headers.rb', line 24 def initialize(fields = []) @fields = fields @indexed = to_h end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
29 30 31 |
# File 'lib/async/http/headers.rb', line 29 def fields @fields end |
Instance Method Details
#==(other) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/async/http/headers.rb', line 96 def == other if other.is_a? Hash to_h == other else @fields == other.fields end end |
#[](key) ⇒ Object
76 77 78 79 80 |
# File 'lib/async/http/headers.rb', line 76 def [] key @indexed ||= to_h @indexed[key] end |
#[]=(key, value) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/async/http/headers.rb', line 62 def []= key, value @fields << [key, value] if @indexed key = key.downcase if current_value = @indexed[key] @indexed[key] = Array(current_value) << value else @indexed[key] = value end end end |
#delete(key) ⇒ Object
Delete all headers with the given key, and return the value of the last one, if any.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/async/http/headers.rb', line 48 def delete(key) values, @fields = @fields.partition do |field| field.first.downcase == key end if @indexed @indexed.delete(key) end if field = values.last return field.last end end |
#each(&block) ⇒ Object
39 40 41 |
# File 'lib/async/http/headers.rb', line 39 def each(&block) @fields.each(&block) end |
#freeze ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/async/http/headers.rb', line 31 def freeze return if frozen? @indexed = to_h super end |
#include?(key) ⇒ Boolean
43 44 45 |
# File 'lib/async/http/headers.rb', line 43 def include? key self[key] != nil end |
#to_h ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/async/http/headers.rb', line 82 def to_h @fields.inject({}) do |hash, (key, value)| key = key.downcase if current_value = hash[key] hash[key] = Array(current_value) << value else hash[key] = value end hash end end |