Skip to content

Commit eba8115

Browse files
committed
🚜 Refactor Tractor 🚜
This commit represents a nearly complete rewrite of the gem's code with (hopefully) zero change to the user-facing API. There are _many_ fewer files and the end results should be the same.
1 parent 0fe6cc3 commit eba8115

16 files changed

+168
-238
lines changed

indieweb-endpoints.gemspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ Gem::Specification.new do |spec|
2727
'rubygems_mfa_required' => 'true'
2828
}
2929

30-
spec.add_runtime_dependency 'addressable', '~> 2.8'
3130
spec.add_runtime_dependency 'http', '~> 5.0'
3231
spec.add_runtime_dependency 'link-header-parser', '~> 4.0'
33-
spec.add_runtime_dependency 'nokogiri', '~> 1.12'
32+
spec.add_runtime_dependency 'nokogiri', '~> 1.13'
3433
end

lib/indieweb/endpoints.rb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
# frozen_string_literal: true
22

3-
require 'addressable/uri'
43
require 'http'
54
require 'link-header-parser'
65
require 'nokogiri'
76

87
require_relative 'endpoints/version'
9-
require_relative 'endpoints/exceptions'
10-
11-
require_relative 'endpoints/services/response_parser_service'
128

139
require_relative 'endpoints/client'
14-
require_relative 'endpoints/parsers'
15-
16-
require_relative 'endpoints/parsers/base_parser'
17-
require_relative 'endpoints/parsers/authorization_endpoint_parser'
18-
require_relative 'endpoints/parsers/micropub_parser'
19-
require_relative 'endpoints/parsers/microsub_parser'
20-
require_relative 'endpoints/parsers/redirect_uri_parser'
21-
require_relative 'endpoints/parsers/token_endpoint_parser'
22-
require_relative 'endpoints/parsers/webmention_parser'
10+
require_relative 'endpoints/parser'
11+
require_relative 'endpoints/response_body_parser'
12+
require_relative 'endpoints/response_headers_parser'
2313

2414
module IndieWeb
2515
module Endpoints
16+
class Error < StandardError; end
17+
class HttpError < Error; end
18+
class InvalidURIError < Error; end
19+
2620
# Discover a URL's IndieAuth, Micropub, Microsub, and Webmention endpoints
2721
#
2822
# IndieWeb::Endpoints.get('https://aaronparecki.com')

lib/indieweb/endpoints/client.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,43 @@ class Client
1212
#
1313
# client = IndieWeb::Endpoints::Client.new('https://aaronparecki.com')
1414
#
15-
# @param url [String] an absolute URL
15+
# @param url [String, HTTP::URI, #to_s] an absolute URL
16+
# @raise [IndieWeb::Endpoints::InvalidURIError]
1617
def initialize(url)
17-
@url = url.to_str
18+
@uri = HTTP::URI.parse(url.to_s)
19+
rescue Addressable::URI::InvalidURIError => e
20+
raise InvalidURIError, e
1821
end
1922

2023
# @return [String]
2124
def inspect
22-
"#<#{self.class.name}:#{format('%#0x', object_id)} url: #{url.inspect}>"
25+
%(#<#{self.class.name}:#{format('%#0x', object_id)} uri: "#{uri}">)
2326
end
2427

28+
# A Hash of the discovered IndieWeb endpoints from the provided URL
29+
#
2530
# @return [Hash{Symbol => String, Array, nil}]
2631
def endpoints
27-
@endpoints ||= Parsers.registered.transform_values { |parser| parser.new(response).results }
32+
@endpoints ||= Parser.new(response).results
2833
end
2934

30-
# @see https://www.w3.org/TR/webmention/#limits-on-get-requests
35+
# The HTTP::Response object returned by the provided URL
3136
#
3237
# @return [HTTP::Response]
38+
# @raise [IndieWeb::Endpoints::HttpError]
3339
def response
34-
@response ||= HTTP.follow(max_hops: 20).headers(HTTP_HEADERS_OPTS).timeout(connect: 5, read: 5).get(uri)
40+
@response ||= HTTP.follow(max_hops: 20)
41+
.headers(HTTP_HEADERS_OPTS)
42+
.timeout(connect: 5, read: 5)
43+
.get(uri)
3544
rescue HTTP::Error => e
3645
raise HttpError, e
3746
end
3847

3948
private
4049

41-
attr_accessor :url
42-
43-
def uri
44-
@uri ||= Addressable::URI.parse(url)
45-
rescue Addressable::URI::InvalidURIError => e
46-
raise InvalidURIError, e
47-
end
50+
# @return [HTTP::URI]
51+
attr_reader :uri
4852
end
4953
end
5054
end

lib/indieweb/endpoints/exceptions.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/indieweb/endpoints/parser.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
module IndieWeb
4+
module Endpoints
5+
# @api private
6+
class Parser
7+
# @param response [HTTP::Response]
8+
def initialize(response)
9+
@response = response
10+
end
11+
12+
# @return [Hash{Symbol => String, Array<String>, nil}]
13+
def results
14+
{
15+
authorization_endpoint: result_for(:authorization_endpoint),
16+
micropub: result_for(:micropub),
17+
microsub: result_for(:microsub),
18+
redirect_uri: results_for(:redirect_uri),
19+
token_endpoint: result_for(:token_endpoint),
20+
webmention: result_for(:webmention, %w[link a])
21+
}
22+
end
23+
24+
private
25+
26+
# @return [HTTP::Response]
27+
attr_reader :response
28+
29+
# @return [IndieWeb::Endpoints::ResponseBodyParser]
30+
def response_body_parser
31+
@response_body_parser ||= ResponseBodyParser.new(response)
32+
end
33+
34+
# @return [IndieWeb::Endpoints::ResponseHeadersParser]
35+
def response_headers_parser
36+
@response_headers_parser ||= ResponseHeadersParser.new(response)
37+
end
38+
39+
# @param identifier [Symbol]
40+
# @param nodes [Array<String>]
41+
# @return [String, nil]
42+
def result_for(identifier, nodes = ['link'])
43+
results_for(identifier, nodes)&.first
44+
end
45+
46+
# @param identifier [Symbol]
47+
# @param nodes [Array<String>]
48+
# @return [Array<String>, nil]
49+
# @raise [IndieWeb::Endpoints::InvalidURIError]
50+
def results_for(identifier, nodes = ['link'])
51+
results_from_request = [
52+
response_headers_parser.results_for(identifier),
53+
response_body_parser.results_for(identifier, nodes)
54+
].flatten.compact
55+
56+
return if results_from_request.none?
57+
58+
results_from_request.map { |endpoint| response.uri.join(endpoint).to_s }.uniq.sort
59+
rescue Addressable::URI::InvalidURIError => e
60+
raise InvalidURIError, e
61+
end
62+
end
63+
end
64+
end

lib/indieweb/endpoints/parsers.rb

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/indieweb/endpoints/parsers/authorization_endpoint_parser.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/indieweb/endpoints/parsers/base_parser.rb

Lines changed: 0 additions & 59 deletions
This file was deleted.

lib/indieweb/endpoints/parsers/micropub_parser.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/indieweb/endpoints/parsers/microsub_parser.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)