class Stripe::StripeResponseHeaders
case-insensitive access to header names and flattening of header values.
‘Net::HTTPResponse` object while still getting some of its benefits like
mainly exists so that we don’t need to expose the entire
Headers provides an access wrapper to an API response’s header data. It
def self.from_net_http(resp)
def self.from_net_http(resp) new(resp.to_hash) end
def [](name)
def [](name) values = @hash[name.downcase] warn("Duplicate header values for `#{name}`; returning only first") if values && values.count > 1 values ? values.first : nil end
def initialize(hash)
repeated multiple times. Using `#[]` will collapse values down to just
`#to_hash` on a `Net::HTTPResponse` object because headers can be
header values. This is the default format generated by calling
`hash` is expected to be a hash mapping header names to arrays of
def initialize(hash) if !hash.is_a?(Hash) || !hash.keys.all? { |n| n.is_a?(String) } || !hash.values.all? { |a| a.is_a?(Array) } || !hash.values.all? { |a| a.all? { |v| v.is_a?(String) } } raise ArgumentError, "expect hash to be a map of string header names to arrays of " \ "header values" end @hash = {} # This shouldn't be strictly necessary because `Net::HTTPResponse` will # produce a hash with all headers downcased, but do it anyway just in # case an object of this class was constructed manually. # # Also has the effect of duplicating the hash, which is desirable for a # little extra object safety. hash.each do |k, v| @hash[k.downcase] = v end end