redhead 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.md +1 -117
- data/lib/redhead.rb +0 -13
- data/lib/redhead/header.rb +6 -38
- data/lib/redhead/header_set.rb +8 -38
- data/lib/redhead/redhead_string.rb +3 -5
- data/redhead.gemspec +1 -1
- data/test/header_set_spec.rb +3 -147
- data/test/header_spec.rb +4 -131
- data/test/redhead_string_spec.rb +0 -20
- metadata +53 -50
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -165,8 +165,6 @@ There are conventions followed in creating a Redhead string and modifying certai
|
|
165
165
|
|
166
166
|
By default, newly added header field names (i.e., the keys) will become capitalised and hyphen-separated to create their raw header name, so that the symbolic header name `:if_modified_since` becomes the raw header name `If-Modified-Since`. A similar process also happens when turning a string into a Redhead string (in reverse), and when using the default behaviour of `to_s`. To keep symbol names pleasant, by default, anything which isn't `A-Z`, `a-z` or `_` is converted into a `_` character. So `"Some! Long! Header! Name!"` becomes `:some_long_header_name` for accessing within Ruby.
|
167
167
|
|
168
|
-
For information on changing the formatting rules, see the section on <a href="#special_circumstances">special circumstances</a>.
|
169
|
-
|
170
168
|
## Header name memory
|
171
169
|
|
172
170
|
Original header names are remembered from the input string, and not simply created on-the-fly when using `to_s`. This is to make sure you get the same raw heading name, as a string, that you originally read in, instead of assuming you want it to be changed. Access as a Ruby object, however, is with a symbol by default.
|
@@ -336,126 +334,12 @@ The custom raw header name can also be given explicitly at creation time.
|
|
336
334
|
>> str.headers
|
337
335
|
=> { { :awesome => "quite" } }
|
338
336
|
|
339
|
-
>> str.
|
337
|
+
>> str.headers.add(:temporary, "temp", "A-Rather-Temporary-Value")
|
340
338
|
=> { :temp => "temp" }
|
341
339
|
|
342
340
|
>> str.headers.to_s
|
343
341
|
=> "Awesome-Rating: quite\nA-Rather-Temporary-Value: temp"
|
344
342
|
|
345
|
-
# Enterprise Header Solutions
|
346
|
-
|
347
|
-
As mentioned, Redhead performs two conversions. One to produce a symbolic header name from the raw header name, and one to produce the raw header name from the symbolic header name. The symbolic -> raw process is used in `to_s!`, to force header names to be produced instead of using the header name remembered from when the header was created.
|
348
|
-
|
349
|
-
If you need to control the format of header names beyond the simple separator option given above, you can provide a block, the result of which is the name used for the raw or symbolic header name.
|
350
|
-
|
351
|
-
If that doesn't make a lot of sense on the first read, don't worry, here's some code.
|
352
|
-
|
353
|
-
>> string = "A-Header-Name: a header value\n\nContent."
|
354
|
-
=> "A-Header-Name: a header value\n\nContent."
|
355
|
-
|
356
|
-
>> str = Redhead::String.new(string) do |name|
|
357
|
-
?> name.split(/-/).join("").upcase.to_sym
|
358
|
-
?> end
|
359
|
-
=> +"Content."
|
360
|
-
|
361
|
-
>> str.headers
|
362
|
-
=> { { :AHEADERNAME => "a header value" } }
|
363
|
-
|
364
|
-
Note that this uses `Redhead::String.new` instead of `Redhead::String.[]` because of the block argument.
|
365
|
-
|
366
|
-
The above defines how symbolic headers are created in when creating the header objects. Using this approach, you can work with non-standard headers quite easily.
|
367
|
-
|
368
|
-
Note that `to_sym` is _not_ implicit, to suggest you use a pleasant symbol as the key.
|
369
|
-
|
370
|
-
It's also possible to specify the code to be used when calling `to_s`:
|
371
|
-
|
372
|
-
>> str.headers
|
373
|
-
=> { { :AHEADERNAME => "a header value" } }
|
374
|
-
|
375
|
-
>> str.headers.to_s do |name|
|
376
|
-
?> name.to_s.downcase.scan(/..?.?/).join("-").capitalize
|
377
|
-
?> end
|
378
|
-
=> "ahe-ade-rna-me: a header value"
|
379
|
-
|
380
|
-
The block to `to_s` will not modify the headers in-place, in keeping with the behaviour of the block-less `to_s`. To change how the symbolic-to-raw header name conversion works, you can do so on the object holding the headers.
|
381
|
-
|
382
|
-
>> str.headers.to_raw = lambda do |name|
|
383
|
-
?> name.to_s.downcase.scan(/..?.?/).join("-").capitalize
|
384
|
-
?> end
|
385
|
-
=> #<Proc:...>
|
386
|
-
|
387
|
-
Similarly, you can modify `to_key`. You can also change `to_raw` and `to_key` for each individual header. If no block is given for a specific header, it defaults to the block for the containing `headers` object. If nothing is given _there_, then it goes to the default.
|
388
|
-
|
389
|
-
If `to_raw(produced_key) != original_key` for all the headers in the object, then the headers are in a mismatched state. Equally, a single header is in a mismatched state if the condition fails for that header.
|
390
|
-
|
391
|
-
This can be checked with `reversible?`.
|
392
|
-
|
393
|
-
>> string = "A-Header-Name: a header value\n\nContent."
|
394
|
-
=> "A-Header-Name: a header value\n\nContent."
|
395
|
-
|
396
|
-
>> str = Redhead::String.new(string) do |name|
|
397
|
-
?> name.gsub(/-/, "").upcase.to_sym
|
398
|
-
?> end
|
399
|
-
=> +"Content."
|
400
|
-
|
401
|
-
# At this point, `to_key` is not reversible via `to_raw`
|
402
|
-
|
403
|
-
>> str.headers.reversible?
|
404
|
-
=> false
|
405
|
-
|
406
|
-
>> str = Redhead::String.new(string) do |name|
|
407
|
-
?> name.split(/-/).map { |e| e.upcase }.join("zzz").to_sym
|
408
|
-
?> end
|
409
|
-
=> +"Content."
|
410
|
-
|
411
|
-
>> str.headers
|
412
|
-
=> { { :AzzzHEADERzzzNAME => "a header value" } }
|
413
|
-
|
414
|
-
>> str.headers.reversible?
|
415
|
-
=> false
|
416
|
-
|
417
|
-
>> str.headers.to_raw = lambda do |name|
|
418
|
-
?> name.to_s.split(/zzz/).map { |e| e.capitalize }.join("-")
|
419
|
-
?> end
|
420
|
-
=> #<Proc:...>
|
421
|
-
|
422
|
-
# We can go back and forth without issue on this string
|
423
|
-
|
424
|
-
>> str.headers.reversible?
|
425
|
-
=> true
|
426
|
-
|
427
|
-
>> str.headers
|
428
|
-
=> { { :AzzzHEADERzzzzNAME => "a header value" } }
|
429
|
-
|
430
|
-
>> str.headers.to_s
|
431
|
-
=> "A-Header-Name: a header value"
|
432
|
-
|
433
|
-
Reversibility is checked by calling `reversible?` on all the headers in `str.headers`, since each header can have its own `to_key` and `to_raw` blocks. `reversible?` returning false will not raise an error or stop execution.
|
434
|
-
|
435
|
-
When creating new headers, `to_raw`, is used, meaning your custom block will be picked up and used to create the raw header as though it had been created from a string.
|
436
|
-
|
437
|
-
>> str.headers.to_raw = proc { "Genuinely" }
|
438
|
-
|
439
|
-
>> str.headers[:foo_bar] = "temp"
|
440
|
-
=> "temp"
|
441
|
-
|
442
|
-
>> temp_header = str.headers[:foo_bar]
|
443
|
-
=> { :foo_bar => "temp" }
|
444
|
-
|
445
|
-
>> temp_header.to_s
|
446
|
-
=> "Genuinely: temp"
|
447
|
-
|
448
|
-
Changing `to_raw` after-the-fact will not change the raw header name stored for the object. To force `to_raw` to be used instead of the stored value, use `to_s!`, which _always_ uses `to_raw`.
|
449
|
-
|
450
|
-
>> temp_header.to_raw = lambda { "nothing meaningful" }
|
451
|
-
=> #<Proc:...>
|
452
|
-
|
453
|
-
>> temp_header.to_s
|
454
|
-
=> "Temp-Orary-Header: temp"
|
455
|
-
|
456
|
-
>> temp_header.to_s!
|
457
|
-
=> "nothing meaningful: temp"
|
458
|
-
|
459
343
|
# TODO
|
460
344
|
|
461
345
|
Headers on different lines with the same raw name. Important for HTTP.
|
data/lib/redhead.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
# Copyright (c) 2010 Adam Prescott
|
2
|
-
# Licensed under the MIT license. See LICENSE.
|
3
|
-
|
4
1
|
module Redhead
|
5
2
|
|
6
3
|
# The character used to separate raw header names from their values.
|
@@ -20,16 +17,6 @@ module Redhead
|
|
20
17
|
|
21
18
|
# The default code to convert a given raw header name to a key.
|
22
19
|
TO_KEY = lambda { |raw| raw.split(/[^a-z_]+/i).join("_").downcase.to_sym }
|
23
|
-
|
24
|
-
# Method wrapping TO_RAW, to allow overriding for customisation.
|
25
|
-
def self.to_raw
|
26
|
-
TO_RAW
|
27
|
-
end
|
28
|
-
|
29
|
-
# Method wrapping TO_KEY, to allow overriding for customisation.
|
30
|
-
def self.to_key
|
31
|
-
TO_KEY
|
32
|
-
end
|
33
20
|
end
|
34
21
|
|
35
22
|
require "redhead/header"
|
data/lib/redhead/header.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Redhead
|
2
2
|
class Header
|
3
3
|
attr_accessor :key, :raw, :value
|
4
|
-
attr_writer :to_key, :to_raw
|
5
4
|
|
6
5
|
def initialize(key, raw, value)
|
7
6
|
@key = key
|
@@ -11,17 +10,11 @@ module Redhead
|
|
11
10
|
|
12
11
|
# Parses a string representing a header. Uses HEADER_NAME_VALUE_SEPARATOR_PATTERN
|
13
12
|
# to determine the name and value parts of the string.
|
14
|
-
|
15
|
-
# With a given block, the parsed raw header name is passed to the block and the result is
|
16
|
-
# used as the key name. If no block is given, then the default is used to create the key
|
17
|
-
# name.
|
18
|
-
def self.parse(header_string, &block)
|
13
|
+
def self.parse(header_string)
|
19
14
|
header_string =~ HEADER_NAME_VALUE_SEPARATOR_PATTERN
|
20
15
|
raw = $`
|
21
16
|
value = $'
|
22
|
-
|
23
|
-
to_key_block = block || Redhead.to_key
|
24
|
-
key = to_key_block[raw]
|
17
|
+
key = TO_KEY[raw]
|
25
18
|
|
26
19
|
header = new(key, raw, value)
|
27
20
|
|
@@ -38,23 +31,13 @@ module Redhead
|
|
38
31
|
"#{r}#{Redhead::HEADER_NAME_VALUE_SEPARATOR_CHARACTER} #{value}"
|
39
32
|
end
|
40
33
|
|
41
|
-
# Does the same thing as #to_s, but instead of calling #raw in the last case, it
|
34
|
+
# Does the same thing as #to_s, but instead of calling #raw in the last case, it computes the
|
35
|
+
# raw header name dynamically from the key.
|
42
36
|
def to_s!(raw_name = nil)
|
43
|
-
r = raw_name || (block_given? ? yield(key) :
|
37
|
+
r = raw_name || (block_given? ? yield(key) : TO_RAW[key])
|
44
38
|
"#{r}#{Redhead::HEADER_NAME_VALUE_SEPARATOR_CHARACTER} #{value}"
|
45
39
|
end
|
46
|
-
|
47
|
-
# Calls #to_key with #raw as the argument.
|
48
|
-
def key!
|
49
|
-
to_key[raw]
|
50
|
-
end
|
51
|
-
|
52
|
-
# Calls #to_raw with #key as the argument. By-passes the stored raw header name and reproduces
|
53
|
-
# it with #to_raw dynamically.
|
54
|
-
def raw!
|
55
|
-
to_raw[key]
|
56
|
-
end
|
57
|
-
|
40
|
+
|
58
41
|
def inspect
|
59
42
|
"{ #{key.inspect} => #{value.inspect} }"
|
60
43
|
end
|
@@ -64,20 +47,5 @@ module Redhead
|
|
64
47
|
raw == other_header.raw &&
|
65
48
|
value == other_header.value
|
66
49
|
end
|
67
|
-
|
68
|
-
# Returns the Proc object used to convert keys to raw header names. Defaults to Redhead.to_raw.
|
69
|
-
def to_raw
|
70
|
-
@to_raw || Redhead.to_raw
|
71
|
-
end
|
72
|
-
|
73
|
-
# Returns the Proc object used to convert keys to raw header names. Defaults to Redhead.to_raw.
|
74
|
-
def to_key
|
75
|
-
@to_key || Redhead.to_key
|
76
|
-
end
|
77
|
-
|
78
|
-
# Returns true if to_raw[to_key[raw]] == raw, otherwise, false.
|
79
|
-
def reversible?
|
80
|
-
to_raw[to_key[raw]] == raw
|
81
|
-
end
|
82
50
|
end
|
83
51
|
end
|
data/lib/redhead/header_set.rb
CHANGED
@@ -9,10 +9,10 @@ module Redhead
|
|
9
9
|
|
10
10
|
# Parses lines of header strings with Header.parse. Returns a new HeaderSet object
|
11
11
|
# for the parsed headers.
|
12
|
-
def self.parse(header_string
|
12
|
+
def self.parse(header_string)
|
13
13
|
headers = []
|
14
14
|
header_string.split("\n").each do |str|
|
15
|
-
headers << Redhead::Header.parse(str
|
15
|
+
headers << Redhead::Header.parse(str)
|
16
16
|
end
|
17
17
|
|
18
18
|
new(headers)
|
@@ -34,15 +34,13 @@ module Redhead
|
|
34
34
|
end
|
35
35
|
|
36
36
|
# If there is a header in the set with a key matching _key_, then set its value to _value_.
|
37
|
-
# If there is no header matching _key_, create a new header with the given key and value
|
38
|
-
# with a raw header equal to to_raw[key]. Sets the new header's to_raw to self#to_raw.
|
37
|
+
# If there is no header matching _key_, create a new header with the given key and value.
|
39
38
|
def []=(key, value)
|
40
39
|
h = self[key]
|
41
40
|
if h
|
42
41
|
h.value = value
|
43
42
|
else
|
44
|
-
new_header = Redhead::Header.new(key,
|
45
|
-
new_header.to_raw = to_raw
|
43
|
+
new_header = Redhead::Header.new(key, TO_RAW[key], value)
|
46
44
|
self << new_header
|
47
45
|
end
|
48
46
|
end
|
@@ -54,8 +52,7 @@ module Redhead
|
|
54
52
|
|
55
53
|
# Similar to #[]= but allows manually setting the value of Header#raw to _raw_.
|
56
54
|
def add(key, value, raw = nil)
|
57
|
-
new_header = Redhead::Header.new(key, raw ||
|
58
|
-
new_header.to_raw = to_raw
|
55
|
+
new_header = Redhead::Header.new(key, raw || TO_RAW[key], value)
|
59
56
|
self << new_header
|
60
57
|
new_header
|
61
58
|
end
|
@@ -83,40 +80,13 @@ module Redhead
|
|
83
80
|
end.join("\n")
|
84
81
|
end
|
85
82
|
|
86
|
-
# If a block is given, passes the block to Header#to_s!
|
83
|
+
# If a block is given, passes the block to Header#to_s! Joins
|
87
84
|
# the result with newlines.
|
88
85
|
def to_s!(&block)
|
89
|
-
blk = block ||
|
86
|
+
blk = block || TO_RAW
|
90
87
|
@headers.map { |header| header.to_s!(&blk) }.join("\n")
|
91
88
|
end
|
92
|
-
|
93
|
-
# Returns true if Header#reversible? is true for each header in the set, otherwise false.
|
94
|
-
def reversible?
|
95
|
-
all? { |header| header.reversible? }
|
96
|
-
end
|
97
|
-
|
98
|
-
# Returns the Proc to be used to convert key names to raw header names. Defaults to Redhead.to_raw.
|
99
|
-
def to_raw
|
100
|
-
@to_raw || Redhead.to_raw
|
101
|
-
end
|
102
|
-
|
103
|
-
# Sets HeaderSet#to_raw to _new_to_raw_. Sets Header#to_raw for each header in the set to _new_to_raw_.
|
104
|
-
def to_raw=(new_to_raw)
|
105
|
-
@to_raw = new_to_raw
|
106
|
-
each { |header| header.to_raw = new_to_raw }
|
107
|
-
end
|
108
|
-
|
109
|
-
# Returns the Proc to be used to convert raw header names to key names. Defaults to Redhead.to_key.
|
110
|
-
def to_key
|
111
|
-
@to_key || Redhead.to_key
|
112
|
-
end
|
113
|
-
|
114
|
-
# Sets HeaderSet#to_raw to _new_to_raw_. Sets Header#to_raw for each header in the set to _new_to_raw_.
|
115
|
-
def to_key=(new_to_key)
|
116
|
-
@to_key = new_to_key
|
117
|
-
each { |header| header.to_key = new_to_key }
|
118
|
-
end
|
119
|
-
|
89
|
+
|
120
90
|
def inspect
|
121
91
|
"{ #{@headers.map { |header| header.inspect }.join(", ")} }"
|
122
92
|
end
|
@@ -9,15 +9,13 @@ module Redhead
|
|
9
9
|
end
|
10
10
|
|
11
11
|
# Takes _string_, splits the headers from the content using HEADERS_SEPARATOR_PATTERN, then
|
12
|
-
# creates the headers by calling HeaderSet.parse
|
13
|
-
|
14
|
-
def initialize(string, &block)
|
12
|
+
# creates the headers by calling HeaderSet.parse.
|
13
|
+
def initialize(string)
|
15
14
|
string =~ HEADERS_SEPARATOR_PATTERN
|
16
15
|
@string = $'
|
17
16
|
super(@string)
|
18
17
|
|
19
|
-
@headers = Redhead::HeaderSet.parse(
|
20
|
-
@headers.to_key = block
|
18
|
+
@headers = Redhead::HeaderSet.parse($`)
|
21
19
|
end
|
22
20
|
|
23
21
|
# Returns the main body content wrapped in the Redhead String object.
|
data/redhead.gemspec
CHANGED
data/test/header_set_spec.rb
CHANGED
@@ -8,11 +8,8 @@ describe Redhead::HeaderSet do
|
|
8
8
|
@header = @headers.first
|
9
9
|
@first_header_key = :a
|
10
10
|
@header_set = Redhead::HeaderSet.new(@headers)
|
11
|
-
@default_to_raw = Redhead.to_raw
|
12
|
-
@default_to_key = Redhead.to_key
|
13
11
|
|
14
|
-
@
|
15
|
-
@irreversible_headers = Redhead::HeaderSet.parse("a_header: one\na_header_two: two")
|
12
|
+
@simple_headers = Redhead::HeaderSet.parse("A-Header: one\nA-Header-Two: two")
|
16
13
|
end
|
17
14
|
|
18
15
|
context "as a class" do
|
@@ -24,18 +21,6 @@ describe Redhead::HeaderSet do
|
|
24
21
|
Redhead::HeaderSet.parse(@full_header_set_string).should == parsed_header_set
|
25
22
|
end
|
26
23
|
end
|
27
|
-
|
28
|
-
context "with a block" do
|
29
|
-
it "uses the given block as to_key" do
|
30
|
-
Redhead::HeaderSet.parse(@full_header_set_string) { :foo }.each { |h| h.key.should == :foo }
|
31
|
-
end
|
32
|
-
|
33
|
-
it "does not set to_key on each parsed header object in the set" do
|
34
|
-
# note: Proc#== bug!
|
35
|
-
to_key = proc { :foo }
|
36
|
-
Redhead::HeaderSet.parse(@full_header_set_string, &to_key).each { |header| header.to_key.should_not == to_key }
|
37
|
-
end
|
38
|
-
end
|
39
24
|
end
|
40
25
|
|
41
26
|
it "is Enumerable and responds to #each" do
|
@@ -74,18 +59,6 @@ describe Redhead::HeaderSet do
|
|
74
59
|
@header_set[:brand_new_key].should_not be_nil
|
75
60
|
@header_set[:brand_new_key].value.should == new_value
|
76
61
|
end
|
77
|
-
|
78
|
-
it "calls #to_raw to create the value of #raw" do
|
79
|
-
@header_set.to_raw = proc { "WHOA!" }
|
80
|
-
@header_set[:brand_new_key] = "some value"
|
81
|
-
@header_set[:brand_new_key].raw.should == "WHOA!"
|
82
|
-
end
|
83
|
-
|
84
|
-
it "sets new_header#to_raw to header_set#to_raw" do
|
85
|
-
@header_set.to_raw = proc { "Whoa!" }
|
86
|
-
@header_set[:brand_new_key] = "some value"
|
87
|
-
@header_set[:brand_new_key].to_raw.should == @header_set.to_raw
|
88
|
-
end
|
89
62
|
end
|
90
63
|
end
|
91
64
|
|
@@ -109,21 +82,6 @@ describe Redhead::HeaderSet do
|
|
109
82
|
it "returns a Redhead::Header object" do
|
110
83
|
@header_set.add(:brand_new_key, "some value").class.should == Redhead::Header
|
111
84
|
end
|
112
|
-
|
113
|
-
it "calls #to_raw to create the value of #raw" do
|
114
|
-
@header_set.add(:brand_new_key, "some value")
|
115
|
-
@header_set[:brand_new_key].raw.should == "Brand-New-Key"
|
116
|
-
|
117
|
-
@header_set.to_raw = proc { "Nothing really" }
|
118
|
-
@header_set.add(:something_or_other, "something or other")
|
119
|
-
@header_set[:something_or_other].raw.should == "Nothing really"
|
120
|
-
end
|
121
|
-
|
122
|
-
it "sets new_header#to_raw to header_set#to_raw" do
|
123
|
-
@header_set.to_raw = proc { "Whoa!" }
|
124
|
-
@header_set.add(:brand_new_key, "some value")
|
125
|
-
@header_set[:brand_new_key].to_raw.should == @header_set.to_raw
|
126
|
-
end
|
127
85
|
end
|
128
86
|
|
129
87
|
it "takes an optional third argument which sets the value of the raw header" do
|
@@ -131,7 +89,6 @@ describe Redhead::HeaderSet do
|
|
131
89
|
@header_set[:foo].value.should == "bar"
|
132
90
|
@header_set[:foo].key.should == :foo
|
133
91
|
@header_set[:foo].raw.should == "BAZ!"
|
134
|
-
@header_set[:foo].raw!.should == "Foo"
|
135
92
|
end
|
136
93
|
end
|
137
94
|
|
@@ -199,11 +156,6 @@ describe Redhead::HeaderSet do
|
|
199
156
|
@header_set.to_s(&new_to_raw).should == modified_full_header_set_string(&new_to_raw)
|
200
157
|
end
|
201
158
|
|
202
|
-
it "does not leave the given block as #to_raw" do
|
203
|
-
@header_set.to_s { "test" + "test" }
|
204
|
-
@header_set.to_raw.should_not == proc { "test" + "test" }
|
205
|
-
end
|
206
|
-
|
207
159
|
it "follows the hash argument first, falling back to the given block" do
|
208
160
|
@header_set.to_s(:a => "something raw") { "NOT RAW AT ALL" }.split("\n").first.should == "something raw: value_a"
|
209
161
|
end
|
@@ -212,9 +164,8 @@ describe Redhead::HeaderSet do
|
|
212
164
|
|
213
165
|
describe "#to_s!" do
|
214
166
|
context "without a block" do
|
215
|
-
it "calls to_s! on each header in the set,
|
216
|
-
@
|
217
|
-
@header_set.to_s!.should == @headers.map { |header| "Total foo bar: #{header.value}" }.join("\n")
|
167
|
+
it "calls to_s! on each header in the set, joining the results with newlines" do
|
168
|
+
@simple_headers.to_s!.should == @simple_headers.map { |header| header.to_s! }.join("\n")
|
218
169
|
end
|
219
170
|
end
|
220
171
|
|
@@ -222,20 +173,6 @@ describe Redhead::HeaderSet do
|
|
222
173
|
it "calls to_s! on each header in the set, passing the given block, joining the results with newlines" do
|
223
174
|
@header_set.to_s! { "Total foo bar" }.should == @headers.map { |header| "Total foo bar: #{header.value}" }.join("\n")
|
224
175
|
end
|
225
|
-
|
226
|
-
it "does not leave the given block as #to_raw on existing headers" do
|
227
|
-
temp_to_raw = proc { "Total foo bar" }
|
228
|
-
@header_set.to_s!(&temp_to_raw)
|
229
|
-
@header_set.all? { |header| header.raw!.should_not == "Total foo bar" }
|
230
|
-
end
|
231
|
-
|
232
|
-
it "does not leave the given block as #to_raw on the header set" do
|
233
|
-
temp_to_raw = proc { "Total foo bar" }
|
234
|
-
@header_set.to_s!(&temp_to_raw)
|
235
|
-
@header_set.to_raw.should_not == temp_to_raw
|
236
|
-
@header_set[:temp_foo_foo] = "what"
|
237
|
-
@header_set[:temp_foo_foo].raw!.should_not == "Total foo bar"
|
238
|
-
end
|
239
176
|
end
|
240
177
|
end
|
241
178
|
|
@@ -260,85 +197,4 @@ describe Redhead::HeaderSet do
|
|
260
197
|
one.any? { |a| two.find { |b| a == b } }.should be_true
|
261
198
|
end
|
262
199
|
end
|
263
|
-
|
264
|
-
describe "#reversible?" do
|
265
|
-
it "returns true if each header in the set is reversible, otherwise false" do
|
266
|
-
@reversible_headers.reversible?.should == @reversible_headers.all? { |header| header.reversible? }
|
267
|
-
@irreversible_headers.reversible?.should == @irreversible_headers.all? { |header| header.reversible? }
|
268
|
-
|
269
|
-
@reversible_headers.reversible?.should be_true
|
270
|
-
@irreversible_headers.reversible?.should be_false
|
271
|
-
|
272
|
-
@reversible_headers.to_raw = proc { "" }
|
273
|
-
@reversible_headers.reversible?.should_not be_true # contained headers get caught up in the change
|
274
|
-
|
275
|
-
# example from the readme:
|
276
|
-
|
277
|
-
string = "A-Header-Name: a header value\n\nContent."
|
278
|
-
|
279
|
-
str = Redhead::String.new(string) do |name|
|
280
|
-
name.gsub(/-/, "").upcase.to_sym
|
281
|
-
end
|
282
|
-
|
283
|
-
str.headers.reversible?.should be_false
|
284
|
-
|
285
|
-
str = Redhead::String.new(string) do |name|
|
286
|
-
name.split(/-/).map { |e| e.upcase }.join("zzz").to_sym
|
287
|
-
end
|
288
|
-
|
289
|
-
str.headers.reversible?.should be_false
|
290
|
-
|
291
|
-
str.headers.to_raw = lambda do |name|
|
292
|
-
name.to_s.split(/zzz/).map { |e| e.capitalize }.join("-")
|
293
|
-
end
|
294
|
-
|
295
|
-
str.headers.reversible?.should be_true
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
describe "#to_raw" do
|
300
|
-
it "defaults to Redhead.to_raw" do
|
301
|
-
# note: Proc#== bug!
|
302
|
-
@header_set.to_raw.should == @default_to_raw
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
describe "#to_raw=" do
|
307
|
-
it "sets a new block to be used as to_raw" do
|
308
|
-
# note: Proc#== bug!
|
309
|
-
new_to_raw = proc { "" + "" }
|
310
|
-
@header_set.to_raw = new_to_raw
|
311
|
-
@header_set.to_raw.should_not == @default_to_raw
|
312
|
-
@header_set.to_raw.should == new_to_raw
|
313
|
-
end
|
314
|
-
|
315
|
-
it "sets to_raw, for each header in the set" do
|
316
|
-
new_to_raw = proc { "" + "" }
|
317
|
-
@header_set.to_raw = new_to_raw
|
318
|
-
@header_set.each { |header| header.to_raw.should == new_to_raw }
|
319
|
-
end
|
320
|
-
end
|
321
|
-
|
322
|
-
describe "#to_key" do
|
323
|
-
it "defaults to Redhead.to_key" do
|
324
|
-
# note: Proc#== bug!
|
325
|
-
@header_set.to_key.should == @default_to_key
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
describe "#to_key=" do
|
330
|
-
it "sets a new block to be used as to_key" do
|
331
|
-
# note: Proc#== bug!
|
332
|
-
new_to_key = proc { "" + "" }
|
333
|
-
@header_set.to_key = new_to_key
|
334
|
-
@header_set.to_key.should_not == @default_to_key
|
335
|
-
@header_set.to_key.should == new_to_key
|
336
|
-
end
|
337
|
-
|
338
|
-
it "sets to_key, for each header in the set" do
|
339
|
-
new_to_key = proc { "" + "" }
|
340
|
-
@header_set.to_key = new_to_key
|
341
|
-
@header_set.each { |header| header.to_key.should == new_to_key }
|
342
|
-
end
|
343
|
-
end
|
344
200
|
end
|
data/test/header_spec.rb
CHANGED
@@ -14,9 +14,6 @@ describe Redhead::Header do
|
|
14
14
|
|
15
15
|
@different_header_raw_name = "An original HEADER name"
|
16
16
|
@header_different_name = Redhead::Header.new(:a_header_name, "An original HEADER name", "something here")
|
17
|
-
|
18
|
-
@default_to_raw = Redhead.to_raw
|
19
|
-
@default_to_key = Redhead.to_key
|
20
17
|
end
|
21
18
|
|
22
19
|
context "as a class" do
|
@@ -50,25 +47,6 @@ describe Redhead::Header do
|
|
50
47
|
Redhead::Header.parse("#{@header_raw_name}#{before}:#{after}#{@header_value}").key.should == @header_name
|
51
48
|
end
|
52
49
|
end
|
53
|
-
|
54
|
-
context "with a given block" do
|
55
|
-
it "uses the given block as to_key" do
|
56
|
-
Redhead::Header.parse(@full_header_string) { :foo }.key.should == :foo
|
57
|
-
end
|
58
|
-
|
59
|
-
# do not set to_key to the given block so that using parse with a block does not prevent any dynamic
|
60
|
-
# calls up the chain to work out what to_raw should be for each object. i.e., Header#to_key should default
|
61
|
-
# to Redhead.to_key, but since each header doesn't know which headerset it's in, HeaderSet needs to pass down
|
62
|
-
# a block to each Header object. if the Header object set the block as to_key, there'd be problems.
|
63
|
-
#
|
64
|
-
# Similarly, Redhead::String.parse(..., &blk) should not set Header#to_key because then there'd never be any
|
65
|
-
# calls up the hierarchy, since an individual Header's @to_key would exist already.
|
66
|
-
it "sets to_key to the given block" do
|
67
|
-
# note: Proc#== bug!
|
68
|
-
to_key = proc { :foo }
|
69
|
-
Redhead::Header.parse(@full_header_string, &to_key).to_key.should_not == to_key
|
70
|
-
end
|
71
|
-
end
|
72
50
|
end
|
73
51
|
end
|
74
52
|
|
@@ -78,51 +56,12 @@ describe Redhead::Header do
|
|
78
56
|
end
|
79
57
|
end
|
80
58
|
|
81
|
-
describe "#key!" do
|
82
|
-
it "uses #to_key to convert #raw to a header key" do
|
83
|
-
@header.to_key = proc { "test" }
|
84
|
-
@header.key!.should == "test"
|
85
|
-
|
86
|
-
@header.to_key = proc { |x| x.upcase.reverse.gsub("-", "_").to_sym }
|
87
|
-
@header.key!.should == @header_raw_name.upcase.reverse.gsub("-", "_").to_sym
|
88
|
-
end
|
89
|
-
|
90
|
-
it "does not change #key after being called" do
|
91
|
-
@header.to_key = proc { "test" }
|
92
|
-
@header.key.should == @header_name
|
93
|
-
@header.key!.should == "test"
|
94
|
-
@header.key.should_not == "test"
|
95
|
-
@header.key.should == @header_name
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
59
|
describe "#raw" do
|
100
60
|
it "returns the raw header name stored at creation time" do
|
101
61
|
@header.raw.should == @header_raw_name
|
102
|
-
@header.to_raw = proc { "" }
|
103
|
-
@header.raw!.should == ""
|
104
|
-
@header.raw.should == @header_raw_name
|
105
62
|
end
|
106
63
|
end
|
107
|
-
|
108
|
-
describe "#raw!" do
|
109
|
-
it "uses #to_raw to convert #key to a raw header, ignoring the value of #raw" do
|
110
|
-
@header.to_raw = proc { "test" }
|
111
|
-
@header.raw!.should == "test"
|
112
|
-
|
113
|
-
@header.to_raw = proc { |x| x.to_s.upcase.reverse }
|
114
|
-
@header.raw!.should == @header_name.to_s.upcase.reverse
|
115
|
-
end
|
116
64
|
|
117
|
-
it "does not change #raw after being called" do
|
118
|
-
@header.to_raw = proc { "test" }
|
119
|
-
@header.raw.should == @header_raw_name
|
120
|
-
@header.raw!.should == "test"
|
121
|
-
@header.raw.should_not == "test"
|
122
|
-
@header.raw.should == @header_raw_name
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
65
|
describe "#value" do
|
127
66
|
it "returns the header value" do
|
128
67
|
@header.value.should == @header_value
|
@@ -145,12 +84,6 @@ describe Redhead::Header do
|
|
145
84
|
it "uses the given block to convert #key to a raw header, and returns the raw string" do
|
146
85
|
@header.to_s { "test" }.should == "test#{@separator} #{@header_value}"
|
147
86
|
end
|
148
|
-
|
149
|
-
it "does not set a new value for #to_raw" do
|
150
|
-
@header.to_s { "test" }
|
151
|
-
@header.to_s.should_not == "test#{@separator} #{@header_value}"
|
152
|
-
@header.to_s.should == @full_header_string
|
153
|
-
end
|
154
87
|
end
|
155
88
|
|
156
89
|
it "takes an optional argument which specifies the raw header to use, without side-effects" do
|
@@ -182,77 +115,17 @@ describe Redhead::Header do
|
|
182
115
|
end
|
183
116
|
end
|
184
117
|
|
185
|
-
describe "#reversible?" do
|
186
|
-
it "returns true for the defaults" do
|
187
|
-
@header.reversible?.should be_true
|
188
|
-
end
|
189
|
-
|
190
|
-
it "returns true if self.to_raw[self.key!] == self.raw, i.e., if we can't recover the current raw header name via to_key(to_raw(current_raw_header_name))" do
|
191
|
-
@header.to_raw[@header.key!].should == @header_raw_name
|
192
|
-
@header.reversible?.should be_true # by default
|
193
|
-
|
194
|
-
@header.to_raw = proc { |x| "1#{x}" }
|
195
|
-
@header.to_key = proc { |x| "2#{x}" }
|
196
|
-
@header.to_raw[@header.key!].should_not == @header_raw_name
|
197
|
-
@header.reversible?.should be_false # :key does not equal 2Key1Key
|
198
|
-
|
199
|
-
@header.to_raw = proc { |x| x.to_s.reverse }
|
200
|
-
@header.to_key = proc { |x| x.reverse.to_sym }
|
201
|
-
@header.to_raw[@header.key!].should == @header_raw_name
|
202
|
-
@header.reversible?.should be_true
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
describe "#to_raw" do
|
207
|
-
it "returns Redhead.to_raw by default" do
|
208
|
-
# note: Proc#== bug!
|
209
|
-
@header.to_raw.should == @default_to_raw
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
describe "#to_key" do
|
214
|
-
it "returns Redhead.to_key by default" do
|
215
|
-
# note: Proc#== bug!
|
216
|
-
@header.to_key.should == @default_to_key
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
describe "#to_raw=(blk)" do
|
221
|
-
it "sets to_raw to blk" do
|
222
|
-
new_block = proc { }
|
223
|
-
@header.to_raw.should_not == new_block
|
224
|
-
@header.to_raw = new_block
|
225
|
-
@header.to_raw.should == new_block
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
describe "#to_key=(blk)" do
|
230
|
-
it "sets to_key to blk" do
|
231
|
-
new_block = proc { "" + "" }
|
232
|
-
@header.to_key.should_not == new_block
|
233
|
-
@header.to_key = new_block
|
234
|
-
@header.to_key.should == new_block
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
118
|
describe "#to_s!" do
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
@header.to_s!.should == "#{@header.raw!}#{@separator} #{@header_value}"
|
119
|
+
it "returns <computed_raw_header><separator> <value>" do
|
120
|
+
@header.to_s!.should == "#{@header_raw_name}#{@separator} #{@header_value}"
|
121
|
+
@header_different_name.to_s!.should == "#{@header_raw_name}#{@separator} something here"
|
243
122
|
end
|
244
123
|
|
245
124
|
context "with a block argument" do
|
246
|
-
it "uses the given block
|
125
|
+
it "uses the given block to compute the raw header name" do
|
247
126
|
@header.to_s! { "testing" }.should == "testing: #{@header_value}"
|
248
127
|
@header.to_s! { |x| x.to_s.upcase.reverse.gsub("_", "-") }.should == "#{@header_raw_name.to_s.upcase.reverse}: #{@header_value}"
|
249
128
|
end
|
250
|
-
|
251
|
-
it "does not leave to_raw as the given block" do
|
252
|
-
@header.to_s! { "testing" }
|
253
|
-
@header.to_raw.should_not == proc { "testing" }
|
254
|
-
@header.to_raw[:random].should_not == "testing"
|
255
|
-
end
|
256
129
|
end
|
257
130
|
|
258
131
|
it "takes an optional argument specifying the raw header name to use, without side-effects" do
|
data/test/redhead_string_spec.rb
CHANGED
@@ -14,26 +14,6 @@ describe Redhead::String do
|
|
14
14
|
Redhead::String[@string].should == Redhead::String.new(@string)
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
18
|
-
describe "self.new" do
|
19
|
-
context "with a given block:" do
|
20
|
-
it "uses the given block as to_key" do
|
21
|
-
Redhead::String.new(@string) { :foo }.headers.first.key.should == :foo
|
22
|
-
end
|
23
|
-
|
24
|
-
it "sets the given block as the HeaderSet's to_key" do
|
25
|
-
# note: Proc#== bug!
|
26
|
-
blk = proc { :foo }
|
27
|
-
Redhead::String.new(@string, &blk).headers.to_key.should == blk
|
28
|
-
end
|
29
|
-
|
30
|
-
it "sets each individual header's to_key to the given block" do
|
31
|
-
# note: Proc#== bug!
|
32
|
-
blk = proc { :foo }
|
33
|
-
Redhead::String.new(@string, &blk).headers.each { |header| header.to_key.should == blk }
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
17
|
end
|
38
18
|
|
39
19
|
context "before any modifications:" do
|
metadata
CHANGED
@@ -1,57 +1,63 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: redhead
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.4
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Adam Prescott
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-10-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: rake
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
24
22
|
type: :development
|
25
|
-
version_requirements: *id001
|
26
|
-
- !ruby/object:Gem::Dependency
|
27
|
-
name: rspec
|
28
23
|
prerelease: false
|
29
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
25
|
none: false
|
31
|
-
requirements:
|
32
|
-
- -
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version:
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
35
38
|
type: :development
|
36
|
-
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
37
46
|
description: String header metadata.
|
38
|
-
email:
|
47
|
+
email:
|
39
48
|
- adam@aprescott.com
|
40
49
|
executables: []
|
41
|
-
|
42
50
|
extensions: []
|
43
|
-
|
44
51
|
extra_rdoc_files: []
|
45
|
-
|
46
|
-
|
47
|
-
- lib/redhead/header_set.rb
|
52
|
+
files:
|
53
|
+
- lib/redhead.rb
|
48
54
|
- lib/redhead/redhead_string.rb
|
49
55
|
- lib/redhead/header.rb
|
50
|
-
- lib/redhead.rb
|
51
|
-
- test/header_set_spec.rb
|
52
|
-
- test/redhead_spec.rb
|
56
|
+
- lib/redhead/header_set.rb
|
53
57
|
- test/test_helper.rb
|
58
|
+
- test/redhead_spec.rb
|
54
59
|
- test/redhead_string_spec.rb
|
60
|
+
- test/header_set_spec.rb
|
55
61
|
- test/header_spec.rb
|
56
62
|
- redhead.gemspec
|
57
63
|
- .gemtest
|
@@ -61,34 +67,31 @@ files:
|
|
61
67
|
- README.md
|
62
68
|
homepage: https://github.com/aprescott/redhead
|
63
69
|
licenses: []
|
64
|
-
|
65
70
|
post_install_message:
|
66
71
|
rdoc_options: []
|
67
|
-
|
68
|
-
require_paths:
|
72
|
+
require_paths:
|
69
73
|
- lib
|
70
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
75
|
none: false
|
72
|
-
requirements:
|
73
|
-
- -
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version:
|
76
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
81
|
none: false
|
78
|
-
requirements:
|
79
|
-
- -
|
80
|
-
- !ruby/object:Gem::Version
|
81
|
-
version:
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
82
86
|
requirements: []
|
83
|
-
|
84
87
|
rubyforge_project:
|
85
|
-
rubygems_version: 1.8.
|
88
|
+
rubygems_version: 1.8.24
|
86
89
|
signing_key:
|
87
90
|
specification_version: 3
|
88
91
|
summary: String header metadata.
|
89
|
-
test_files:
|
90
|
-
- test/header_set_spec.rb
|
91
|
-
- test/redhead_spec.rb
|
92
|
+
test_files:
|
92
93
|
- test/test_helper.rb
|
94
|
+
- test/redhead_spec.rb
|
93
95
|
- test/redhead_string_spec.rb
|
96
|
+
- test/header_set_spec.rb
|
94
97
|
- test/header_spec.rb
|