Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment