Created
October 6, 2012 15:32
-
-
Save steelThread/3845233 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'net/https' | |
require 'open-uri' | |
module AppCache | |
extend self | |
def run(host = "http://***.***.***") | |
@host = host | |
puts "Loading manifest..." | |
puts "Total Size => #{total.to_human}" | |
end | |
def total | |
assets.inject(0) {|sum, asset| sum + size(asset)} | |
end | |
def assets | |
filter manifest.scan(/^CACHE:([^[NET]]*)/)[0][0].split(/\r?\n/) | |
end | |
def filter(lines) | |
lines.reject! {|line| line.empty? || /^#/ =~ line} | |
end | |
def manifest | |
open("#{@host}/manifest/cache.manifest").read | |
end | |
def size(asset) | |
uri = URI /^http/ !~ asset ? "#{@host}#{asset}" : asset | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
request = Net::HTTP::Get.new uri.request_uri | |
http.request request do |response| | |
return response['content-length'].to_i | |
end | |
end | |
end | |
end | |
class Numeric | |
def to_human | |
units = %w{b Kb Mb Gb Tb} | |
e = (Math.log(self)/Math.log(1024)).floor | |
s = "%.1f" % (to_f / 1024**e) | |
s.sub(/\.?0*$/, units[e]) | |
end | |
end | |
AppCache.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment