module Wayfarer::Finders

Public Instance Methods

images(*filters) click to toggle source

Returns the expanded `src` attribute URIs from all or targeted `<img>` tags. TODO: Tests @param [*Array<String>] filters CSS/XPath expressions. @return [Array<URI>]

# File lib/wayfarer/finders.rb, line 33
def images(*filters)
  query("img", "src", *filters)
end
javascripts(*filters) click to toggle source

Returns the expanded `src` attribute URIs from all or targeted `<script>` tags. TODO: Tests @param [*Array<String>] filters CSS/XPath expressions. @return [Array<URI>]

# File lib/wayfarer/finders.rb, line 23
def javascripts(*filters)
  query("script", "src", *filters)
end
Also aliased as: scripts
scripts(*filters)
Alias for: javascripts
stylesheets(*filters) click to toggle source

Returns the expanded `href` attribute URIs from all or targeted `<link rel=“stylesheet” …>` tags. @param [*Array<String>] filters CSS/XPath expressions. @return [Array<URI>]

# File lib/wayfarer/finders.rb, line 15
def stylesheets(*filters)
  query("link[rel='stylesheet']", "href", *filters)
end

Private Instance Methods

query(selector, attr, *filters) click to toggle source

TODO: Lord have mercy

# File lib/wayfarer/finders.rb, line 40
def query(selector, attr, *filters)
  nodes = if filters.any?
            doc.search(*filters).css(selector)
          else
            doc.css(selector)
          end

  links = nodes.map { |node|
    begin
      URI.join(uri, node.attr(attr))
    rescue
      nil
    end
  }

  links
    .find_all { |uri| uri.is_a?(URI) }
    .uniq
    .map(&:to_s)
end