lib/deadweight.rb in aanand-deadweight-0.0.1 vs lib/deadweight.rb in aanand-deadweight-0.0.2

- old
+ new

@@ -13,10 +13,11 @@ @ignore_selectors = [] @mechanize = false @log_file = STDERR end + # Find all unused CSS selectors and return them as an array. def run css = CssParser::Parser.new @stylesheets.each do |path| css.add_block!(fetch(path)) @@ -64,16 +65,33 @@ log.info "found #{unused_selectors.size} unused selectors out of #{total_selectors} total" unused_selectors end - private - + # Returns the Mechanize instance, if +mechanize+ is set to +true+. def agent @agent ||= initialize_agent end + # Fetch a path, using Mechanize if +mechanize+ is set to +true+. + def fetch(path) + log.info(path) + + loc = root + path + + if @mechanize + loc = "file://#{File.expand_path(loc)}" unless loc =~ %r{^\w+://} + page = agent.get(loc) + log.warn("#{path} redirected to #{page.uri}") unless page.uri.to_s == loc + page.body + else + open(loc).read + end + end + + private + def log @log ||= Logger.new(@log_file) end def initialize_agent @@ -87,24 +105,9 @@ Install it like so: gem install mechanize ================================================================= } raise - end - end - - def fetch(path) - log.info(path) - - loc = root + path - - if @mechanize - loc = "file://#{File.expand_path(loc)}" unless loc =~ %r{^\w+://} - page = agent.get(loc) - log.warn("#{path} redirected to #{page.uri}") unless page.uri.to_s == loc - page.body - else - open(loc).read end end end