Class: Worklog::Takeout
- Inherits:
-
Object
- Object
- Worklog::Takeout
- Defined in:
- lib/takeout.rb
Overview
Exporter for worklog entries to a compressed tarball.
Instance Method Summary collapse
-
#all_files ⇒ Array<String>
Retrieves all files from the storage path.
-
#initialize(configuration) ⇒ Takeout
constructor
Constructor.
-
#to_tar_gz ⇒ String
Creates a tar.gz archive of all worklog files, including settings.
Constructor Details
#initialize(configuration) ⇒ Takeout
Constructor
12 13 14 |
# File 'lib/takeout.rb', line 12 def initialize(configuration) @configuration = configuration end |
Instance Method Details
#all_files ⇒ Array<String>
Retrieves all files from the storage path.
18 19 20 |
# File 'lib/takeout.rb', line 18 def all_files Dir.glob(File.join(@configuration.storage_path, '*')).select { |file| File.file?(file) } end |
#to_tar_gz ⇒ String
Creates a tar.gz archive of all worklog files, including settings.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/takeout.rb', line 24 def to_tar_gz tar_io = StringIO.new # Build a mapping of file names to their stats for timestamp preservation file_stats = {} all_files.each do |file_path| file_name = File.basename(file_path) file_stats[file_name] = File.stat(file_path) end Gem::Package::TarWriter.new(tar_io) do |tar| all_files.each do |file_path| file_name = File.basename(file_path) File.open(file_path, 'rb') do |file| stat = file.stat tar.add_file(file_name, stat.mode, stat.mtime) do |tar_file| IO.copy_stream(file, tar_file) end end end end # Compress the tar data with gzip gz_io = StringIO.new Zlib::GzipWriter.wrap(gz_io) do |gzip| tar_io.rewind IO.copy_stream(tar_io, gzip) end gz_io.rewind gz_io.string end |