class RemoteZip::Reader

Attributes

url[RW]

Public Class Methods

new(url) click to toggle source
# File lib/remote_zip/reader.rb, line 11
def initialize(url)
  self.url = URI(url)
end

Public Instance Methods

each_file_with_path(&block) click to toggle source
# File lib/remote_zip/reader.rb, line 15
def each_file_with_path(&block)
  with_tempfile do |temp|
    temp.write(open(url).read)
    temp.flush

    extract(temp.path) do |absolute_path, zip_path|
      File.open(absolute_path) do |file|
        block.call(file, zip_path)
      end
    end
  end
rescue OpenURI::HTTPError => e
  raise DownloadError, e
end

Private Instance Methods

extract(path, &block) click to toggle source
# File lib/remote_zip/reader.rb, line 36
def extract(path, &block)
  contents_dir = path + "_contents"
  FileUtils.mkdir_p(contents_dir)

  Zip::File.open(path) do |zip|
    zip.each do |entry|
      absolute_path = File.join(contents_dir, entry.name)
      FileUtils.mkdir_p(File.dirname(absolute_path))
      entry.extract(absolute_path)
      block.call(absolute_path, entry.name)
    end
  end
ensure
  FileUtils.rm_r(contents_dir)
end
with_tempfile(&block) click to toggle source
# File lib/remote_zip/reader.rb, line 52
def with_tempfile(&block)
  temp = Tempfile.new(self.class.name)
  temp.binmode
  block.call(temp)
ensure
  temp.close # GC will do the rest
end