Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit d2dff52

Browse files
committed
Add support for anchor/context/context_uri
1 parent cfb843f commit d2dff52

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

lib/link_header_parser/parsed_header.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ def initialize(header, base:)
1010
@base = base
1111
end
1212

13+
# https://tools.ietf.org/html/rfc8288#section-3.2
14+
def context
15+
@context ||= parameters.anchor || target
16+
end
17+
18+
def context_uri
19+
@context_uri ||= Absolutely.to_abs(base: target_uri, relative: context)
20+
end
21+
1322
def inspect
1423
format(%(#<#{self.class.name}:%#0x @header="#{header.gsub('"', '\"')}">), object_id)
1524
end
@@ -22,10 +31,12 @@ def relation_types
2231
@relation_types ||= relations&.split(' ') || nil
2332
end
2433

34+
# https://tools.ietf.org/html/rfc8288#section-3.3
2535
def relations
2636
@relations ||= parameters.rel || nil
2737
end
2838

39+
# https://tools.ietf.org/html/rfc8288#section-3.1
2940
def target
3041
@target ||= header_match_data[:target]
3142
end
@@ -40,6 +51,8 @@ def to_h
4051
target_uri: target_uri,
4152
relations: relations,
4253
relation_types: relation_types,
54+
context: context,
55+
context_uri: context_uri,
4356
parameters: parameters.to_h
4457
}
4558
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
describe LinkHeaderParser::ParsedHeader, '#context' do
2+
ExampleLinkHeaders::EXAMPLE_LINK_HEADERS_WITH_ANCHORS.each do |header, result|
3+
context "when header is #{header}" do
4+
subject(:parsed_header) { described_class.new(header, base: 'https://example.com') }
5+
6+
it 'returns a Hash' do
7+
expect(parsed_header.to_h).to include(result)
8+
end
9+
end
10+
end
11+
end
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
module ExampleLinkHeaders
2+
EXAMPLE_LINK_HEADERS_WITH_ANCHORS = [
3+
[
4+
'<https://example.com>; anchor=#foo',
5+
{
6+
context: '#foo',
7+
context_uri: 'https://example.com/#foo',
8+
parameters: {
9+
anchor: '#foo'
10+
}
11+
}
12+
],
13+
[
14+
'<https://example.com>; anchor="#foo"',
15+
{
16+
context: '#foo',
17+
context_uri: 'https://example.com/#foo',
18+
parameters: {
19+
anchor: '#foo'
20+
}
21+
}
22+
],
23+
[
24+
'</>; anchor=#foo',
25+
{
26+
context: '#foo',
27+
context_uri: 'https://example.com/#foo',
28+
parameters: {
29+
anchor: '#foo'
30+
}
31+
}
32+
],
33+
[
34+
'</>; anchor="#foo"',
35+
{
36+
context: '#foo',
37+
context_uri: 'https://example.com/#foo',
38+
parameters: {
39+
anchor: '#foo'
40+
}
41+
}
42+
],
43+
[
44+
'<https://example.com/foo>; anchor=bar',
45+
{
46+
context: 'bar',
47+
context_uri: 'https://example.com/bar',
48+
parameters: {
49+
anchor: 'bar'
50+
}
51+
}
52+
],
53+
[
54+
'<https://example.com/foo>; anchor="bar"',
55+
{
56+
context: 'bar',
57+
context_uri: 'https://example.com/bar',
58+
parameters: {
59+
anchor: 'bar'
60+
}
61+
}
62+
],
63+
[
64+
'<https://example.com/foo>; anchor=../bar',
65+
{
66+
context: '../bar',
67+
context_uri: 'https://example.com/bar',
68+
parameters: {
69+
anchor: '../bar'
70+
}
71+
}
72+
],
73+
[
74+
'<https://example.com/foo>; anchor="../bar"',
75+
{
76+
context: '../bar',
77+
context_uri: 'https://example.com/bar',
78+
parameters: {
79+
anchor: '../bar'
80+
}
81+
}
82+
],
83+
[
84+
'</>; anchor=bar',
85+
{
86+
context: 'bar',
87+
context_uri: 'https://example.com/bar',
88+
parameters: {
89+
anchor: 'bar'
90+
}
91+
}
92+
],
93+
[
94+
'</>; anchor="bar"',
95+
{
96+
context: 'bar',
97+
context_uri: 'https://example.com/bar',
98+
parameters: {
99+
anchor: 'bar'
100+
}
101+
}
102+
],
103+
[
104+
'</>; anchor=../bar',
105+
{
106+
context: '../bar',
107+
context_uri: 'https://example.com/bar',
108+
parameters: {
109+
anchor: '../bar'
110+
}
111+
}
112+
],
113+
[
114+
'</>; anchor="../bar"',
115+
{
116+
context: '../bar',
117+
context_uri: 'https://example.com/bar',
118+
parameters: {
119+
anchor: '../bar'
120+
}
121+
}
122+
],
123+
[
124+
'<https://example.com>; rel=home; anchor="#foo"',
125+
{
126+
context: '#foo',
127+
context_uri: 'https://example.com/#foo',
128+
parameters: {
129+
anchor: '#foo',
130+
rel: 'home'
131+
}
132+
}
133+
],
134+
[
135+
'<https://example.com>; rel="home previous"; anchor=#foo',
136+
{
137+
context: '#foo',
138+
context_uri: 'https://example.com/#foo',
139+
parameters: {
140+
anchor: '#foo',
141+
rel: 'home previous'
142+
}
143+
}
144+
],
145+
[
146+
'</foo>; rel=home; anchor="#bar"',
147+
{
148+
context: '#bar',
149+
context_uri: 'https://example.com/foo#bar',
150+
parameters: {
151+
anchor: '#bar',
152+
rel: 'home'
153+
}
154+
}
155+
],
156+
[
157+
'</foo>; rel="home previous"; anchor=#bar',
158+
{
159+
context: '#bar',
160+
context_uri: 'https://example.com/foo#bar',
161+
parameters: {
162+
anchor: '#bar',
163+
rel: 'home previous'
164+
}
165+
}
166+
],
167+
[
168+
'</>; anchor="https://context.example.com"',
169+
{
170+
context: 'https://context.example.com',
171+
context_uri: 'https://context.example.com/',
172+
parameters: {
173+
anchor: 'https://context.example.com'
174+
}
175+
}
176+
]
177+
].freeze
178+
end

0 commit comments

Comments
 (0)