Class: StaticdUtils::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/staticd_utils/archive.rb

Overview

Manage Staticd archives.

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Archive

Returns a new instance of Archive.



39
40
41
# File 'lib/staticd_utils/archive.rb', line 39

def initialize(stream)
  @stream = stream
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



14
15
16
# File 'lib/staticd_utils/archive.rb', line 14

def stream
  @stream
end

Class Method Details

.create(directory_path, manifest = nil) ⇒ Object

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.


29
30
31
32
33
34
35
36
37
# 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

.open_file(url) ⇒ Object



16
17
18
# File 'lib/staticd_utils/archive.rb', line 16

def self.open_file(url)
  new(open(url))
end

Instance Method Details

#closeObject



52
53
54
# File 'lib/staticd_utils/archive.rb', line 52

def close
  @stream.close unless @stream.closed?
end

#extract(path) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/staticd_utils/archive.rb', line 56

def extract(path)
  return false if @stream.closed?

  Tar.untar(@stream, path)
  close
  path
end

#openObject



43
44
45
46
47
48
49
50
# 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

#sizeObject



64
65
66
# File 'lib/staticd_utils/archive.rb', line 64

def size
  @stream.size
end

#to_file(path) ⇒ Object



68
69
70
71
72
73
74
# 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_fileObject



76
77
78
# File 'lib/staticd_utils/archive.rb', line 76

def to_memory_file
  StaticdUtils::MemoryFile.new(@stream)
end