Sha256: 2f5302be8c2f4a40894c8cb779eb860483d6d902dfd0b6fa053706b63d283ae7

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module IndieWeb
  module Endpoints
    module Parsers
      class RedirectUriParser < BaseParser
        def self.identifier
          :redirect_uri
        end

        Parsers.register(self)

        def results
          return unless results_from_http_request.any?

          @results ||= results_from_http_request.map { |endpoint| Absolutely.to_abs(base: response.uri.to_s, relative: endpoint) }.uniq.sort
        rescue Absolutely::InvalidURIError => exception
          raise InvalidURIError, exception
        end

        private

        def results_from_body
          RedirectUriLinkElementParser.new(response, self.class.identifier).results
        end

        def results_from_headers
          RedirectUriLinkHeaderParser.new(response, self.class.identifier).results
        end

        def results_from_http_request
          @results_from_http_request ||= [results_from_headers, results_from_body].flatten.compact
        end

        class RedirectUriLinkElementParser < BaseLinkElementParser
          def results
            link_elements.map { |element| element['href'] } if response_is_html && link_elements.any?
          end
        end

        class RedirectUriLinkHeaderParser < BaseLinkHeaderParser
          def results
            return unless link_headers

            link_headers.map(&:target_uri)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
indieweb-endpoints-0.7.0 lib/indieweb/endpoints/parsers/redirect_uri_parser.rb
indieweb-endpoints-0.6.0 lib/indieweb/endpoints/parsers/redirect_uri_parser.rb