Sha256: b72bc6d2f1c5af290338830d1ba01d50a109eacd9600d940ba2ff5feab8b7848

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module IndieWeb
  module Endpoints
    # @api private
    class Parser
      # @param response [HTTP::Response]
      def initialize(response)
        @response = response
      end

      # @return [Hash{Symbol => String, Array<String>, nil}]
      def results
        {
          authorization_endpoint: result_for(:authorization_endpoint),
          micropub: result_for(:micropub),
          microsub: result_for(:microsub),
          redirect_uri: results_for(:redirect_uri),
          token_endpoint: result_for(:token_endpoint),
          webmention: result_for(:webmention, %w[link a])
        }
      end

      private

      # @return [HTTP::Response]
      attr_reader :response

      # @return [IndieWeb::Endpoints::ResponseBodyParser]
      def response_body_parser
        @response_body_parser ||= ResponseBodyParser.new(response)
      end

      # @return [IndieWeb::Endpoints::ResponseHeadersParser]
      def response_headers_parser
        @response_headers_parser ||= ResponseHeadersParser.new(response)
      end

      # @param identifier [Symbol]
      # @param nodes [Array<String>]
      # @return [String, nil]
      def result_for(identifier, nodes = ['link'])
        results_for(identifier, nodes)&.first
      end

      # @param identifier [Symbol]
      # @param nodes [Array<String>]
      # @return [Array<String>, nil]
      # @raise [IndieWeb::Endpoints::InvalidURIError]
      def results_for(identifier, nodes = ['link'])
        results_from_request = [
          response_headers_parser.results_for(identifier),
          response_body_parser.results_for(identifier, nodes)
        ].flatten.compact

        return if results_from_request.none?

        results_from_request.map { |endpoint| response.uri.join(endpoint).to_s }.uniq.sort
      rescue Addressable::URI::InvalidURIError => e
        raise InvalidURIError, e
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
indieweb-endpoints-7.1.0 lib/indieweb/endpoints/parser.rb