Class: Rack::Headers

Inherits:
Hash
  • Object
show all
Defined in:
lib/rack/headers.rb

Overview

Rack::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 collapse

KNOWN_HEADERS =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*items) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object



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

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

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



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

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

#assoc(key) ⇒ Object



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

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

#compare_by_identityObject

Raises:

  • (TypeError)


123
124
125
# 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) ⇒ Object



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

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

#dig(key, *a) ⇒ Object



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

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

#except(*a) ⇒ Object

:nocov:



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

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

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



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

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

#fetch_values(*a) ⇒ Object



140
141
142
# 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?

Returns:

  • (Boolean)


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

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

#invertObject



151
152
153
154
155
# File 'lib/rack/headers.rb', line 151

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

#merge(hash, &block) ⇒ Object



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

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

#reject(&block) ⇒ Object



161
162
163
164
165
# File 'lib/rack/headers.rb', line 161

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

#replace(hash) ⇒ Object



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

def replace(hash)
  clear
  update(hash)
end

#select(&block) ⇒ Object



172
173
174
175
176
# File 'lib/rack/headers.rb', line 172

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

#slice(*a) ⇒ Object

:nocov:



205
206
207
208
209
# 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

#to_procObject



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

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

#transform_keys(&block) ⇒ Object



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

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

#transform_keys!Object



215
216
217
218
219
220
221
# 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) ⇒ Object



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

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

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



186
187
188
189
190
191
192
193
194
195
# 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) ⇒ Object



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

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