link-header-parser 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/CHANGELOG.md +5 -1
- data/README.md +55 -10
- data/lib/link_header_parser/parsed_header.rb +13 -0
- data/lib/link_header_parser/parsed_header_collection.rb +4 -4
- data/lib/link_header_parser/version.rb +1 -1
- data/link-header-parser.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39406f09a5e4cfecc6d2e1918b947b4fef83ce65915f93af711fc816b92d3760
|
4
|
+
data.tar.gz: 10e5a92616f6db295efbeb544dd650b2ca8099f98bb269c4dc8ec45540ee4fc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d64a96e22925e26599216e3c6e3e27f90b546a9dea6d7aba08fd6566240ec25688e674424d2803eed7d63c424477c8f02841b9f0061e0100928f3c37d146628
|
7
|
+
data.tar.gz: 6d2951bdd0d745f7ae9176f63bb0e5f7c9f2ecab722252f8f5c557e03ebb2afea6a41494a0d697f7ef9e73eb699150d4763d31e2246ae1ebf970ba19f0ac7e26
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
0.
|
3
|
+
## 0.2.0 / 2019-07-02
|
4
|
+
|
5
|
+
- Add support for the `anchor` parameter to `ParsedHeader` exposed via `context` and `context_uri` methods ([d2dff52](https://github.com/jgarber623/link-header-parser-ruby/commit/d2dff52)).
|
6
|
+
|
7
|
+
## 0.1.0 / 2019-06-06
|
4
8
|
|
5
9
|
- Initial release!
|
data/README.md
CHANGED
@@ -44,14 +44,34 @@ response = HTTP.get('https://sixtwothree.org')
|
|
44
44
|
link_headers = response.headers.get('link')
|
45
45
|
|
46
46
|
collection = LinkHeaderParser.parse(link_headers, base: response.uri.to_s)
|
47
|
+
```
|
48
|
+
|
49
|
+
The `parse` method accepts two arguments:
|
50
|
+
|
51
|
+
1. an `Array` of strings representing HTTP Link headers (e.g. `['</>; rel="home"', '</chapters/1>; anchor="#copyright"; rel="license"']`)
|
52
|
+
1. a `String` representing the absolute URL of the resource providing the HTTP Link headers
|
53
|
+
|
54
|
+
In the example above, `collection` is an instance of `ParsedHeaderCollection` which includes Ruby's [`Enumerable`](https://ruby-doc.org/core/Enumerable.html) mixin. This mixin allows for use of common methods like `each`, `first`/`last`, and `map`.
|
47
55
|
|
48
|
-
|
56
|
+
For example, you could retrieve an array of `target_uri`s:
|
57
|
+
|
58
|
+
```ruby
|
49
59
|
puts collection.map(&:target_uri) # => ["https://assets.sixtwothree.org/", "https://fonts.googleapis.com/", "https://fonts.gstatic.com/", "https://sixtwothree.org/webmentions"]
|
50
60
|
```
|
51
61
|
|
52
|
-
|
62
|
+
### Working with a `ParsedHeaderCollection`
|
63
|
+
|
64
|
+
In addition to the included `Enumerable` methods, the following methods may be used to interact with a `ParsedHeaderCollection`:
|
53
65
|
|
54
|
-
|
66
|
+
#### The `relation_types` Method
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
puts collection.relation_types # => ["preconnect", "webmention"]
|
70
|
+
```
|
71
|
+
|
72
|
+
#### The `by_relation_type` Method
|
73
|
+
|
74
|
+
Using the `collection` from above, the `by_relation_type` method returns an `OpenStruct` with the following attributes:
|
55
75
|
|
56
76
|
```ruby
|
57
77
|
{
|
@@ -66,18 +86,43 @@ Additionally, `collection.by_relation_type` returns an `OpenStruct` with the fol
|
|
66
86
|
}
|
67
87
|
```
|
68
88
|
|
69
|
-
|
89
|
+
### Working with a `ParsedHeader`
|
90
|
+
|
91
|
+
You may interact with one or more `ParsedHeader`s in a `ParsedHeaderCollection` using the methods outlined below. The naming conventions for these methods draws heavily on the terminology established in [RFC-5988](https://tools.ietf.org/html/rfc5988) and [RFC-8288](https://tools.ietf.org/html/rfc8288).
|
92
|
+
|
93
|
+
#### Link Target ([§ 3.1](https://tools.ietf.org/html/rfc8288#section-3.1))
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
link_headers = ['</index.html>; rel="home"']
|
97
|
+
parsed_header = LinkHeaderParser.parse(link_headers, base: 'https://example.com/').first
|
98
|
+
|
99
|
+
parsed_header.target # => '/index.html'
|
100
|
+
parsed_header.target_uri # => 'https://example.com/index.html'
|
101
|
+
```
|
102
|
+
|
103
|
+
The `target` method returns a string of the value between the opening and closing angle brackets at the beginning of the Link header. The `target_uri` method returns a string representing the resolved URL.
|
104
|
+
|
105
|
+
#### Link Context ([§ 3.2](https://tools.ietf.org/html/rfc8288#section-3.2))
|
70
106
|
|
71
107
|
```ruby
|
72
|
-
|
108
|
+
link_headers = ['<https://example.com/chapters/1>; anchor="#copyright"; rel="license"']
|
109
|
+
parsed_header = LinkHeaderParser.parse(link_headers, base: 'https://example.com/').first
|
73
110
|
|
74
|
-
parsed_header.
|
75
|
-
parsed_header.
|
76
|
-
parsed_header.target # => 'https://assets.sixtwothree.org/'
|
77
|
-
parsed_header.target_uri # => 'https://assets.sixtwothree.org/'
|
111
|
+
parsed_header.context # => '#copyright'
|
112
|
+
parsed_header.context_uri # => 'https://example.com/chapters/1#copyright'
|
78
113
|
```
|
79
114
|
|
80
|
-
The
|
115
|
+
The `anchor` parameter's value may be a fragment identifier (e.g. `#foo`), a relative URL (e.g. `/foo`), or an absolute URL (e.g. `https://context.example.com`). The `context` method returns the `anchor` parameter's value (when present) and defaults to the `target` value.
|
116
|
+
|
117
|
+
#### Relation Type ([§ 3.3](https://tools.ietf.org/html/rfc8288#section-3.3))
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
link_headers = ['<https://example.com/chapters/1>; rel="prev start"']
|
121
|
+
parsed_header = LinkHeaderParser.parse(link_headers, base: 'https://example.com/').first
|
122
|
+
|
123
|
+
parsed_header.relations # => 'prev start'
|
124
|
+
parsed_header.relation_types # => ['prev', 'start']
|
125
|
+
```
|
81
126
|
|
82
127
|
## Contributing
|
83
128
|
|
@@ -10,6 +10,15 @@ module LinkHeaderParser
|
|
10
10
|
@base = base
|
11
11
|
end
|
12
12
|
|
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
|
+
|
13
22
|
def inspect
|
14
23
|
format(%(#<#{self.class.name}:%#0x @header="#{header.gsub('"', '\"')}">), object_id)
|
15
24
|
end
|
@@ -22,10 +31,12 @@ module LinkHeaderParser
|
|
22
31
|
@relation_types ||= relations&.split(' ') || nil
|
23
32
|
end
|
24
33
|
|
34
|
+
# https://tools.ietf.org/html/rfc8288#section-3.3
|
25
35
|
def relations
|
26
36
|
@relations ||= parameters.rel || nil
|
27
37
|
end
|
28
38
|
|
39
|
+
# https://tools.ietf.org/html/rfc8288#section-3.1
|
29
40
|
def target
|
30
41
|
@target ||= header_match_data[:target]
|
31
42
|
end
|
@@ -38,6 +49,8 @@ module LinkHeaderParser
|
|
38
49
|
{
|
39
50
|
target: target,
|
40
51
|
target_uri: target_uri,
|
52
|
+
context: context,
|
53
|
+
context_uri: context_uri,
|
41
54
|
relations: relations,
|
42
55
|
relation_types: relation_types,
|
43
56
|
parameters: parameters.to_h
|
@@ -47,12 +47,12 @@ module LinkHeaderParser
|
|
47
47
|
@mapped_relation_types ||= relation_types.map { |relation_type| [relation_type, find_all_by_relation_type(relation_type)] }.to_h
|
48
48
|
end
|
49
49
|
|
50
|
-
def uniq_headers
|
51
|
-
@uniq_headers ||= headers.map { |header| header.split(/,(?=[\s|<])/) }.flatten.map(&:strip)
|
52
|
-
end
|
53
|
-
|
54
50
|
def parsed_headers
|
55
51
|
@parsed_headers ||= uniq_headers.map { |header| ParsedHeader.new(header, base: @base) }
|
56
52
|
end
|
53
|
+
|
54
|
+
def uniq_headers
|
55
|
+
@uniq_headers ||= headers.map { |header| header.split(/,(?=[\s|<])/) }.flatten.map(&:strip)
|
56
|
+
end
|
57
57
|
end
|
58
58
|
end
|
data/link-header-parser.gemspec
CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_development_dependency 'rake', '~> 12.3'
|
29
29
|
spec.add_development_dependency 'reek', '~> 5.4'
|
30
30
|
spec.add_development_dependency 'rspec', '~> 3.8'
|
31
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
31
|
+
spec.add_development_dependency 'rubocop', '~> 0.72.0'
|
32
32
|
spec.add_development_dependency 'rubocop-performance', '~> 1.3'
|
33
33
|
spec.add_development_dependency 'rubocop-rspec', '~> 1.33'
|
34
34
|
spec.add_development_dependency 'simplecov', '~> 0.16.1'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: link-header-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Garber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.
|
61
|
+
version: 0.72.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.
|
68
|
+
version: 0.72.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rubocop-performance
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -169,7 +169,7 @@ licenses:
|
|
169
169
|
- MIT
|
170
170
|
metadata:
|
171
171
|
bug_tracker_uri: https://github.com/jgarber623/link-header-parser-ruby/issues
|
172
|
-
changelog_uri: https://github.com/jgarber623/link-header-parser-ruby/blob/v0.
|
172
|
+
changelog_uri: https://github.com/jgarber623/link-header-parser-ruby/blob/v0.2.0/CHANGELOG.md
|
173
173
|
post_install_message:
|
174
174
|
rdoc_options: []
|
175
175
|
require_paths:
|