Class: Worklog::Takeout

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

Overview

Exporter for worklog entries to a compressed tarball.

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Takeout

Constructor

Parameters:

  • configuration (Configuration)

    The application configuration



12
13
14
# File 'lib/takeout.rb', line 12

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#all_filesArray<String>

Retrieves all files from the storage path.

Returns:

  • (Array<String>)

    List of file paths



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_gzString

Creates a tar.gz archive of all worklog files, including settings.

Returns:

  • (String)

    The tar.gz data as a binary string



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
# File 'lib/takeout.rb', line 24

def to_tar_gz
  tar_io = StringIO.new

  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