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.



88
89
90
91
92
# File 'lib/azure/core/http/http_response.rb', line 88

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.



99
100
101
# File 'lib/azure/core/http/http_response.rb', line 99

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.



111
112
113
114
115
116
117
# File 'lib/azure/core/http/http_response.rb', line 111

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