Class: Apia::RequestHeaders

Inherits:
Object
  • Object
show all
Defined in:
lib/apia/request_headers.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers) ⇒ RequestHeaders

Returns a new instance of RequestHeaders.



6
7
8
# File 'lib/apia/request_headers.rb', line 6

def initialize(headers)
  @headers = headers
end

Class Method Details

.create_from_request(request) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/apia/request_headers.rb', line 28

def create_from_request(request)
  hash = request.each_header.with_object({}) do |(key, value), inner_hash|
    next unless key =~ /\AHTTP_(\w+)\z/

    name = Regexp.last_match[1]

    inner_hash[name] = value
  end
  new(hash)
end

.make_key(key) ⇒ Object



24
25
26
# File 'lib/apia/request_headers.rb', line 24

def make_key(key)
  key.gsub('-', '_').upcase
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
# File 'lib/apia/request_headers.rb', line 14

def [](key)
  fetch(key)
end

#[]=(key, value) ⇒ Object



18
19
20
# File 'lib/apia/request_headers.rb', line 18

def []=(key, value)
  @headers[self.class.make_key(key)] = value
end

#fetch(key, default = nil) ⇒ Object



10
11
12
# File 'lib/apia/request_headers.rb', line 10

def fetch(key, default = nil)
  @headers[self.class.make_key(key)] || default
end