Class: Songkick::Transport::Headers

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/songkick/transport/headers.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Headers

Returns a new instance of Headers.



19
20
21
22
23
24
25
# File 'lib/songkick/transport/headers.rb', line 19

def initialize(hash = {})
  @hash = {}
  hash.each do |key, value|
    next if value.nil?
    @hash[self.class.normalize(key)] = value
  end
end

Class Method Details

.new(hash) ⇒ Object



7
8
9
10
# File 'lib/songkick/transport/headers.rb', line 7

def self.new(hash)
  return hash if self === hash
  super
end

.normalize(header_name) ⇒ Object



12
13
14
15
16
17
# File 'lib/songkick/transport/headers.rb', line 12

def self.normalize(header_name)
  header_name.
      gsub(/^HTTP_/, '').gsub('_', '-').
      downcase.
      gsub(/(^|-)([a-z])/) { $1 + $2.upcase }
end

Instance Method Details

#[](header_name) ⇒ Object



31
32
33
# File 'lib/songkick/transport/headers.rb', line 31

def [](header_name)
  @hash[self.class.normalize(header_name)]
end

#[]=(header_name, value) ⇒ Object



35
36
37
# File 'lib/songkick/transport/headers.rb', line 35

def []=(header_name, value)
  @hash[self.class.normalize(header_name)] = value
end

#each(&block) ⇒ Object



27
28
29
# File 'lib/songkick/transport/headers.rb', line 27

def each(&block)
  @hash.each(&block)
end

#merge(hash) ⇒ Object



39
40
41
42
43
# File 'lib/songkick/transport/headers.rb', line 39

def merge(hash)
  headers = self.class.new(to_hash)
  hash.each { |k,v| headers[k] = v }
  headers
end

#to_hashObject



45
46
47
# File 'lib/songkick/transport/headers.rb', line 45

def to_hash
  @hash.dup
end