Class: Cure::Export::Exporter

Inherits:
Object
  • Object
show all
Includes:
FileHelpers, Log
Defined in:
lib/cure/export/exporter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#log_debug, #log_error, #log_info, #log_warn

Methods included from FileHelpers

#clean_dir, #read_file, #with_file, #with_temp_dir

Class Method Details

.export_ctx(ctx, output_dir, file_name) ⇒ Object



12
13
14
15
16
17
# File 'lib/cure/export/exporter.rb', line 12

def self.export_ctx(ctx, output_dir, file_name)
  column_headers = ctx.column_headers.keys

  exporter = Exporter.new
  exporter.export(output_dir, file_name, ctx.transformed_rows, column_headers)
end

Instance Method Details

#export(output_dir, file_name, rows, columns) ⇒ Object

Parameters:

  • rows (Array)
  • columns (Array)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cure/export/exporter.rb', line 21

def export(output_dir, file_name, rows, columns)
  log_info("Exporting file to [#{output_dir}/#{file_name}] with #{rows.length} rows")

  file_contents = []
  file_contents << columns.join(",")

  rows.each do |row|
    file_contents << row.join(",")
  end

  write_to_file(
    output_dir, file_name, "csv", file_contents.join("\n")
  )
end

#write_to_file(file_path, file_name, file_extension, contents) ⇒ Object

Parameters:

  • file_path (String)
  • contents (String)
  • file_extension (String)


39
40
41
42
43
44
45
46
# File 'lib/cure/export/exporter.rb', line 39

def write_to_file(file_path, file_name, file_extension, contents)
  file_location = "#{file_path}/#{file_name || Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S%-z")}"
  clean_dir(file_path)

  with_file(file_location, file_extension) do |file|
    file.write(contents)
  end
end