Sha256: 61f4f8bbebe30c843a76b26cf1fa429bfdfab8060b7f204b6546d5bd0ec34e29

Contents?: true

Size: 758 Bytes

Versions: 1

Compression:

Stored size: 758 Bytes

Contents

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

    def call
      @links ||= extract_links
    end

  private

    attr_reader :document, :website_root

    def extract_links
      document_anchors.map do |link|
        if website_root && link['href'].start_with?('/')
          "#{website_root}#{link['href']}"
        else
          link['href']
        end
      end
    end

    def document_anchors
      processed_govspeak.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

1 entries across 1 versions & 1 rubygems

Version Path
govspeak-5.4.0 lib/govspeak/link_extractor.rb