class StaticdUtils::Archive

Manage Staticd archives.

This class can manage the archives used as transport package to transfer files beetween Staticd client and Staticd API.

Attributes

stream[R]

Public Class Methods

create(directory_path, manifest=nil) click to toggle source

Create an archive from a folder.

Can include a manifest as an array of files full path (from directory path as root).

Example:

StaticdUtils::Archive.create("/tmp/my_site", ["/index.html"])
# Only the /tmp/my_site/index.html file will be included into
  the archive.
# File lib/staticd_utils/archive.rb, line 29
def self.create(directory_path, manifest=nil)
  files =
    if manifest
      manifest.map { |entry| directory_path + entry }
    else
      Dir["#{directory_path}/**/*"].select { |f| File.file?(f) }
    end
  new(Tar.tar(files))
end
new(stream) click to toggle source
# File lib/staticd_utils/archive.rb, line 39
def initialize(stream)
  @stream = stream
end
open_file(url) click to toggle source
# File lib/staticd_utils/archive.rb, line 16
def self.open_file(url)
  new(open(url))
end

Public Instance Methods

close() click to toggle source
# File lib/staticd_utils/archive.rb, line 52
def close
  @stream.close unless @stream.closed?
end
extract(path) click to toggle source
# File lib/staticd_utils/archive.rb, line 56
def extract(path)
  return false if @stream.closed?

  Tar.untar(@stream, path)
  close
  path
end
open() { |tmp| ... } click to toggle source
# File lib/staticd_utils/archive.rb, line 43
def open
  Dir.mktmpdir do |tmp|
    Dir.chdir(tmp) do
      extract(tmp)
      yield tmp
    end
  end
end
size() click to toggle source
# File lib/staticd_utils/archive.rb, line 64
def size
  @stream.size
end
to_file(path) click to toggle source
# File lib/staticd_utils/archive.rb, line 68
def to_file(path)
  return false if @stream.closed?

  File.open(path, 'w') { |file| file.write(@stream.read) }
  self.close
  path
end
to_memory_file() click to toggle source
# File lib/staticd_utils/archive.rb, line 76
def to_memory_file
  StaticdUtils::MemoryFile.new(@stream)
end