Class: Faraday::Utils::Headers

Inherits:
HeaderHash
  • Object
show all
Defined in:
lib/faraday/utils.rb

Constant Summary collapse

KeyMap =

symbol -> string mapper + cache

Hash.new do |map, key|
  map[key] = if key.respond_to?(:to_str) then key
  else
    key.to_s.split('_').            # :user_agent => %w(user agent)
      each { |w| w.capitalize! }.   # => %w(User Agent)
      join('-')                     # => "User-Agent"
  end
end

Instance Method Summary collapse

Instance Method Details

#[](k) ⇒ Object



22
23
24
# File 'lib/faraday/utils.rb', line 22

def [](k)
  super(KeyMap[k])
end

#[]=(k, v) ⇒ Object



26
27
28
29
30
# File 'lib/faraday/utils.rb', line 26

def []=(k, v)
  # join multiple values with a comma
  v = v.to_ary.join(', ') if v.respond_to? :to_ary
  super(KeyMap[k], v)
end

#parse(header_string) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/faraday/utils.rb', line 34

def parse(header_string)
  return unless header_string && !header_string.empty?
  header_string.split(/\r\n/).
    tap  { |a| a.shift if a.first.index('HTTP/') == 0 }. # drop the HTTP status line
    map  { |h| h.split(/:\s+/, 2) }.reject { |(k, v)| k.nil? }. # split key and value, ignore blank lines
    each { |key, value|
      # join multiple values with a comma
      if self[key] then self[key] << ', ' << value
      else self[key] = value
      end
    }
end