Class: HTMLPackage::Loader
- Inherits:
-
Object
- Object
- HTMLPackage::Loader
- Defined in:
- lib/html_package/loader.rb
Instance Method Summary collapse
-
#initialize(package, out_dir) ⇒ Loader
constructor
A new instance of Loader.
- #load(options = {}) ⇒ Object
- #load_file(uri, out_dir) ⇒ Object
Constructor Details
#initialize(package, out_dir) ⇒ Loader
Returns a new instance of Loader.
7 8 9 10 11 |
# File 'lib/html_package/loader.rb', line 7 def initialize(package, out_dir) @package = package @out_dir = Pathname.new(out_dir). FileUtils.mkdir_p(@out_dir) end |
Instance Method Details
#load(options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/html_package/loader.rb', line 37 def load(={}) threads = [] threads += @package.all_files.map do |uri| load_file(uri, @out_dir) end threads.map{|thread| thread and thread.join } end |
#load_file(uri, out_dir) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/html_package/loader.rb', line 13 def load_file(uri, out_dir) return if uri == nil puts "loading: #{uri.blue}" Thread.new do begin content = open(uri).read outfile = out_dir + Pathname.new(uri).basename.to_s.gsub(/-([\d].?){3}.*\.js$/, ".js") begin File.open(outfile, "w") { |file| file.write content } rescue Exception => e puts "failed: File.write #{outfile.to_s.red}" puts " #{e.class.name.bold} #{e..red.bold}" puts end rescue OpenURI::HTTPError => e puts "failed: GET #{uri.red}" puts " #{e.class.name.bold} #{e..red.bold}" puts end end end |