Adapted from Rack::Utils::HeaderHash
symbol -> string mapper + cache
# File lib/faraday/utils.rb, line 13 def self.allocate new_self = super new_self.initialize_names new_self end
# File lib/faraday/utils.rb, line 9 def self.from(value) new(value) end
# File lib/faraday/utils.rb, line 19 def initialize(hash = nil) super() @names = {} self.update(hash || {}) end
# File lib/faraday/utils.rb, line 51 def [](k) k = KeyMap[k] super(k) || super(@names[k.downcase]) end
# File lib/faraday/utils.rb, line 56 def []=(k, v) k = KeyMap[k] k = (@names[k.downcase] ||= k) # join multiple values with a comma v = v.to_ary.join(', ') if v.respond_to? :to_ary super(k, v) end
# File lib/faraday/utils.rb, line 70 def delete(k) k = KeyMap[k] if k = @names[k.downcase] @names.delete k.downcase super(k) end end
# File lib/faraday/utils.rb, line 64 def fetch(k, *args, &block) k = KeyMap[k] key = @names.fetch(k.downcase, k) super(key, *args, &block) end
# File lib/faraday/utils.rb, line 78 def include?(k) @names.include? k.downcase end
on dup/clone, we need to duplicate @names hash
# File lib/faraday/utils.rb, line 30 def initialize_copy(other) super @names = other.names.dup end
# File lib/faraday/utils.rb, line 25 def initialize_names @names = {} end
# File lib/faraday/utils.rb, line 92 def merge(other) hash = dup hash.merge! other end
# File lib/faraday/utils.rb, line 86 def merge!(other) other.each { |k, v| self[k] = v } self end
# File lib/faraday/utils.rb, line 106 def parse(header_string) return unless header_string && !header_string.empty? headers = header_string.split(/\r\n/) # Find the last set of response headers. start_index = headers.rindex { |x| x.match(/^HTTP\//) } || 0 last_response = headers.slice(start_index, headers.size) last_response. tap { |a| a.shift if a.first.index('HTTP/') == 0 }. # drop the HTTP status line map { |h| h.split(/:\s*/, 2) }.reject { |p| p[0].nil? }. # split key and value, ignore blank lines each { |key, value| # join multiple values with a comma if self[key] self[key] << ', ' << value else self[key] = value end } end
# File lib/faraday/utils.rb, line 97 def replace(other) clear @names.clear self.update other self end
# File lib/faraday/utils.rb, line 104 def to_hash() ::Hash.new.update(self) end
# File lib/faraday/utils.rb, line 130 def names @names end