Class: Runoff::FileWriter

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

Overview

Public: Writes data received from the database to text files.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FileWriter

Public: Initializes a FileWriter object.

options - A Hash with commandline options.



10
11
12
13
14
15
# File 'lib/runoff/file_writer.rb', line 10

def initialize(options)
  @export_path       = Location.get_export_path options
  @adapter           = Object.const_get("Runoff::Adapters::#{options[:adapter]}").new
  @current_file_name = nil
  @buffer            = []
end

Instance Method Details

#archiveObject

Public: Saves all the exported files in a Zip archive.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/runoff/file_writer.rb', line 38

def archive
  archive_name = "#{@export_path}_#{Time.now.to_i}.zip"

  Zip::File.open archive_name, Zip::File::CREATE do |archive|
    Dir[File.join(@export_path, '**', '**')].each do |file|
      archive.add File.basename(file), file
    end
  end

  FileUtils.rm_rf @export_path # Delete the folder.
end

#write(messages) ⇒ Object

Public: Writes a single row of data to a text file.

messages - An Array of data received from the database.

Examples

write [{ chatname: "#first_user/$second_user;d3d86c6b0e3b8320" ... }, ...]


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/runoff/file_writer.rb', line 24

def write(messages)
  messages.each_with_index do |m, i|
    file_name = @adapter.get_file_name m[Runoff::COLUMNS[1]]

    dump unless @current_file_name.nil? || @current_file_name == file_name

    @current_file_name = file_name

    @buffer << @adapter.build_entry(m)
    dump if i == (messages.count - 1)
  end
end