Class: Faraday::Utils::Headers
- Inherits:
-
Hash
- Object
- Hash
- Faraday::Utils::Headers
- 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
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #delete(k) ⇒ Object
- #fetch(k, *args, &block) ⇒ Object
- #include?(k) ⇒ Boolean (also: #has_key?, #member?, #key?)
-
#initialize(hash = nil) ⇒ Headers
constructor
A new instance of Headers.
-
#initialize_copy(other) ⇒ Object
on dup/clone, we need to duplicate @names hash.
- #initialize_names ⇒ Object
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object (also: #update)
- #parse(header_string) ⇒ Object
- #replace(other) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(hash = nil) ⇒ Headers
Returns a new instance of Headers.
19 20 21 22 23 |
# File 'lib/faraday/utils.rb', line 19 def initialize(hash = nil) super() @names = {} self.update(hash || {}) end |
Class Method Details
.allocate ⇒ Object
13 14 15 16 17 |
# File 'lib/faraday/utils.rb', line 13 def self.allocate new_self = super new_self.initialize_names new_self end |
.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
51 52 53 54 |
# File 'lib/faraday/utils.rb', line 51 def [](k) k = KeyMap[k] super(k) || super(@names[k.downcase]) end |
#[]=(k, v) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/faraday/utils.rb', line 56 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
70 71 72 73 74 75 76 |
# File 'lib/faraday/utils.rb', line 70 def delete(k) k = KeyMap[k] if k = @names[k.downcase] @names.delete k.downcase super(k) end end |
#fetch(k, *args, &block) ⇒ Object
64 65 66 67 68 |
# File 'lib/faraday/utils.rb', line 64 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?
78 79 80 |
# File 'lib/faraday/utils.rb', line 78 def include?(k) @names.include? k.downcase end |
#initialize_copy(other) ⇒ Object
on dup/clone, we need to duplicate @names hash
30 31 32 33 |
# File 'lib/faraday/utils.rb', line 30 def initialize_copy(other) super @names = other.names.dup end |
#initialize_names ⇒ Object
25 26 27 |
# File 'lib/faraday/utils.rb', line 25 def initialize_names @names = {} end |
#merge(other) ⇒ Object
92 93 94 95 |
# File 'lib/faraday/utils.rb', line 92 def merge(other) hash = dup hash.merge! other end |
#merge!(other) ⇒ Object Also known as: update
86 87 88 89 |
# File 'lib/faraday/utils.rb', line 86 def merge!(other) other.each { |k, v| self[k] = v } self end |
#parse(header_string) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/faraday/utils.rb', line 106 def parse(header_string) return unless header_string && !header_string.empty? headers = header_string.split(/\r\n/) # Find the last set of response headers. start_index = headers.rindex { |x| x.match(/^HTTP\//) } || 0 last_response = headers.slice(start_index, headers.size) last_response. 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
97 98 99 100 101 102 |
# File 'lib/faraday/utils.rb', line 97 def replace(other) clear @names.clear self.update other self end |
#to_hash ⇒ Object
104 |
# File 'lib/faraday/utils.rb', line 104 def to_hash() ::Hash.new.update(self) end |