Sha256: 9f53d5df91e417eddfe0d00e56e91f72649dd6dcabae7b9776739d00fc5a13c9

Contents?: true

Size: 871 Bytes

Versions: 44

Compression:

Stored size: 871 Bytes

Contents

module Govspeak
  class LinkExtractor
    def initialize(document, website_root: nil)
      @document = document
      @website_root = website_root
    end

    def call
      @call ||= extract_links
    end

  private

    attr_reader :document, :website_root

    def extract_links
      document_anchors
        .map { |link| extract_href_from_link(link) }
        .reject(&:blank?)
    end

    def extract_href_from_link(link)
      href = link["href"] || ""
      if website_root && href.start_with?("/")
        "#{website_root}#{href}"
      else
        href
      end
    end

    def document_anchors
      processed_govspeak.css("a[href]").css('a:not([href^="mailto"])').css('a:not([href^="#"])')
    end

    def processed_govspeak
      doc = Nokogiri::HTML::Document.new
      doc.encoding = "UTF-8"

      doc.fragment(document.to_html)
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
govspeak-7.0.1 lib/govspeak/link_extractor.rb
govspeak-7.0.0 lib/govspeak/link_extractor.rb
govspeak-6.8.4 lib/govspeak/link_extractor.rb
govspeak-6.8.3 lib/govspeak/link_extractor.rb
govspeak-6.8.2 lib/govspeak/link_extractor.rb
govspeak-6.8.1 lib/govspeak/link_extractor.rb
govspeak-6.8.0 lib/govspeak/link_extractor.rb
govspeak-6.7.8 lib/govspeak/link_extractor.rb
govspeak-6.7.7 lib/govspeak/link_extractor.rb
govspeak-6.7.6 lib/govspeak/link_extractor.rb
govspeak-6.7.5 lib/govspeak/link_extractor.rb
govspeak-6.7.4 lib/govspeak/link_extractor.rb
govspeak-6.7.3 lib/govspeak/link_extractor.rb
govspeak-6.7.2 lib/govspeak/link_extractor.rb
govspeak-6.7.1 lib/govspeak/link_extractor.rb
govspeak-6.7.0 lib/govspeak/link_extractor.rb
govspeak-6.6.0 lib/govspeak/link_extractor.rb
govspeak-6.5.11 lib/govspeak/link_extractor.rb
govspeak-6.5.10 lib/govspeak/link_extractor.rb
govspeak-6.5.9 lib/govspeak/link_extractor.rb