redhead 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/redhead/header_set.rb +23 -0
- data/lib/redhead/redhead_string.rb +43 -5
- data/redhead.gemspec +1 -1
- data/test/header_set_spec.rb +29 -0
- data/test/header_spec.rb +6 -0
- data/test/redhead_string_spec.rb +40 -0
- metadata +2 -2
data/lib/redhead/header_set.rb
CHANGED
@@ -22,6 +22,11 @@ module Redhead
|
|
22
22
|
def each
|
23
23
|
@headers.each { |h| yield h }
|
24
24
|
end
|
25
|
+
|
26
|
+
# Returns the number of headers in the set.
|
27
|
+
def size
|
28
|
+
@headers.size
|
29
|
+
end
|
25
30
|
|
26
31
|
# Returns true if the set of headers is empty.
|
27
32
|
def empty?
|
@@ -86,6 +91,24 @@ module Redhead
|
|
86
91
|
blk = block || TO_RAW
|
87
92
|
@headers.map { |header| header.to_s!(&blk) }.join("\n")
|
88
93
|
end
|
94
|
+
|
95
|
+
# Returns a hash from the headers in the set, where keys are
|
96
|
+
# formed from #key and values from #value for each header.
|
97
|
+
#
|
98
|
+
# rh_string = Redhead::String["foo: bar\nbaz: baaaaz\n\nstring"]
|
99
|
+
# rh_string.headers.to_h
|
100
|
+
#
|
101
|
+
# # A Hash instance:
|
102
|
+
# #=> { :foo => "bar", :baz => "baaaaz" }
|
103
|
+
def to_h
|
104
|
+
h = {}
|
105
|
+
each do |header|
|
106
|
+
h[header.key] = header.value
|
107
|
+
end
|
108
|
+
h
|
109
|
+
end
|
110
|
+
|
111
|
+
alias_method :to_hash, :to_h
|
89
112
|
|
90
113
|
def inspect
|
91
114
|
"{ #{@headers.map { |header| header.inspect }.join(", ")} }"
|
@@ -7,15 +7,53 @@ module Redhead
|
|
7
7
|
class << self
|
8
8
|
alias_method :[], :new
|
9
9
|
end
|
10
|
+
|
11
|
+
# Checks if the input string has header lines at the start of its content,
|
12
|
+
# and returns true or false depending on the value.
|
13
|
+
def self.has_headers?(string)
|
14
|
+
return false if string.strip.empty?
|
15
|
+
|
16
|
+
# check if the string itself is entirely headers
|
17
|
+
has_headers_no_content = string.strip.lines.all? do |l|
|
18
|
+
l =~ HEADER_NAME_VALUE_SEPARATOR_PATTERN
|
19
|
+
end
|
20
|
+
|
21
|
+
return true if has_headers_no_content
|
22
|
+
|
23
|
+
# split based on the headers separator and see if
|
24
|
+
# all lines before the separator look like headers.
|
25
|
+
|
26
|
+
string =~ HEADERS_SEPARATOR_PATTERN
|
27
|
+
head_content = $`
|
28
|
+
|
29
|
+
return false unless $`
|
30
|
+
|
31
|
+
head_content.lines.all? do |l|
|
32
|
+
l =~ HEADER_NAME_VALUE_SEPARATOR_PATTERN
|
33
|
+
end
|
34
|
+
end
|
10
35
|
|
11
36
|
# Takes _string_, splits the headers from the content using HEADERS_SEPARATOR_PATTERN, then
|
12
37
|
# creates the headers by calling HeaderSet.parse.
|
13
38
|
def initialize(string)
|
14
|
-
string
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
39
|
+
if self.class.has_headers?(string)
|
40
|
+
# if there is a separator between header content and body content
|
41
|
+
if string =~ HEADERS_SEPARATOR_PATTERN
|
42
|
+
@string = $'
|
43
|
+
header_content = $`
|
44
|
+
super(@string)
|
45
|
+
|
46
|
+
@headers = Redhead::HeaderSet.parse(header_content)
|
47
|
+
else
|
48
|
+
# we're dealing with only headers, so pass in the entire original string.
|
49
|
+
# this lets us deal with inputs like new("foo: bar")
|
50
|
+
@headers = Redhead::HeaderSet.parse(string)
|
51
|
+
end
|
52
|
+
else
|
53
|
+
@string = string
|
54
|
+
super(@string)
|
55
|
+
@headers = Redhead::HeaderSet.new([])
|
56
|
+
end
|
19
57
|
end
|
20
58
|
|
21
59
|
# Returns the main body content wrapped in the Redhead String object.
|
data/redhead.gemspec
CHANGED
data/test/header_set_spec.rb
CHANGED
@@ -113,6 +113,35 @@ describe Redhead::HeaderSet do
|
|
113
113
|
@header_set.delete(:brand_new_key).should be_nil
|
114
114
|
end
|
115
115
|
end
|
116
|
+
|
117
|
+
# to_hash is an alias for to_h
|
118
|
+
|
119
|
+
[:to_h, :to_hash].each do |meth|
|
120
|
+
describe "##{meth}" do
|
121
|
+
it "returns a Hash instance containing symbol keys and header values" do
|
122
|
+
@header_set.public_send(meth).should == Hash[@headers.map { |header| [header.key, header.value] }]
|
123
|
+
end
|
124
|
+
|
125
|
+
it "contains only keys in the original header" do
|
126
|
+
@header_set.public_send(meth).keys.should == @headers.map { |header| header.key }
|
127
|
+
end
|
128
|
+
|
129
|
+
it "contains correct corresponding values for each key" do
|
130
|
+
h = @header_set.public_send(meth)
|
131
|
+
h.keys.size.should == @header_set.size
|
132
|
+
|
133
|
+
h.each do |key, value|
|
134
|
+
@header_set[key].value.should == value
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "#size" do
|
141
|
+
it "is the number of headers in the set" do
|
142
|
+
@header_set.size.should == @headers.size
|
143
|
+
end
|
144
|
+
end
|
116
145
|
|
117
146
|
describe "#to_s" do
|
118
147
|
it "equals the individual header to_s results, joined with newlines" do
|
data/test/header_spec.rb
CHANGED
@@ -47,6 +47,12 @@ describe Redhead::Header do
|
|
47
47
|
Redhead::Header.parse("#{@header_raw_name}#{before}:#{after}#{@header_value}").key.should == @header_name
|
48
48
|
end
|
49
49
|
end
|
50
|
+
|
51
|
+
it "handles values with a separator" do
|
52
|
+
header = Redhead::Header.parse("created: 20:30")
|
53
|
+
header.key.should == :created
|
54
|
+
header.value.should == "20:30"
|
55
|
+
end
|
50
56
|
end
|
51
57
|
end
|
52
58
|
|
data/test/redhead_string_spec.rb
CHANGED
@@ -22,6 +22,41 @@ describe Redhead::String do
|
|
22
22
|
subject.headers[:bar].value.should == "2"
|
23
23
|
end
|
24
24
|
end
|
25
|
+
|
26
|
+
it "can handle strings with no headers" do
|
27
|
+
expect { Redhead::String[""] }.to_not raise_error
|
28
|
+
expect { Redhead::String["some content\n\nwith no headers"] }.to_not raise_error
|
29
|
+
end
|
30
|
+
|
31
|
+
it "can handle headers that have the header name-value separator in the value" do
|
32
|
+
test_cases = ["foo: bar:baz", "foo: bar:baz\n", "foo: bar:baz\n\ncontent"]
|
33
|
+
|
34
|
+
test_cases.each do |t|
|
35
|
+
s = Redhead::String[t]
|
36
|
+
s.headers.size.should == 1
|
37
|
+
h = s.headers[:foo]
|
38
|
+
h.should_not be_nil
|
39
|
+
h.value.should == "bar:baz"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe ".has_headers?" do
|
45
|
+
it "is true if the string has valid headers" do
|
46
|
+
tests = {
|
47
|
+
"foo: bar\n\ncontent" => true,
|
48
|
+
"foo: bar" => true,
|
49
|
+
"" => false,
|
50
|
+
"some content\n\nhere" => false,
|
51
|
+
"foo: bar:baz" => true,
|
52
|
+
"foo: bar:baz\n" => true,
|
53
|
+
"foo: bar:baz\n\ncontent" => true
|
54
|
+
}
|
55
|
+
|
56
|
+
tests.each do |input, value|
|
57
|
+
Redhead::String.has_headers?(input).should == value
|
58
|
+
end
|
59
|
+
end
|
25
60
|
end
|
26
61
|
end
|
27
62
|
|
@@ -45,6 +80,11 @@ describe Redhead::String do
|
|
45
80
|
it "returns a Redhead::HeaderSet object" do
|
46
81
|
@rh_string.headers.is_a?(Redhead::HeaderSet).should be_true
|
47
82
|
end
|
83
|
+
|
84
|
+
it "works for an empty string, too" do
|
85
|
+
s = Redhead::String[""]
|
86
|
+
s.headers.size.should == 0
|
87
|
+
end
|
48
88
|
end
|
49
89
|
|
50
90
|
it "provides regular String methods" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redhead
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|