module SDL::Util::NokogiriUtils

Public Instance Methods

fetch_from_io(io, base_url, *search) click to toggle source
# File lib/sdl/util/nokogiri.rb, line 19
def fetch_from_io(io, base_url, *search)
  process_result Nokogiri::HTML(io).search(*search), base_url
end
fetch_from_url(url, *search) click to toggle source

Fetches an Nokogiri::XML::NodeSet from the webpage at url by performing *search

# File lib/sdl/util/nokogiri.rb, line 11
def fetch_from_url(url, *search)
  begin
    fetch_from_io open(url), url, *search
  rescue SocketError => e
    []
  end
end
process_result(result, base_url) click to toggle source

It converts all relative href URLs to absolute URLs and adds a ‘target’ attribute to all links, so that they open in a new browser window and not the current broker.

# File lib/sdl/util/nokogiri.rb, line 25
def process_result(result, base_url)
  result.search('//body//@href').each do |attribute|
    begin
      attribute.content = URI.join(base_url, attribute.value.gsub(/\s/, '')).to_s
    rescue URI::InvalidURIError
      next
    end

    if attribute.parent.name.eql? 'a'
      attribute.parent['target'] = '_new'
    end
  end

  result
end