# frozen_string_literal: true

require 'link_thumbnailer/scrapers/default/base'
require 'link_thumbnailer/models/favicon'

module LinkThumbnailer
  module Scrapers
    module Default
      class Favicon < ::LinkThumbnailer::Scrapers::Default::Base

        def value
          modelize(to_uri(href)).to_s
        end

        private

        def to_uri(href)
          uri = ::URI.parse(href)
          uri.scheme ||= website.url.scheme
          uri.host ||= website.url.host
          uri
        rescue ::URI::InvalidURIError
          nil
        end

        def href
          node.attributes['href'].value.to_s if node
        end

        def node
          document.xpath("//link[contains(@rel, 'icon')]").first
        end

        def modelize(uri)
          model_class.new(uri)
        end

      end
    end
  end
end

# frozen_string_literal: true

require 'link_thumbnailer/scrapers/default/base'
require 'link_thumbnailer/models/favicon'

module LinkThumbnailer
  module Scrapers
    module Default
      class Favicon < ::LinkThumbnailer::Scrapers::Default::Base

        def value
          modelize(to_uri(href)).to_s
        end

        private

        def to_uri(href)
          uri = ::URI.parse(href)
          uri.scheme ||= website.url.scheme
          uri.host ||= website.url.host
          uri.path = uri.path&.sub(%r{^(?=[^\/])}, '/')
          uri
        rescue ::URI::InvalidURIError
          nil
        end

        def href
          node.attributes['href'].value.to_s if node
        end

        def node
          document.xpath("//link[contains(@rel, 'icon')]").first
        end

        def modelize(uri)
          model_class.new(uri)
        end

      end
    end
  end
end

Fix problem with uri path
Fix problem with uri path
