link-header-parser 4.0.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -1
- data/CONTRIBUTING.md +3 -3
- data/README.md +6 -7
- data/lib/link-header-parser.rb +13 -4
- data/lib/link_header_parser/link_header.rb +43 -16
- data/lib/link_header_parser/link_header_parameter.rb +13 -3
- data/lib/link_header_parser/link_headers_collection.rb +29 -5
- 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: a3bede096a22050eb158b8c5e55a8467cf2a0abb91a7058962ab99b1aebe9162
|
4
|
+
data.tar.gz: 02a647ff14be96c4dfc11f571694d90ef2e75fe2b3930ceb09c0ecb0ab55d92a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af0da321a1652d3f6740fb4396a95d66213334b04eec1bd794652de894f2c7b6d9effd52701722ca851fbb01bcc38601e4c80b20290cede4b4856f36e6436520
|
7
|
+
data.tar.gz: ea527e8a58c23acafc460a73224d72204d9b82074dfa53275518eea5fd1aad940489d4efc5b770700a7aaf120160e8f589c36f56d9965ffba47d23174a09c73a
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,20 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 5.0.0 / 2022-11-09
|
4
|
+
|
5
|
+
- **Breaking change:** Add `#to_ary`/`#to_a` method to `LinkHeadersCollection` (6a478ed)
|
6
|
+
- Note: marking this as a breaking change since it changes the existing behavior to return an `Array` of `Hash`es instead of an `Array` of `LinkHeader`s
|
7
|
+
- **Breaking change:** Update project Ruby version to 2.7.6 and minimum Ruby version to 2.7 (1e9df45)
|
8
|
+
- Improve documentation and privatize constants (8a68aa0)
|
9
|
+
- Remove Reek development dependency (423bc7a)
|
10
|
+
- Use ruby/debug gem instead of pry-byebug (4cd9d26)
|
11
|
+
|
3
12
|
## 4.0.0 / 2022-01-06
|
4
13
|
|
5
14
|
- Remove Addressable dependency (daedf22)
|
6
15
|
- **Breaking change:** Update minimum Ruby version to 2.6 (e2343ed)
|
7
16
|
- Migrate to GitHub Actions (509b2c4)
|
8
|
-
- Update project Ruby version to 2.6.
|
17
|
+
- Update project Ruby version to 2.6.10 (c39bc66)
|
9
18
|
|
10
19
|
## 3.0.0 / 2021-05-22
|
11
20
|
|
data/CONTRIBUTING.md
CHANGED
@@ -8,9 +8,9 @@ There are a couple ways you can help improve link-header-parser-ruby:
|
|
8
8
|
|
9
9
|
## Getting Started
|
10
10
|
|
11
|
-
link-header-parser-ruby is developed using Ruby 2.6
|
11
|
+
link-header-parser-ruby is developed using Ruby 2.7.6 and is additionally tested against Ruby 3.0 and 3.1 using [GitHub Actions](https://github.com/jgarber623/link-header-parser-ruby/actions).
|
12
12
|
|
13
|
-
Before making changes to link-header-parser-ruby, you'll want to install Ruby 2.6.
|
13
|
+
Before making changes to link-header-parser-ruby, you'll want to install Ruby 2.7.6. It's recommended that you use a Ruby version managment tool like [rbenv](https://github.com/rbenv/rbenv), [chruby](https://github.com/postmodern/chruby), or [rvm](https://github.com/rvm/rvm). Once you've installed Ruby 2.7.6 using your method of choice, install the project's gems by running:
|
14
14
|
|
15
15
|
```sh
|
16
16
|
bundle install
|
@@ -22,7 +22,7 @@ bundle install
|
|
22
22
|
1. Install development dependencies as outlined above.
|
23
23
|
1. Create a feature branch for the code changes you're looking to make: `git checkout -b my-new-feature`.
|
24
24
|
1. _Write some code!_
|
25
|
-
1. If your changes would benefit from testing, add the necessary tests and verify everything passes by running `
|
25
|
+
1. If your changes would benefit from testing, add the necessary tests and verify everything passes by running `bundle exec rspec`.
|
26
26
|
1. Commit your changes: `git commit -am 'Add some new feature or fix some issue'`. _(See [this excellent article](https://chris.beams.io/posts/git-commit/) for tips on writing useful Git commit messages.)_
|
27
27
|
1. Push the branch to your fork: `git push -u origin my-new-feature`.
|
28
28
|
1. Create a new [pull request][pulls] and we'll review your changes.
|
data/README.md
CHANGED
@@ -10,9 +10,9 @@
|
|
10
10
|
|
11
11
|
## Getting Started
|
12
12
|
|
13
|
-
Before installing and using link-header-parser-ruby, you'll want to have [Ruby](https://www.ruby-lang.org) 2.
|
13
|
+
Before installing and using link-header-parser-ruby, you'll want to have [Ruby](https://www.ruby-lang.org) 2.7 (or newer) installed. It's recommended that you use a Ruby version managment tool like [rbenv](https://github.com/rbenv/rbenv), [chruby](https://github.com/postmodern/chruby), or [rvm](https://github.com/rvm/rvm).
|
14
14
|
|
15
|
-
link-header-parser-ruby is developed using Ruby 2.6
|
15
|
+
link-header-parser-ruby is developed using Ruby 2.7.6 and is additionally tested against Ruby 3.0 and 3.1 using [GitHub Actions](https://github.com/jgarber623/link-header-parser-ruby/actions).
|
16
16
|
|
17
17
|
## Installation
|
18
18
|
|
@@ -35,14 +35,13 @@ $ bundle install
|
|
35
35
|
With link-header-parser-ruby added to your project's `Gemfile` and installed, you may parse a URL's HTTP Link headers by doing:
|
36
36
|
|
37
37
|
```ruby
|
38
|
-
require 'http'
|
38
|
+
require 'net/http'
|
39
39
|
require 'link-header-parser'
|
40
40
|
|
41
|
-
|
41
|
+
url = 'https://sixtwothree.org'
|
42
|
+
link_headers = Net::HTTP.get_response(URI.parse(url)).get_fields('Link')
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
collection = LinkHeaderParser.parse(link_headers, base: response.uri)
|
44
|
+
collection = LinkHeaderParser.parse(link_headers, base: url)
|
46
45
|
```
|
47
46
|
|
48
47
|
The `parse` method accepts two arguments:
|
data/lib/link-header-parser.rb
CHANGED
@@ -9,11 +9,20 @@ require_relative 'link_header_parser/link_header_parameter'
|
|
9
9
|
require_relative 'link_header_parser/link_headers_collection'
|
10
10
|
|
11
11
|
module LinkHeaderParser
|
12
|
-
# Parse an array of HTTP Link headers
|
12
|
+
# Parse an array of HTTP Link headers.
|
13
13
|
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# @
|
14
|
+
# Convenience method for {LinkHeaderParser::LinkHeadersCollection#initialize}.
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# require 'net/http'
|
18
|
+
#
|
19
|
+
# url = 'https://sixtwothree.org'
|
20
|
+
# link_headers = Net::HTTP.get_response(URI.parse(url)).get_fields('Link')
|
21
|
+
#
|
22
|
+
# LinkHeaderParser.parse(link_headers, base: url)
|
23
|
+
#
|
24
|
+
# @param (see LinkHeaderParser::LinkHeadersCollection#initialize)
|
25
|
+
# @return (see LinkHeaderParser::LinkHeadersCollection#initialize)
|
17
26
|
def self.parse(*headers, base:)
|
18
27
|
LinkHeadersCollection.new(*headers, base: base)
|
19
28
|
end
|
@@ -3,30 +3,43 @@
|
|
3
3
|
module LinkHeaderParser
|
4
4
|
class LinkHeader
|
5
5
|
FIELD_VALUE_REGEXP_PATTERN = /^\s*<\s*(?<target_string>[^>]+)\s*>\s*(?<parameters>;\s*.*)$/.freeze
|
6
|
+
private_constant :FIELD_VALUE_REGEXP_PATTERN
|
7
|
+
|
6
8
|
PARAMETERS_REGEXP_PATTERN = /(?<!;)\s*([^;]+)/.freeze
|
9
|
+
private_constant :PARAMETERS_REGEXP_PATTERN
|
7
10
|
|
11
|
+
# The +String+ value used to create this {LinkHeader}.
|
12
|
+
#
|
13
|
+
# @return [String]
|
8
14
|
attr_reader :field_value
|
9
15
|
|
10
|
-
# Create a new parsed Link header
|
16
|
+
# Create a new parsed Link header.
|
17
|
+
#
|
11
18
|
# @see https://tools.ietf.org/html/rfc8288#appendix-B.2
|
19
|
+
# IETF RFC 8288 Web Linking Appendix B.2 Parsing a Link Field Value
|
12
20
|
#
|
13
|
-
# @param field_value [String]
|
14
|
-
# @param base [String]
|
21
|
+
# @param field_value [String, #to_str]
|
22
|
+
# @param base [String, #to_str]
|
15
23
|
def initialize(field_value, base:)
|
16
24
|
@field_value = field_value.to_str
|
17
25
|
@base = base.to_str
|
18
26
|
end
|
19
27
|
|
20
|
-
# The context URL for this Link header extracted from field_value (or
|
21
|
-
#
|
28
|
+
# The context URL for this Link header extracted from +field_value+ (or
|
29
|
+
# target URL if no context URL is present).
|
30
|
+
#
|
31
|
+
# @see https://tools.ietf.org/html/rfc8288#appendix-B.2
|
32
|
+
# IETF RFC 8288 Web Linking Appendix B.2.2.11 Parsing a Link Field Value
|
22
33
|
#
|
23
34
|
# @return [String]
|
24
35
|
def context_string
|
25
36
|
@context_string ||= grouped_link_parameters[:anchor]&.first || target_string
|
26
37
|
end
|
27
38
|
|
28
|
-
# The resolved context URL for this Link header
|
29
|
-
#
|
39
|
+
# The resolved context URL for this Link header.
|
40
|
+
#
|
41
|
+
# @see https://tools.ietf.org/html/rfc8288#appendix-B.2
|
42
|
+
# IETF RFC 8288 Web Linking Appendix B.2.2.12 Parsing a Link Field Value
|
30
43
|
#
|
31
44
|
# @return [String]
|
32
45
|
def context_uri
|
@@ -40,8 +53,10 @@ module LinkHeaderParser
|
|
40
53
|
"relation_types: #{relation_types.inspect}>"
|
41
54
|
end
|
42
55
|
|
43
|
-
# The parsed parameters for this Link header extracted from field_value
|
56
|
+
# The parsed parameters for this Link header extracted from +field_value+.
|
57
|
+
#
|
44
58
|
# @see https://tools.ietf.org/html/rfc8288#appendix-B.3
|
59
|
+
# IETF RFC 8288 Web Linking Appendix B.3 Parsing Parameters
|
45
60
|
#
|
46
61
|
# @return [Array<LinkHeaderParser::LinkHeaderParameter>]
|
47
62
|
def link_parameters
|
@@ -50,38 +65,50 @@ module LinkHeaderParser
|
|
50
65
|
.map { |parameter| LinkHeaderParameter.new(parameter) }
|
51
66
|
end
|
52
67
|
|
53
|
-
# The relations_string value returned as an Array
|
54
|
-
#
|
68
|
+
# The +relations_string+ value returned as an +Array+.
|
69
|
+
#
|
70
|
+
# @see LinkHeader#relations_string
|
71
|
+
#
|
72
|
+
# @see https://tools.ietf.org/html/rfc8288#appendix-B.2
|
73
|
+
# IETF RFC 8288 Web Linking Appendix B.2.2.10 and Appendix B.2.2.17.1 Parsing a Link Field Value
|
55
74
|
#
|
56
75
|
# @return [Array<String>]
|
57
76
|
def relation_types
|
58
77
|
@relation_types ||= relations_string.split.map(&:downcase)
|
59
78
|
end
|
60
79
|
|
61
|
-
# The relation types for this Link header extracted from field_value
|
62
|
-
#
|
80
|
+
# The relation types for this Link header extracted from +field_value+.
|
81
|
+
#
|
82
|
+
# @see https://tools.ietf.org/html/rfc8288#appendix-B.2
|
83
|
+
# IETF RFC 8288 Web Linking Appendix B.2.2.9 Parsing a Link Field Value
|
63
84
|
#
|
64
85
|
# @return [String]
|
65
86
|
def relations_string
|
66
87
|
@relations_string ||= grouped_link_parameters[:rel]&.first.to_s
|
67
88
|
end
|
68
89
|
|
69
|
-
# The target URL for this Link header extracted from field_value
|
70
|
-
#
|
90
|
+
# The target URL for this Link header extracted from +field_value+
|
91
|
+
#
|
92
|
+
# @see https://tools.ietf.org/html/rfc8288#appendix-B.2
|
93
|
+
# IETF RFC 8288 Web Linking Appendix B.2.2.4 Parsing a Link Field Value
|
71
94
|
#
|
72
95
|
# @return [String]
|
73
96
|
def target_string
|
74
97
|
@target_string ||= field_value_match_data[:target_string]
|
75
98
|
end
|
76
99
|
|
77
|
-
# The resolved target URL for this Link header
|
78
|
-
#
|
100
|
+
# The resolved target URL for this Link header.
|
101
|
+
#
|
102
|
+
# @see https://tools.ietf.org/html/rfc8288#appendix-B.2
|
103
|
+
# IETF RFC 8288 Web Linking Appendix B.2.2.8 Parsing a Link Field Value
|
79
104
|
#
|
80
105
|
# @return [String]
|
81
106
|
def target_uri
|
82
107
|
@target_uri ||= URI.join(base, target_string).normalize.to_s
|
83
108
|
end
|
84
109
|
|
110
|
+
# Return a +Hash+ representation of this {LinkHeader}.
|
111
|
+
#
|
85
112
|
# @return [Hash{Symbol => String, Array, Hash{Symbol => Array}}]
|
86
113
|
def to_hash
|
87
114
|
{
|
@@ -3,10 +3,16 @@
|
|
3
3
|
module LinkHeaderParser
|
4
4
|
class LinkHeaderParameter
|
5
5
|
PARAMETER_REGEXP_PATTERN = /^(?<name>.+?)(?:="?(?<value>.*?)"?)?$/.freeze
|
6
|
+
private_constant :PARAMETER_REGEXP_PATTERN
|
6
7
|
|
8
|
+
# The +String+ value used to create this {LinkHeaderParameter}.
|
9
|
+
#
|
10
|
+
# @return [String]
|
7
11
|
attr_reader :parameter
|
8
12
|
|
9
|
-
#
|
13
|
+
# Create a new parsed Link header parameter.
|
14
|
+
#
|
15
|
+
# @param parameter [String, #to_str]
|
10
16
|
def initialize(parameter)
|
11
17
|
@parameter = parameter.to_str
|
12
18
|
end
|
@@ -18,20 +24,24 @@ module LinkHeaderParser
|
|
18
24
|
"value: #{value.inspect}>"
|
19
25
|
end
|
20
26
|
|
21
|
-
# @see https://tools.ietf.org/html/rfc8288#appendix-B.3
|
27
|
+
# @see https://tools.ietf.org/html/rfc8288#appendix-B.3
|
28
|
+
# IETF RFC 8288 Web Linking Appendix B.3.2.9 Parsing Parameters
|
22
29
|
#
|
23
30
|
# @return [String]
|
24
31
|
def name
|
25
32
|
@name ||= parameter_match_data[:name].downcase
|
26
33
|
end
|
27
34
|
|
28
|
-
# @see https://tools.ietf.org/html/rfc8288#appendix-B.3
|
35
|
+
# @see https://tools.ietf.org/html/rfc8288#appendix-B.3
|
36
|
+
# IETF RFC 8288 Web Linking Appendix B.3.2.8 Parsing Parameters
|
29
37
|
#
|
30
38
|
# @return [String]
|
31
39
|
def value
|
32
40
|
@value ||= parameter_match_data[:value].to_s
|
33
41
|
end
|
34
42
|
|
43
|
+
# Return an +Array+ representation of this {LinkHeaderParameter}.
|
44
|
+
#
|
35
45
|
# @return [Array<String>]
|
36
46
|
def to_ary
|
37
47
|
[name, value]
|
@@ -8,17 +8,26 @@ module LinkHeaderParser
|
|
8
8
|
|
9
9
|
def_delegators :members, :[], :<<, :each, :last, :length, :push
|
10
10
|
|
11
|
+
# The +Array+ of HTTP Link headers used to create this
|
12
|
+
# {LinkHeadersCollection}.
|
13
|
+
#
|
14
|
+
# @return [Array<String>]
|
11
15
|
attr_reader :headers
|
12
16
|
|
13
|
-
#
|
14
|
-
#
|
17
|
+
# Parse an array of HTTP Link headers.
|
18
|
+
#
|
19
|
+
# @param headers [Array<String, #to_str>]
|
20
|
+
# @param base [String, #to_str]
|
15
21
|
def initialize(*headers, base:)
|
16
22
|
@headers = headers.to_ary.flatten.map(&:to_str)
|
17
23
|
@base = base.to_str
|
18
24
|
|
19
|
-
|
25
|
+
push(*distinct_link_headers)
|
20
26
|
end
|
21
27
|
|
28
|
+
# Retrieve a +Hash+ of this collection's {LinkHeader}s grouped by their
|
29
|
+
# relation type(s).
|
30
|
+
#
|
22
31
|
# @return [Hash{Symbol => Array<LinkHeaderParser::LinkHeader>}]
|
23
32
|
def group_by_relation_type
|
24
33
|
relation_types.to_h do |relation_type|
|
@@ -33,17 +42,32 @@ module LinkHeaderParser
|
|
33
42
|
"relation_types: #{relation_types.inspect}>"
|
34
43
|
end
|
35
44
|
|
45
|
+
# Retrieve a unique sorted +Array+ of this collection's {LinkHeader}
|
46
|
+
# relation types.
|
47
|
+
#
|
36
48
|
# @return [Array<String>]
|
37
49
|
def relation_types
|
38
50
|
@relation_types ||= flat_map(&:relation_types).uniq.sort
|
39
51
|
end
|
40
52
|
|
53
|
+
# Return an +Array+ representation of this {LinkHeadersCollection}.
|
54
|
+
#
|
55
|
+
# @see LinkHeader#to_hash
|
56
|
+
#
|
57
|
+
# @return [Array<Hash>}>]
|
58
|
+
def to_ary
|
59
|
+
map(&:to_hash)
|
60
|
+
end
|
61
|
+
|
62
|
+
alias to_a to_ary
|
63
|
+
|
41
64
|
private
|
42
65
|
|
43
66
|
attr_reader :base
|
44
67
|
|
45
|
-
def
|
46
|
-
|
68
|
+
def distinct_link_headers
|
69
|
+
headers.flat_map { |header| header.split(/,(?=[\s|<])/) }
|
70
|
+
.map { |header| LinkHeader.new(header.strip, base: base) }
|
47
71
|
end
|
48
72
|
|
49
73
|
def members
|
data/link-header-parser.gemspec
CHANGED
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:
|
4
|
+
version: 5.0.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: 2022-
|
11
|
+
date: 2022-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Parse HTTP Link headers.
|
14
14
|
email:
|
@@ -32,7 +32,7 @@ licenses:
|
|
32
32
|
- MIT
|
33
33
|
metadata:
|
34
34
|
bug_tracker_uri: https://github.com/jgarber623/link-header-parser-ruby/issues
|
35
|
-
changelog_uri: https://github.com/jgarber623/link-header-parser-ruby/blob/
|
35
|
+
changelog_uri: https://github.com/jgarber623/link-header-parser-ruby/blob/v5.0.0/CHANGELOG.md
|
36
36
|
rubygems_mfa_required: 'true'
|
37
37
|
post_install_message:
|
38
38
|
rdoc_options: []
|
@@ -42,7 +42,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
42
|
requirements:
|
43
43
|
- - ">="
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version: '2.
|
45
|
+
version: '2.7'
|
46
46
|
- - "<"
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '4'
|
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '0'
|
54
54
|
requirements: []
|
55
|
-
rubygems_version: 3.
|
55
|
+
rubygems_version: 3.3.16
|
56
56
|
signing_key:
|
57
57
|
specification_version: 4
|
58
58
|
summary: Parse HTTP Link headers.
|