indieweb-endpoints 9.0.0 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +13 -10
- data/README.md +5 -13
- data/indieweb-endpoints.gemspec +10 -10
- data/lib/indieweb/endpoints/client.rb +11 -9
- data/lib/indieweb/endpoints/parser.rb +55 -34
- data/lib/indieweb/endpoints.rb +4 -7
- metadata +12 -16
- data/lib/indieweb/endpoints/response_body_parser.rb +0 -43
- data/lib/indieweb/endpoints/response_headers_parser.rb +0 -36
- data/lib/indieweb/endpoints/version.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da8f18ac80e24b04e834e41ef4ec959cc26c3496b888de60c92d518fb89bbc78
|
4
|
+
data.tar.gz: 64a2117a77c161ab5d4c5b616c4fe1685406ea58218ade5af72eb2998048f402
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54fdc8310a995d36c8f46e8926790bafd8352a5db02e32144a4d4ce074258bc5c7ca80cc9778d910a522749f4b9807eb5cd202465ee8d811ca5ee4d4aa6311f7
|
7
|
+
data.tar.gz: 1d36d41eccca9f32ccecd622bc739915af4af9f61596fabf48d994e4d6f741ae4c8013a064c9b908917bf6896ac4ad291df8a90e882f074a3b1a12dbcc23b76b
|
data/CONTRIBUTING.md
CHANGED
@@ -8,9 +8,12 @@ There are a couple ways you can help improve indieweb-endpoints-ruby:
|
|
8
8
|
|
9
9
|
## Getting Started
|
10
10
|
|
11
|
-
indieweb-endpoints-ruby is developed using Ruby 3.
|
11
|
+
indieweb-endpoints-ruby is developed using Ruby 3.4 and is tested against additional Ruby versions using [GitHub Actions](https://github.com/indieweb/indieweb-endpoints-ruby/actions).
|
12
12
|
|
13
|
-
|
13
|
+
> [!TIP]
|
14
|
+
> This project is configured with a [Dev Container](https://containers.dev) which includes everything you'd need to contribute to indieweb-endpoints-ruby. If you use a supported code editor or IDE, you're encouraged to use the existing Dev Container setup.
|
15
|
+
|
16
|
+
Before making changes to indieweb-endpoints-ruby, you'll want to install Ruby 3.4. Using 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) is recommended. Once you've installed Ruby 3.4 using your method of choice, install the project's gems by running:
|
14
17
|
|
15
18
|
```sh
|
16
19
|
bundle install
|
@@ -19,17 +22,17 @@ bundle install
|
|
19
22
|
## Making Changes
|
20
23
|
|
21
24
|
1. Fork and clone the project's repo.
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
2. Install development dependencies as outlined above.
|
26
|
+
3. Create a feature branch for the code changes you're looking to make: `git checkout -b my-new-feature`.
|
27
|
+
4. _Write some code!_
|
28
|
+
5. If your changes would benefit from testing, add the necessary tests and verify everything passes by running `bundle exec rspec`.
|
29
|
+
6. Commit your changes: `git commit -am 'Add some new feature or fix some issue'`. _(See [this excellent article](https://cbea.ms/git-commit/) for tips on writing useful Git commit messages.)_
|
30
|
+
7. Push the branch to your fork: `git push -u origin my-new-feature`.
|
31
|
+
8. Create a new [Pull Request][pulls] and we'll review your changes.
|
29
32
|
|
30
33
|
## Code Style
|
31
34
|
|
32
|
-
Code formatting conventions are defined in the `.editorconfig` file which uses the [EditorConfig](
|
35
|
+
Code formatting conventions are defined in the `.editorconfig` file which uses the [EditorConfig](https://editorconfig.org) syntax. There are [plugins for a variety of editors](https://editorconfig.org/#download) that utilize the settings in the `.editorconfig` file. We recommended installing the EditorConfig plugin for your editor of choice.
|
33
36
|
|
34
37
|
Your bug fix or feature addition won't be rejected if it runs afoul of any (or all) of these guidelines, but following the guidelines will definitely make everyone's lives a little easier.
|
35
38
|
|
data/README.md
CHANGED
@@ -10,13 +10,13 @@
|
|
10
10
|
|
11
11
|
- Compliant with [Section 4.1](https://www.w3.org/TR/indieauth/#discovery-by-clients) and [Section 4.2.2](https://www.w3.org/TR/indieauth/#redirect-url) of [the W3C's IndieAuth Working Group Note](https://www.w3.org/TR/indieauth/), [Section 5.3](https://www.w3.org/TR/micropub/#endpoint-discovery) of [the W3C's Micropub Recommendation](https://www.w3.org/TR/micropub/), and [Section 3.1.2](https://www.w3.org/TR/webmention/#sender-discovers-receiver-webmention-endpoint) of [the W3C's Webmention Recommendation](https://www.w3.org/TR/webmention/).
|
12
12
|
- Passes all Endpoint Discovery tests on [webmention.rocks](https://webmention.rocks).
|
13
|
-
- Supports Ruby
|
13
|
+
- Supports Ruby 2.6 and newer.
|
14
14
|
|
15
15
|
## Getting Started
|
16
16
|
|
17
|
-
Before installing and using indieweb-endpoints-ruby, you'll want to have [Ruby](https://www.ruby-lang.org)
|
17
|
+
Before installing and using indieweb-endpoints-ruby, you'll want to have [Ruby](https://www.ruby-lang.org) 2.6 (or newer) installed. Using 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) is recommended.
|
18
18
|
|
19
|
-
indieweb-endpoints-ruby is developed using Ruby 3.
|
19
|
+
indieweb-endpoints-ruby is developed using Ruby 3.4 and is tested against additional Ruby versions using [GitHub Actions](https://github.com/indieweb/indieweb-endpoints-ruby/actions).
|
20
20
|
|
21
21
|
## Installation
|
22
22
|
|
@@ -41,7 +41,7 @@ IndieWeb::Endpoints.get("https://aaronparecki.com")
|
|
41
41
|
#=> { authorization_endpoint: "https://aaronparecki.com/auth", "indieauth-metadata": "https://aaronparecki.com/.well-known/oauth-authorization-server", micropub: "https://aaronparecki.com/micropub", microsub: "https://aperture.p3k.io/microsub/1", redirect_uri: nil, token_endpoint: "https://aaronparecki.com/auth/token", webmention: "https://webmention.io/aaronpk/webmention" }
|
42
42
|
```
|
43
43
|
|
44
|
-
This example will search
|
44
|
+
This example will search [Aaron's website](https://aaronparecki.com) for valid IndieAuth, Micropub, and Webmention endpoints and return a `Hash` of results. Each key in the returned `Hash` will have a value of either a `String` representing a URL or `nil`. The `redirect_uri` key's value will be either an `Array` or `nil` since a given URL may register multiple callback URLs.
|
45
45
|
|
46
46
|
### Advanced Usage
|
47
47
|
|
@@ -62,18 +62,10 @@ client.endpoints
|
|
62
62
|
|
63
63
|
### Exception Handling
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
From [sporkmonger/addressable](https://github.com/sporkmonger/addressable):
|
65
|
+
indieweb-endpoints-ruby may raise the following exceptions which are subclasses of `IndieWeb::Endpoints::Error` (which itself is a subclass of `StandardError`).
|
68
66
|
|
69
67
|
- `IndieWeb::Endpoints::InvalidURIError`
|
70
|
-
|
71
|
-
From [httprb/http](https://github.com/httprb/http):
|
72
|
-
|
73
68
|
- `IndieWeb::Endpoints::HttpError`
|
74
|
-
|
75
|
-
From the Ruby Standard Library's [`OpenSSL::SSL::SSLError`](https://ruby-doc.org/3.3.0/exts/openssl/OpenSSL/SSL/SSLError.html):
|
76
|
-
|
77
69
|
- `IndieWeb::Endpoints::SSLError`
|
78
70
|
|
79
71
|
## Contributing
|
data/indieweb-endpoints.gemspec
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "lib/indieweb/endpoints/version"
|
4
|
-
|
5
3
|
Gem::Specification.new do |spec|
|
6
|
-
spec.required_ruby_version = ">=
|
4
|
+
spec.required_ruby_version = ">= 2.6"
|
7
5
|
|
8
6
|
spec.name = "indieweb-endpoints"
|
9
|
-
spec.version =
|
7
|
+
spec.version = "9.1.0"
|
10
8
|
spec.authors = ["Jason Garber"]
|
11
9
|
spec.email = ["jason@sixtwothree.org"]
|
12
10
|
|
@@ -16,19 +14,21 @@ Gem::Specification.new do |spec|
|
|
16
14
|
spec.license = "MIT"
|
17
15
|
|
18
16
|
spec.files = Dir["lib/**/*"].reject { |f| File.directory?(f) }
|
19
|
-
spec.files +=
|
20
|
-
spec.files +=
|
17
|
+
spec.files += ["LICENSE", "CHANGELOG.md", "CODE_OF_CONDUCT.md", "CONTRIBUTING.md", "README.md"]
|
18
|
+
spec.files += ["indieweb-endpoints.gemspec"]
|
21
19
|
|
22
20
|
spec.require_paths = ["lib"]
|
23
21
|
|
24
22
|
spec.metadata = {
|
25
23
|
"bug_tracker_uri" => "#{spec.homepage}/issues",
|
26
24
|
"changelog_uri" => "#{spec.homepage}/releases/tag/v#{spec.version}",
|
25
|
+
"documentation_uri" => "https://rubydoc.info/gems/#{spec.name}/#{spec.version}",
|
26
|
+
"homepage_uri" => spec.homepage,
|
27
27
|
"rubygems_mfa_required" => "true",
|
28
|
-
"source_code_uri" => "#{spec.homepage}/tree/v#{spec.version}"
|
28
|
+
"source_code_uri" => "#{spec.homepage}/tree/v#{spec.version}",
|
29
29
|
}
|
30
30
|
|
31
|
-
spec.
|
32
|
-
spec.
|
33
|
-
spec.
|
31
|
+
spec.add_dependency "http", "~> 5.2"
|
32
|
+
spec.add_dependency "link-header-parser", "~> 6.1"
|
33
|
+
spec.add_dependency "nokogiri", ">= 1.13"
|
34
34
|
end
|
@@ -5,7 +5,7 @@ module IndieWeb
|
|
5
5
|
class Client
|
6
6
|
HTTP_HEADERS_OPTS = {
|
7
7
|
accept: "*/*",
|
8
|
-
user_agent: "IndieWeb Endpoint Discovery (https://rubygems.org/gems/indieweb-endpoints)"
|
8
|
+
user_agent: "IndieWeb Endpoint Discovery (https://rubygems.org/gems/indieweb-endpoints)",
|
9
9
|
}.freeze
|
10
10
|
|
11
11
|
private_constant :HTTP_HEADERS_OPTS
|
@@ -16,7 +16,8 @@ module IndieWeb
|
|
16
16
|
# client = IndieWeb::Endpoints::Client.new("https://aaronparecki.com")
|
17
17
|
#
|
18
18
|
# @param url [String, HTTP::URI, #to_s] an absolute URL
|
19
|
-
#
|
19
|
+
#
|
20
|
+
# @raise [InvalidURIError]
|
20
21
|
def initialize(url)
|
21
22
|
@uri = HTTP::URI.parse(url.to_s)
|
22
23
|
rescue Addressable::URI::InvalidURIError => e
|
@@ -32,19 +33,20 @@ module IndieWeb
|
|
32
33
|
#
|
33
34
|
# @return [Hash{Symbol => String, Array, nil}]
|
34
35
|
def endpoints
|
35
|
-
@endpoints ||= Parser.new(response).
|
36
|
+
@endpoints ||= Parser.new(response).to_h
|
36
37
|
end
|
37
38
|
|
38
|
-
# The
|
39
|
+
# The {HTTP::Response} object returned by the provided URL.
|
39
40
|
#
|
40
41
|
# @return [HTTP::Response]
|
41
|
-
#
|
42
|
+
#
|
43
|
+
# @raise [HttpError, SSLError]
|
42
44
|
def response
|
43
45
|
@response ||= HTTP
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
.follow(max_hops: 20)
|
47
|
+
.headers(HTTP_HEADERS_OPTS)
|
48
|
+
.timeout(connect: 5, read: 5)
|
49
|
+
.get(uri)
|
48
50
|
rescue HTTP::Error => e
|
49
51
|
raise HttpError, e
|
50
52
|
rescue OpenSSL::SSL::SSLError => e
|
@@ -9,16 +9,43 @@ module IndieWeb
|
|
9
9
|
@response = response
|
10
10
|
end
|
11
11
|
|
12
|
-
# @
|
13
|
-
|
12
|
+
# @param identifier [String]
|
13
|
+
# @param node_names [Array<String>]
|
14
|
+
#
|
15
|
+
# @return [Array<String>]
|
16
|
+
#
|
17
|
+
# @raise [InvalidURIError]
|
18
|
+
def matches(identifier, node_names: ["link"])
|
19
|
+
results =
|
20
|
+
(matches_from_headers(identifier) + matches_from_body(identifier, node_names))
|
21
|
+
.compact
|
22
|
+
.map! { |endpoint| response.uri.join(endpoint).to_s }
|
23
|
+
|
24
|
+
results.uniq!
|
25
|
+
results.sort!
|
26
|
+
|
27
|
+
results
|
28
|
+
rescue Addressable::URI::InvalidURIError => e
|
29
|
+
raise InvalidURIError, e
|
30
|
+
end
|
31
|
+
|
32
|
+
# @param (see #matches)
|
33
|
+
#
|
34
|
+
# @return [String]
|
35
|
+
def match(identifier, **kwargs)
|
36
|
+
matches(identifier, **kwargs).first
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Hash{Symbol => String, Array, nil}]
|
40
|
+
def to_h
|
14
41
|
{
|
15
|
-
authorization_endpoint:
|
16
|
-
"indieauth-metadata":
|
17
|
-
micropub:
|
18
|
-
microsub:
|
19
|
-
redirect_uri:
|
20
|
-
token_endpoint:
|
21
|
-
webmention:
|
42
|
+
authorization_endpoint: match("authorization_endpoint"),
|
43
|
+
"indieauth-metadata": match("indieauth-metadata"),
|
44
|
+
micropub: match("micropub"),
|
45
|
+
microsub: match("microsub"),
|
46
|
+
redirect_uri: (redirect_uri = matches("redirect_uri")).any? ? redirect_uri : nil,
|
47
|
+
token_endpoint: match("token_endpoint"),
|
48
|
+
webmention: match("webmention", node_names: ["link", "a"]),
|
22
49
|
}
|
23
50
|
end
|
24
51
|
|
@@ -27,38 +54,32 @@ module IndieWeb
|
|
27
54
|
# @return [HTTP::Response]
|
28
55
|
attr_reader :response
|
29
56
|
|
30
|
-
# @return [
|
31
|
-
def
|
32
|
-
@
|
57
|
+
# @return [Nokogiri::HTML5::Document]
|
58
|
+
def body
|
59
|
+
@body ||= Nokogiri::HTML5(response.body)
|
33
60
|
end
|
34
61
|
|
35
|
-
# @return [
|
36
|
-
def
|
37
|
-
@
|
62
|
+
# @return [Hash{Symbol => Array<LinkHeaderParser::LinkHeader>}]
|
63
|
+
def headers
|
64
|
+
@headers ||= LinkHeaderParser.parse(response.headers.get("link"), base: response.uri).group_by_relation_type
|
38
65
|
end
|
39
66
|
|
40
|
-
# @
|
41
|
-
|
42
|
-
|
43
|
-
def result_for(identifier, nodes = ["link"])
|
44
|
-
results_for(identifier, nodes)&.first
|
45
|
-
end
|
67
|
+
# @return [Array<String>]
|
68
|
+
def matches_from_body(identifier, node_names)
|
69
|
+
return [] unless response.mime_type == "text/html"
|
46
70
|
|
47
|
-
|
48
|
-
|
49
|
-
# @return [Array<String>, nil]
|
50
|
-
# @raise [IndieWeb::Endpoints::InvalidURIError]
|
51
|
-
def results_for(identifier, nodes = ["link"])
|
52
|
-
results_from_request = [
|
53
|
-
response_headers_parser.results_for(identifier),
|
54
|
-
response_body_parser.results_for(identifier, nodes)
|
55
|
-
].flatten.compact
|
71
|
+
# Reject endpoints that contain a fragment identifier.
|
72
|
+
selectors = node_names.map { |node| %(#{node}[rel~="#{identifier}"][href]:not([href*="#"])) }.join(",")
|
56
73
|
|
57
|
-
|
74
|
+
body.css(selectors).map { |element| element["href"] }
|
75
|
+
end
|
58
76
|
|
59
|
-
|
60
|
-
|
61
|
-
|
77
|
+
# @return [Array<String>]
|
78
|
+
def matches_from_headers(identifier)
|
79
|
+
# Reject endpoints that contain a fragment identifier.
|
80
|
+
Array(headers[identifier.to_sym])
|
81
|
+
.filter { |header| !HTTP::URI.parse(header.target_uri).fragment }
|
82
|
+
.map(&:target_uri)
|
62
83
|
end
|
63
84
|
end
|
64
85
|
end
|
data/lib/indieweb/endpoints.rb
CHANGED
@@ -4,12 +4,8 @@ require "http"
|
|
4
4
|
require "link-header-parser"
|
5
5
|
require "nokogiri"
|
6
6
|
|
7
|
-
require_relative "endpoints/version"
|
8
|
-
|
9
7
|
require_relative "endpoints/client"
|
10
8
|
require_relative "endpoints/parser"
|
11
|
-
require_relative "endpoints/response_body_parser"
|
12
|
-
require_relative "endpoints/response_headers_parser"
|
13
9
|
|
14
10
|
module IndieWeb
|
15
11
|
module Endpoints
|
@@ -23,13 +19,14 @@ module IndieWeb
|
|
23
19
|
|
24
20
|
# Discover a URL's IndieAuth, Micropub, Microsub, and Webmention endpoints.
|
25
21
|
#
|
26
|
-
# Convenience method for {
|
22
|
+
# Convenience method for {Client#endpoints}.
|
27
23
|
#
|
28
24
|
# @example
|
29
25
|
# IndieWeb::Endpoints.get("https://aaronparecki.com")
|
30
26
|
#
|
31
|
-
# @param (see
|
32
|
-
#
|
27
|
+
# @param (see Client#initialize)
|
28
|
+
#
|
29
|
+
# @return (see Client#endpoints)
|
33
30
|
def self.get(url)
|
34
31
|
Client.new(url).endpoints
|
35
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: indieweb-endpoints
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 9.
|
4
|
+
version: 9.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Garber
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: http
|
@@ -30,28 +29,28 @@ dependencies:
|
|
30
29
|
requirements:
|
31
30
|
- - "~>"
|
32
31
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
32
|
+
version: '6.1'
|
34
33
|
type: :runtime
|
35
34
|
prerelease: false
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
37
36
|
requirements:
|
38
37
|
- - "~>"
|
39
38
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
39
|
+
version: '6.1'
|
41
40
|
- !ruby/object:Gem::Dependency
|
42
41
|
name: nokogiri
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
44
43
|
requirements:
|
45
44
|
- - ">="
|
46
45
|
- !ruby/object:Gem::Version
|
47
|
-
version: '1.
|
46
|
+
version: '1.13'
|
48
47
|
type: :runtime
|
49
48
|
prerelease: false
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
51
50
|
requirements:
|
52
51
|
- - ">="
|
53
52
|
- !ruby/object:Gem::Version
|
54
|
-
version: '1.
|
53
|
+
version: '1.13'
|
55
54
|
description: Discover a URL’s IndieAuth, Micropub, Microsub, and Webmention endpoints.
|
56
55
|
email:
|
57
56
|
- jason@sixtwothree.org
|
@@ -68,18 +67,16 @@ files:
|
|
68
67
|
- lib/indieweb/endpoints.rb
|
69
68
|
- lib/indieweb/endpoints/client.rb
|
70
69
|
- lib/indieweb/endpoints/parser.rb
|
71
|
-
- lib/indieweb/endpoints/response_body_parser.rb
|
72
|
-
- lib/indieweb/endpoints/response_headers_parser.rb
|
73
|
-
- lib/indieweb/endpoints/version.rb
|
74
70
|
homepage: https://github.com/indieweb/indieweb-endpoints-ruby
|
75
71
|
licenses:
|
76
72
|
- MIT
|
77
73
|
metadata:
|
78
74
|
bug_tracker_uri: https://github.com/indieweb/indieweb-endpoints-ruby/issues
|
79
|
-
changelog_uri: https://github.com/indieweb/indieweb-endpoints-ruby/releases/tag/v9.
|
75
|
+
changelog_uri: https://github.com/indieweb/indieweb-endpoints-ruby/releases/tag/v9.1.0
|
76
|
+
documentation_uri: https://rubydoc.info/gems/indieweb-endpoints/9.1.0
|
77
|
+
homepage_uri: https://github.com/indieweb/indieweb-endpoints-ruby
|
80
78
|
rubygems_mfa_required: 'true'
|
81
|
-
source_code_uri: https://github.com/indieweb/indieweb-endpoints-ruby/tree/v9.
|
82
|
-
post_install_message:
|
79
|
+
source_code_uri: https://github.com/indieweb/indieweb-endpoints-ruby/tree/v9.1.0
|
83
80
|
rdoc_options: []
|
84
81
|
require_paths:
|
85
82
|
- lib
|
@@ -87,15 +84,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
84
|
requirements:
|
88
85
|
- - ">="
|
89
86
|
- !ruby/object:Gem::Version
|
90
|
-
version: '
|
87
|
+
version: '2.6'
|
91
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
89
|
requirements:
|
93
90
|
- - ">="
|
94
91
|
- !ruby/object:Gem::Version
|
95
92
|
version: '0'
|
96
93
|
requirements: []
|
97
|
-
rubygems_version: 3.
|
98
|
-
signing_key:
|
94
|
+
rubygems_version: 3.6.7
|
99
95
|
specification_version: 4
|
100
96
|
summary: Discover a URL’s IndieAuth, Micropub, Microsub, and Webmention endpoints.
|
101
97
|
test_files: []
|
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module IndieWeb
|
4
|
-
module Endpoints
|
5
|
-
# @api private
|
6
|
-
class ResponseBodyParser
|
7
|
-
# @param response [HTTP::Response]
|
8
|
-
def initialize(response)
|
9
|
-
@body = response.body.to_s
|
10
|
-
@mime_type = response.mime_type
|
11
|
-
@uri = response.uri
|
12
|
-
end
|
13
|
-
|
14
|
-
# @param identifier [Symbol]
|
15
|
-
# @param nodes [Array<String>]
|
16
|
-
# @return [Array<string>, nil]
|
17
|
-
def results_for(identifier, nodes = ["link"])
|
18
|
-
return unless mime_type == "text/html"
|
19
|
-
|
20
|
-
# Reject endpoints that contain a fragment identifier
|
21
|
-
selectors = nodes.map { |node| %(#{node}[rel~="#{identifier}"][href]:not([href*="#"])) }.join(",")
|
22
|
-
|
23
|
-
parsed_body.css(selectors).map { |element| element["href"] }
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
# @return [String]
|
29
|
-
attr_reader :body
|
30
|
-
|
31
|
-
# @return [String]
|
32
|
-
attr_reader :mime_type
|
33
|
-
|
34
|
-
# @return [HTTP::URI]
|
35
|
-
attr_reader :uri
|
36
|
-
|
37
|
-
# @return [Nokogiri::HTML5::Document]
|
38
|
-
def parsed_body
|
39
|
-
@parsed_body ||= Nokogiri::HTML5(body)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module IndieWeb
|
4
|
-
module Endpoints
|
5
|
-
# @api private
|
6
|
-
class ResponseHeadersParser
|
7
|
-
# @param response [HTTP::Response]
|
8
|
-
def initialize(response)
|
9
|
-
@headers = response.headers.get("link")
|
10
|
-
@uri = response.uri
|
11
|
-
end
|
12
|
-
|
13
|
-
# @param identifier [Symbol]
|
14
|
-
# @return [Array<String>, nil]
|
15
|
-
def results_for(identifier)
|
16
|
-
# Reject endpoints that contain a fragment identifier
|
17
|
-
parsed_headers[identifier]&.filter_map do |header|
|
18
|
-
header.target_uri unless HTTP::URI.parse(header.target_uri).fragment
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
# @return [Array<String>]
|
25
|
-
attr_reader :headers
|
26
|
-
|
27
|
-
# @return [HTTP::URI]
|
28
|
-
attr_reader :uri
|
29
|
-
|
30
|
-
# @return [Hash{Symbol => Array<LinkHeaderParser::LinkHeader>}]
|
31
|
-
def parsed_headers
|
32
|
-
@parsed_headers ||= LinkHeaderParser.parse(headers, base: uri).group_by_relation_type
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|