class IndieWeb::Endpoints::Parsers::BaseParser

Attributes

identifier[R]
response[R]

Public Class Methods

new(response) click to toggle source

@param response [HTTP::Response]

# File lib/indieweb/endpoints/parsers/base_parser.rb, line 10
def initialize(response)
  raise ArgumentError, "response must be an HTTP::Response (given #{response.class.name})" unless response.is_a?(HTTP::Response)

  @response = response
end

Public Instance Methods

results() click to toggle source

@return [String]

# File lib/indieweb/endpoints/parsers/base_parser.rb, line 17
def results
  mapped_results.shift
end

Private Instance Methods

mapped_results() click to toggle source
# File lib/indieweb/endpoints/parsers/base_parser.rb, line 25
def mapped_results
  @mapped_results ||= results_from_http_request.map { |endpoint| Addressable::URI.join(response.uri, endpoint).to_s }.uniq.sort
rescue Addressable::URI::InvalidURIError => exception
  raise InvalidURIError, exception
end
parsed_response_body() click to toggle source
# File lib/indieweb/endpoints/parsers/base_parser.rb, line 31
def parsed_response_body
  @parsed_response_body ||= Nokogiri::HTML(response.body.to_s)
end
parsed_response_headers() click to toggle source
# File lib/indieweb/endpoints/parsers/base_parser.rb, line 35
def parsed_response_headers
  @parsed_response_headers ||= LinkHeaderParser.parse(response.headers.get('link'), base: response.uri)
end
results_from_body() click to toggle source
# File lib/indieweb/endpoints/parsers/base_parser.rb, line 39
def results_from_body
  return if response.mime_type != 'text/html'

  Services::ResponseParserService.parse_body(parsed_response_body, self.class.identifier)
end
results_from_headers() click to toggle source
# File lib/indieweb/endpoints/parsers/base_parser.rb, line 45
def results_from_headers
  return if parsed_response_headers.none?

  Services::ResponseParserService.parse_headers(parsed_response_headers.group_by_relation_type, self.class.identifier)
end
results_from_http_request() click to toggle source
# File lib/indieweb/endpoints/parsers/base_parser.rb, line 51
def results_from_http_request
  @results_from_http_request ||= [results_from_headers, results_from_body].flatten.compact
end