Class: Hurley::Header

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hurley/header.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial = nil) ⇒ Header

Returns a new instance of Header.



5
6
7
8
# File 'lib/hurley/header.rb', line 5

def initialize(initial = nil)
  @hash = {}
  update(initial) if initial
end

Class Method Details

.add_canonical_key(*canonicals) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/hurley/header.rb', line 63

def self.add_canonical_key(*canonicals)
  canonicals.each do |canonical|
    canonical.freeze
    KEYS[canonical] = canonical
    shortcut = canonical.downcase
    KEYS[shortcut.freeze] = canonical
    KEYS[shortcut.tr(DASH, UNDERSCORE).to_sym] = canonical
  end
end

.canonical(input) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/hurley/header.rb', line 54

def self.canonical(input)
  KEYS[input] || begin
    key = input.to_s.tr(UNDERSCORE, DASH)
    key.downcase!
    key.gsub!(/(\A|\-)(\S)/) { |s| s.upcase! ; s }
    key
  end
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/hurley/header.rb', line 17

def [](key)
  @hash[self.class.canonical(key)]
end

#[]=(key, value) ⇒ Object



21
22
23
# File 'lib/hurley/header.rb', line 21

def []=(key, value)
  @hash[self.class.canonical(key)] = value.to_s
end

#delete(key) ⇒ Object



29
30
31
# File 'lib/hurley/header.rb', line 29

def delete(key)
  @hash.delete(self.class.canonical(key))
end

#dupObject



39
40
41
# File 'lib/hurley/header.rb', line 39

def dup
  self.class.new(@hash.dup)
end

#inspectObject



47
48
49
50
51
52
# File 'lib/hurley/header.rb', line 47

def inspect
  "#<%s %s>" % [
    self.class.name,
    @hash.inspect,
  ]
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/hurley/header.rb', line 25

def key?(key)
  @hash.key?(self.class.canonical(key))
end

#to_hashObject



43
44
45
# File 'lib/hurley/header.rb', line 43

def to_hash
  @hash
end

#update(hash) ⇒ Object



33
34
35
36
37
# File 'lib/hurley/header.rb', line 33

def update(hash)
  hash.each do |key, value|
    self[key] = value
  end
end