Class: Dispatcher::HTTPHeaderHash

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

Overview

Defines a Hash of HTTP headers.

Keys are case-insensitive, and converted to ‘pretty’ keys e.g. ‘CoNtEnT-TYPE’ would be ‘Content-Type’

This class does not parse for valid HTTP header fields, so you may recieve errors from your webserver if a malformed header is passed.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ HTTPHeaderHash

Returns a new instance of HTTPHeaderHash.



270
271
272
273
274
# File 'lib/dispatcher.rb', line 270

def initialize(*values)
  values.each do |field, value|
    self[field] = value
  end
end

Class Method Details

.pretty_field(field) ⇒ Object

Converts the given key string into a ‘pretty’ HTTP header field



277
278
279
280
281
282
283
284
285
# File 'lib/dispatcher.rb', line 277

def self.pretty_field(field)
  chunks = field.gsub('_', '-').split('-')
  
  chunks.collect! do |val| 
    val.capitalize
  end
  
  return chunks.join('-')
end

Instance Method Details

#[](field) ⇒ Object

Returns the value stored for key.



293
294
295
# File 'lib/dispatcher.rb', line 293

def [](field)
  super(HTTPHeaderHash.pretty_field(field.to_s))
end

#[]=(field, value) ⇒ Object

Sets the key to value.



288
289
290
# File 'lib/dispatcher.rb', line 288

def []=(field, value)
  super(HTTPHeaderHash.pretty_field(field.to_s), value.to_s)
end