Skip to content

Instantly share code, notes, and snippets.

@danbernier
Created September 15, 2011 00:53
Show Gist options
  • Save danbernier/1218252 to your computer and use it in GitHub Desktop.
Save danbernier/1218252 to your computer and use it in GitHub Desktop.
module RSlow
class HtmlResource < ParsableResource
attr_accessor :doc, :scripts, :stylesheets, :images
def initialize(url)
super
@doc = Nokogiri::HTML(@contents)
@scripts = fetch_script_resources(".//script", 'src', JsResource)
@stylesheets = fetch_stylesheet_resources(".//link[@rel]", 'href', CssResource)
@images = fetch_image_resources(".//img", 'src', Resource)
end
private
def fetch_resources(xpath, url_attribute, klass)
resources = []
@doc.xpath(xpath).each do |elem|
url = elem[url_attribute]
res = klass.new(url, self) unless url.nil? || url.empty?
resources << res
@children << res
end
return resources
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment