123456789_123456789_123456789_123456789_123456789_

Class: Rack::Headers

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Hash
Instance Chain:
self, Hash
Inherits: Hash
  • Object
Defined in: lib/rack/headers.rb

Overview

Headers is a Hash subclass that downcases all keys. It’s designed to be used by rack applications that don’t implement the ::Rack 3 SPEC (by using non-lowercase response header keys), automatically handling the downcasing of keys.

Constant Summary

Class Method Summary

Instance Method Summary

Class Method Details

.[](*items)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 91

def self.[](*items)
  if items.length % 2 != 0
    if items.length == 1 && items.first.is_a?(Hash)
      new.merge!(items.first)
    else
      raise ArgumentError, "odd number of arguments for Rack::Headers"
    end
  else
    hash = new
    loop do
      break if items.length == 0
      key = items.shift
      value = items.shift
      hash[key] = value
    end
    hash
  end
end

Instance Method Details

#[](key)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 110

def [](key)
  super(downcase_key(key))
end

#[]=(key, value) Also known as: #store

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 114

def []=(key, value)
  super(KNOWN_HEADERS[key] || key.downcase.freeze, value)
end

#assoc(key)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 119

def assoc(key)
  super(downcase_key(key))
end

#compare_by_identity

Raises:

  • (TypeError)
[ GitHub ]

  
# File 'lib/rack/headers.rb', line 123

def compare_by_identity
  raise TypeError, "Rack::Headers cannot compare by identity, use regular Hash"
end

#delete(key)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 127

def delete(key)
  super(downcase_key(key))
end

#dig(key, *a)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 131

def dig(key, *a)
  super(downcase_key(key), *a)
end

#downcase_key(key) (private)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 234

def downcase_key(key)
  key.is_a?(String) ? KNOWN_HEADERS[key] || key.downcase : key
end

#except(*a)

:nocov:

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 227

def except(*a)
  super(*a.map!{|key| downcase_key(key)})
end

#fetch(key, *default, &block)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 135

def fetch(key, *default, &block)
  key = downcase_key(key)
  super
end

#fetch_values(*a)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 140

def fetch_values(*a)
  super(*a.map!{|key| downcase_key(key)})
end

#has_key?(key) ⇒ Boolean Also known as: #include?, #key?, #member?

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 144

def has_key?(key)
  super(downcase_key(key))
end

#include?(key)

Alias for #has_key?.

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 147

alias include? has_key?

#invert

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 151

def invert
  hash = self.class.new
  each{|key, value| hash[value] = key}
  hash
end

#key?(key)

Alias for #has_key?.

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 148

alias key? has_key?

#member?(key)

Alias for #has_key?.

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 149

alias member? has_key?

#merge(hash, &block)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 157

def merge(hash, &block)
  dup.merge!(hash, &block)
end

#merge!(hash, &block)

Alias for #update.

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 196

alias merge! update

#reject(&block)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 161

def reject(&block)
  hash = dup
  hash.reject!(&block)
  hash
end

#replace(hash)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 167

def replace(hash)
  clear
  update(hash)
end

#select(&block)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 172

def select(&block)
  hash = dup
  hash.select!(&block)
  hash
end

#slice(*a)

:nocov:

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 205

def slice(*a)
  h = self.class.new
  a.each{|k| h[k] = self[k] if has_key?(k)}
  h
end

#store(key, value)

Alias for #[]=.

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 117

alias store []=

#to_proc

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 178

def to_proc
  lambda{|x| self[x]}
end

#transform_keys(&block)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 211

def transform_keys(&block)
  dup.transform_keys!(&block)
end

#transform_keys!

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 215

def transform_keys!
  hash = self.class.new
  each do |k, v|
    hash[yield k] = v
  end
  replace(hash)
end

#transform_values(&block)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 182

def transform_values(&block)
  dup.transform_values!(&block)
end

#update(hash, &block) Also known as: #merge!

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 186

def update(hash, &block)
  hash.each do |key, value|
    self[key] = if block_given? && include?(key)
      block.call(key, self[key], value)
    else
      value
    end
  end
  self
end

#values_at(*keys)

[ GitHub ]

  
# File 'lib/rack/headers.rb', line 198

def values_at(*keys)
  keys.map{|key| self[key]}
end