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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2af19382d4d537959b66ca55c6fcfab4cc52cb552748e6cc9cbf7d18176491e1
4
- data.tar.gz: f576800eaa08e75441edf34b65bac17302cbff52ad161ad84e5604fdd8a66890
3
+ metadata.gz: 39406f09a5e4cfecc6d2e1918b947b4fef83ce65915f93af711fc816b92d3760
4
+ data.tar.gz: 10e5a92616f6db295efbeb544dd650b2ca8099f98bb269c4dc8ec45540ee4fc7
5
5
  SHA512:
6
- metadata.gz: d0823f00fe68edffc867354972a4e3efa4f7f4ebe5cd4ba14ff0dbefe7a291531daca5b789f3392d0b53a9231edc1261f4ea01c094545a74119aed0c9c7f668d
7
- data.tar.gz: 9bf55d725122524374d47024900768bb497239422e826a0d07f66052e091dc950f982e8df3d4275d75837d8e4b3e19bbaa92a8aa5f937299f2429b66e02ee215
6
+ metadata.gz: 9d64a96e22925e26599216e3c6e3e27f90b546a9dea6d7aba08fd6566240ec25688e674424d2803eed7d63c424477c8f02841b9f0061e0100928f3c37d146628
7
+ data.tar.gz: 6d2951bdd0d745f7ae9176f63bb0e5f7c9f2ecab722252f8f5c557e03ebb2afea6a41494a0d697f7ef9e73eb699150d4763d31e2246ae1ebf970ba19f0ac7e26
@@ -8,6 +8,10 @@ Layout/AlignHash:
8
8
  Metrics/LineLength:
9
9
  Enabled: false
10
10
 
11
+ Metrics/ModuleLength:
12
+ Exclude:
13
+ - spec/support/**/*.rb
14
+
11
15
  Naming/FileName:
12
16
  Exclude:
13
17
  - lib/link-header-parser.rb
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
- 0.1.0 / 2019-06-06
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
- puts collection.relation_types # => ["preconnect", "webmention"]
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
- 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` (as demonstrated above).
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
- Additionally, `collection.by_relation_type` returns an `OpenStruct` with the following attributes:
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
- Building on this example, you may interact with one or more `ParsedHeader`s in a `ParsedHeaderCollection`:
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
- parsed_header = collection.first
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.relation_types # => ['preconnect']
75
- parsed_header.relations # => 'preconnect'
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 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).
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
@@ -1,3 +1,3 @@
1
1
  module LinkHeaderParser
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -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.71.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.1.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-06-07 00:00:00.000000000 Z
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.71.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.71.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.1.0/CHANGELOG.md
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: