Created
September 15, 2011 00:53
-
-
Save danbernier/1218251 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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