protocol-http 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f51612225b6b4d55c2468ad41603450e620bb2a01d675a330c51ea412d91ecf
4
- data.tar.gz: d3b907303a51a1a12961342dc9feb5f19f76c75092c26dd4b6224d494d0d9e91
3
+ metadata.gz: 15b85751f04e7fa40248ed85dc937f2c33cd787602ab98f2794538a0cec1c344
4
+ data.tar.gz: 9a9b91043c2116ed6ef940b5900104a89ea4392ec00f625d59f85d8173302afe
5
5
  SHA512:
6
- metadata.gz: c0a8210b3174cdd5ff2a1da7ce76aea33754521fdc34ddc87b82f83feb7d57bf189b7cad76c201155c6f681722b97757596db737f7b399436b8a4fa565e6fed7
7
- data.tar.gz: 3a3a098e8c021fb38bdca588cb62d9b4122d47eeffa022809b39dbef8013ffff4506a3b0985da0320501869313bc8e29ac9097c66998ab38d45a376da8b7019f
6
+ metadata.gz: 644d4523b67479376df035a027ef38e4f52d2010336ca5890b7a34ace8444500f96c7e74895989ba959d8ca8f66185353b6793b107372589958497e19bc236e5
7
+ data.tar.gz: 4cc9eadf0db696beee04516b7a03f1363c93c962220dd1307af8d54fd4b2766063ade1cb9a472a6abf9b57a8192ad1b75bf0dff32aeb2343c4637068e6b1609f
@@ -20,7 +20,9 @@
20
20
 
21
21
  module Protocol
22
22
  module HTTP
23
+ # Headers are an array of key-value pairs. Some header keys represent multiple values.
23
24
  class Headers
25
+ # Split by commas.
24
26
  class Split < Array
25
27
  COMMA = /\s*,\s*/
26
28
 
@@ -37,6 +39,7 @@ module Protocol
37
39
  end
38
40
  end
39
41
 
42
+ # Split by newline charaters.
40
43
  class Multiple < Array
41
44
  def initialize(value)
42
45
  super()
@@ -63,7 +66,7 @@ module Protocol
63
66
  if indexed
64
67
  @indexed = indexed.dup
65
68
  else
66
- @indexed = self.to_h
69
+ @indexed = nil
67
70
  end
68
71
  end
69
72
 
@@ -71,12 +74,14 @@ module Protocol
71
74
  self.class.new(@fields, @indexed)
72
75
  end
73
76
 
77
+ # An array of `[key, value]` pairs.
74
78
  attr :fields
75
79
 
76
80
  def freeze
77
81
  return if frozen?
78
82
 
79
- @indexed = to_h
83
+ # Generate @indexed
84
+ self.to_h
80
85
 
81
86
  super
82
87
  end
@@ -93,21 +98,22 @@ module Protocol
93
98
  self[key] != nil
94
99
  end
95
100
 
96
- def slice!(keys)
97
- _, @fields = @fields.partition do |field|
101
+ def extract(keys)
102
+ deleted, @fields = @fields.partition do |field|
98
103
  keys.include?(field.first.downcase)
99
104
  end
100
105
 
101
- keys.each do |key|
102
- @indexed.delete(key)
106
+ if @indexed
107
+ keys.each do |key|
108
+ @indexed.delete(key)
109
+ end
103
110
  end
104
111
 
105
- return self
112
+ return deleted
106
113
  end
107
114
 
108
- def slice(keys)
109
- self.dup.slice!(keys)
110
- end
115
+ # This is deprecated.
116
+ alias slice! extract
111
117
 
112
118
  def add(key, value)
113
119
  self[key] = value
@@ -129,7 +135,9 @@ module Protocol
129
135
  # @param key [String] The header key.
130
136
  # @param value The header value.
131
137
  def []= key, value
132
- merge_into(@indexed, key.downcase, value)
138
+ if @indexed
139
+ merge_into(@indexed, key.downcase, value)
140
+ end
133
141
 
134
142
  @fields << [key, value]
135
143
  end
@@ -164,11 +172,23 @@ module Protocol
164
172
 
165
173
  # Delete all headers with the given key, and return the merged value.
166
174
  def delete(key)
167
- _, @fields = @fields.partition do |field|
175
+ deleted, @fields = @fields.partition do |field|
168
176
  field.first.downcase == key
169
177
  end
170
178
 
171
- return @indexed.delete(key)
179
+ if @indexed
180
+ return @indexed.delete(key)
181
+ elsif policy = MERGE_POLICY[key]
182
+ (key, value), *tail = deleted
183
+ merged = policy.new(value)
184
+
185
+ tail.each{|k,v| merged << v}
186
+
187
+ return merged
188
+ else
189
+ key, value = deleted.last
190
+ return value
191
+ end
172
192
  end
173
193
 
174
194
  protected def merge_into(hash, key, value)
@@ -179,25 +199,28 @@ module Protocol
179
199
  hash[key] = policy.new(value)
180
200
  end
181
201
  else
182
- raise ArgumentError, "Header #{key} can only be set once!" if hash.include?(key)
183
-
184
202
  # We can't merge these, we only expose the last one set.
185
203
  hash[key] = value
186
204
  end
187
205
  end
188
206
 
189
207
  def [] key
190
- @indexed[key]
208
+ to_h[key]
191
209
  end
192
210
 
211
+ # A hash table of `{key, policy[key].map(values)}`
193
212
  def to_h
194
- @fields.inject({}) do |hash, (key, value)|
213
+ @indexed ||= @fields.inject({}) do |hash, (key, value)|
195
214
  merge_into(hash, key.downcase, value)
196
215
 
197
216
  hash
198
217
  end
199
218
  end
200
219
 
220
+ def inspect
221
+ "#<#{self.class} #{@fields.inspect}>"
222
+ end
223
+
201
224
  def == other
202
225
  if other.is_a? Hash
203
226
  to_h == other
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Protocol
22
22
  module HTTP
23
- VERSION = "0.2.0"
23
+ VERSION = "0.3.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-08 00:00:00.000000000 Z
11
+ date: 2019-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: covered
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.0.2
111
+ rubygems_version: 3.0.3
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Provides abstractions to handle HTTP protocols.