Skip to content

Commit e22e0af

Browse files
committed
Refactor parsers
1 parent 65361ee commit e22e0af

4 files changed

Lines changed: 23 additions & 22 deletions

File tree

lib/indieweb/endpoints/parsers/base_parser.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ def mapped_results
2828
raise InvalidURIError, exception
2929
end
3030

31+
def parsed_response_body
32+
@parsed_response_body ||= Nokogiri::HTML(response.body.to_s)
33+
end
34+
35+
def parsed_response_headers
36+
@parsed_response_headers ||= LinkHeaderParser.parse(response.headers.get('link'), base: response.uri)
37+
end
38+
3139
def results_from_body
32-
@results_from_body ||= Services::ResponseParserService.parse_body(response, self.class.identifier)
40+
return unless response.mime_type == 'text/html'
41+
42+
Services::ResponseParserService.parse_body(parsed_response_body, self.class.identifier)
3343
end
3444

3545
def results_from_headers
36-
@results_from_headers ||= Services::ResponseParserService.parse_headers(response, self.class.identifier)
46+
return if parsed_response_headers.none?
47+
48+
Services::ResponseParserService.parse_headers(parsed_response_headers, self.class.identifier)
3749
end
3850

3951
def results_from_http_request

lib/indieweb/endpoints/parsers/webmention_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class WebmentionParser < BaseParser
99
private
1010

1111
def results_for_node(node)
12-
Services::ResponseParserService.parse_body(response, self.class.identifier, node)
12+
Services::ResponseParserService.parse_body(parsed_response_body, self.class.identifier, node)
1313
end
1414

1515
# https://www.w3.org/TR/webmention/#sender-discovers-receiver-webmention-endpoint

lib/indieweb/endpoints/services/response_parser_service.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@ module IndieWeb
22
module Endpoints
33
module Services
44
class ResponseParserService
5-
# @param response [HTTP::Response]
5+
# @param body [Nokogiri::HTML::Document]
66
# @param identifier [Symbol]
77
# @return [Array<String>]
8-
def self.parse_body(response, identifier, node = 'link')
9-
return unless response.mime_type == 'text/html'
10-
8+
def self.parse_body(body, identifier, node = 'link')
119
# Reject endpoints that contain a fragment identifier
12-
Nokogiri::HTML(response.body.to_s).css(%(#{node}[rel~="#{identifier}"][href]:not([href*="#"]))).map { |element| element['href'] }
10+
body.css(%(#{node}[rel~="#{identifier}"][href]:not([href*="#"]))).map { |element| element['href'] }
1311
end
1412

15-
# @param response [HTTP::Response]
13+
# @param headers [LinkHeaderParser::LinkHeadersCollection]
1614
# @param identifier [Symbol]
1715
# @return [Array<String>, nil]
18-
def self.parse_headers(response, identifier)
19-
headers = LinkHeaderParser.parse(response.headers.get('link'), base: response.uri.to_s).group_by_relation_type[identifier]
16+
def self.parse_headers(headers, identifier)
17+
headers_for_identifier = headers.group_by_relation_type[identifier]
2018

21-
return unless headers
19+
return unless headers_for_identifier
2220

2321
# Reject endpoints that contain a fragment identifier
24-
headers.reject { |header| Addressable::URI.parse(header.target_uri).fragment }.map(&:target_uri)
22+
headers_for_identifier.reject { |header| Addressable::URI.parse(header.target_uri).fragment }.map(&:target_uri)
2523
end
2624
end
2725
end

spec/lib/indieweb/endpoints/parsers/base_parser_spec.rb

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

0 commit comments

Comments
 (0)