Class: Simple::HTTP::Headers

Inherits:
Hash
  • Object
show all
Defined in:
lib/simple/http/headers.rb

Instance Method Summary collapse

Constructor Details

#initialize(headers) ⇒ Headers

Returns a new instance of Headers.



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/simple/http/headers.rb', line 2

def initialize(headers)
  case headers
  when Net::HTTPHeader
    headers.each_name.map do |name|
      values = headers.get_fields(name)
      self[name] = values.length == 1 ? values.first : values
    end
  else
    headers.each do |name, value|
      self[name] = value
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
# File 'lib/simple/http/headers.rb', line 16

def [](key)
  super key.downcase
end

#value(key) ⇒ Object



20
21
22
# File 'lib/simple/http/headers.rb', line 20

def value(key)
  values(key)&.first
end

#values(key) ⇒ Object



24
25
26
27
# File 'lib/simple/http/headers.rb', line 24

def values(key)
  v = self[key]
  Array(v) if v
end