Class: Faraday::Utils::Headers

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

Overview

Adapted from Rack::Utils::HeaderHash

Constant Summary collapse

KeyMap =

symbol -> string mapper + cache

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ Headers

Returns a new instance of Headers.



13
14
15
16
17
# File 'lib/faraday/utils.rb', line 13

def initialize(hash = nil)
  super()
  @names = {}
  self.update(hash || {})
end

Class Method Details

.from(value) ⇒ Object



9
10
11
# File 'lib/faraday/utils.rb', line 9

def self.from(value)
  new(value)
end

Instance Method Details

#[](k) ⇒ Object



41
42
43
44
# File 'lib/faraday/utils.rb', line 41

def [](k)
  k = KeyMap[k]
  super(k) || super(@names[k.downcase])
end

#[]=(k, v) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/faraday/utils.rb', line 46

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

#delete(k) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/faraday/utils.rb', line 60

def delete(k)
  k = KeyMap[k]
  if k = @names[k.downcase]
    @names.delete k.downcase
    super(k)
  end
end

#encode_with(coder) ⇒ Object



115
116
117
# File 'lib/faraday/utils.rb', line 115

def encode_with(coder)
  coder['names'] = @names
end

#fetch(k, *args, &block) ⇒ Object



54
55
56
57
58
# File 'lib/faraday/utils.rb', line 54

def fetch(k, *args, &block)
  k = KeyMap[k]
  key = @names.fetch(k.downcase, k)
  super(key, *args, &block)
end

#include?(k) ⇒ Boolean Also known as: has_key?, member?, key?

Returns:

  • (Boolean)


68
69
70
# File 'lib/faraday/utils.rb', line 68

def include?(k)
  @names.include? k.downcase
end

#init_with(coder) ⇒ Object



111
112
113
# File 'lib/faraday/utils.rb', line 111

def init_with(coder)
  @names = coder['names']
end

#initialize_copy(other) ⇒ Object

on dup/clone, we need to duplicate @names hash



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

def initialize_copy(other)
  super
  @names = other.names.dup
end

#merge(other) ⇒ Object



82
83
84
85
# File 'lib/faraday/utils.rb', line 82

def merge(other)
  hash = dup
  hash.merge! other
end

#merge!(other) ⇒ Object Also known as: update



76
77
78
79
# File 'lib/faraday/utils.rb', line 76

def merge!(other)
  other.each { |k, v| self[k] = v }
  self
end

#parse(header_string) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/faraday/utils.rb', line 96

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 { |p| p[0].nil? }. # split key and value, ignore blank lines
    each { |key, value|
      # join multiple values with a comma
      if self[key]
        self[key] << ', ' << value
      else
        self[key] = value
      end
    }
end

#replace(other) ⇒ Object



87
88
89
90
91
92
# File 'lib/faraday/utils.rb', line 87

def replace(other)
  clear
  @names.clear
  self.update other
  self
end

#to_hashObject



94
# File 'lib/faraday/utils.rb', line 94

def to_hash() ::Hash.new.update(self) end