Class: Browser::HTTP::Headers
- Includes:
- Enumerable
- Defined in:
- opal/browser/http/headers.rb
Overview
Represents HTTP headers.
Instance Attribute Summary collapse
-
#length ⇒ Integer
readonly
The number of headers.
Class Method Summary collapse
-
.[](hash) ⇒ Object
Create Headers from a hash.
-
.parse(string) ⇒ Headers
Parse HTTP headers from a string.
Instance Method Summary collapse
-
#<<(header) ⇒ self
(also: #push)
Push a header.
-
#[](name) ⇒ String
Get the value of a header.
-
#[]=(name, value) ⇒ Object
Set a value for the header.
-
#clear ⇒ Object
Clear the Headers.
-
#each {|name, value| ... } ⇒ self
Enumerate over the headers.
-
#initialize ⇒ Headers
constructor
Create an empty Headers.
-
#merge!(other) ⇒ self
Merge in place other headers.
Constructor Details
permalink #initialize ⇒ Headers
Create an empty Browser::HTTP::Headers.
32 33 34 |
# File 'opal/browser/http/headers.rb', line 32 def initialize @hash = Hash.new end |
Instance Attribute Details
permalink #length ⇒ Integer (readonly)
Returns the number of headers.
104 105 106 |
# File 'opal/browser/http/headers.rb', line 104 def length @hash.length end |
Class Method Details
permalink .[](hash) ⇒ Object
Create Browser::HTTP::Headers from a hash.
19 20 21 22 23 24 25 26 27 |
# File 'opal/browser/http/headers.rb', line 19 def self.[](hash) result = new hash.each {|name, value| result[name] = value } result end |
permalink .parse(string) ⇒ Headers
Parse HTTP headers from a string.
12 13 14 |
# File 'opal/browser/http/headers.rb', line 12 def self.parse(string) self[string.lines.map { |l| l.chomp.split(/\s*:\s*/) }] end |
Instance Method Details
permalink #<<(header) ⇒ self Also known as: push
Push a header.
81 82 83 84 85 |
# File 'opal/browser/http/headers.rb', line 81 def <<(header) @hash[header.name.downcase] = header self end |
permalink #[](name) ⇒ String
Get the value of a header.
62 63 64 |
# File 'opal/browser/http/headers.rb', line 62 def [](name) @hash[name.downcase] end |
permalink #[]=(name, value) ⇒ Object
Set a value for the header.
70 71 72 73 74 |
# File 'opal/browser/http/headers.rb', line 70 def []=(name, value) header = Header.new(name, value) @hash[name.downcase] = header end |
permalink #clear ⇒ Object
Clear the Browser::HTTP::Headers.
37 38 39 |
# File 'opal/browser/http/headers.rb', line 37 def clear @hash.clear end |
permalink #each {|name, value| ... } ⇒ self
Enumerate over the headers.
47 48 49 50 51 52 53 54 55 |
# File 'opal/browser/http/headers.rb', line 47 def each(&block) return enum_for :each unless block @hash.each {|_, header| block.call [header.name, header.value] } self end |
permalink #merge!(other) ⇒ self
Merge in place other headers.
94 95 96 97 98 99 100 |
# File 'opal/browser/http/headers.rb', line 94 def merge!(other) other.each {|name, value| self[name] = value } self end |