Class: Browser::HTTP::Headers

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
opal/browser/http/headers.rb

Overview

Represents HTTP headers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeaders

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

#lengthInteger (readonly)

Returns the number of headers.

Returns:

  • (Integer)

    the number of headers


104
105
106
# File 'opal/browser/http/headers.rb', line 104

def length
  @hash.length
end

Class Method Details

.[](hash) ⇒ Object

Create Browser::HTTP::Headers from a hash.

Parameters:


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

.parse(string) ⇒ Headers

Parse HTTP headers from a string.

Parameters:

  • string (String)

    the whole HTTP headers response

Returns:


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

#<<(header) ⇒ self Also known as: push

Push a header.

Parameters:

  • header (Header)

    the header to push

Returns:

  • (self)

81
82
83
84
85
# File 'opal/browser/http/headers.rb', line 81

def <<(header)
  @hash[header.name.downcase] = header

  self
end

#[](name) ⇒ String

Get the value of a header.

Parameters:

  • name (String)

    the name of the header

Returns:

  • (String)

    the value of the header


62
63
64
# File 'opal/browser/http/headers.rb', line 62

def [](name)
  @hash[name.downcase]
end

#[]=(name, value) ⇒ Object

Set a value for the header.

Parameters:

  • name (String)

    the name of the header

  • value (String)

    the value of 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

#clearObject


37
38
39
# File 'opal/browser/http/headers.rb', line 37

def clear
  @hash.clear
end

#each {|name, value| ... } ⇒ self

Enumerate over the headers.

Yield Parameters:

  • name (String)

    the name of the header

  • value (String)

    the value of the header

Returns:

  • (self)

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

#merge!(other) ⇒ self

Merge in place other headers.

Parameters:

Returns:

  • (self)

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