Class: Azure::Core::Http::HttpResponse::HeaderHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/azure/core/http/http_response.rb

Overview

Since HTTP Headers are case insensitive, this class will normalize them to lowercase. This also wraps Net::HTTPResponse headers by returning their values as strings, not arrays, by selecting the first value from the array.

Instance Method Summary collapse

Constructor Details

#initialize(headers) ⇒ HeaderHash

Public: Initialize the header hash.

headers - A Hash of headers as returned from Net::HTTPResponse#to_hash.



81
82
83
84
85
# File 'lib/azure/core/http/http_response.rb', line 81

def initialize(headers)
  super
  headers = Hash[headers.map { |k,v| [k.downcase.freeze, v.first.freeze] }]
  replace(headers)
end

Instance Method Details

#[](header) ⇒ Object

Public: Get a header’s value or nil if it’s not present.

header - A string with the header’s name.

Returns a String or nil.



92
93
94
# File 'lib/azure/core/http/http_response.rb', line 92

def [](header)
  super(header.downcase)
end

#fetch(header, *default, &block) ⇒ Object

Public: Get a header’s value or a specified default.

header - A string with the header’s name. default - A default value.

Yields a block where you can specify the default value.

Returns a String, or the specified default.



104
105
106
107
108
109
110
# File 'lib/azure/core/http/http_response.rb', line 104

def fetch(header, *default, &block)
  if (args = default.size) > 1
    raise ArgumentError, "wrong number of arguments(#{args + 1} for 2)"
  end

  super(header.downcase, *default, &block)
end