Class: Cure::Export::Exporter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#log_debug, #log_error, #log_info, #log_warn

Methods included from Configuration

#config, #create_config, #register_config

Methods included from Helpers::FileHelpers

#clean_dir, #read_file, #with_file, #with_temp_dir

Constructor Details

#initialize(output_dir) ⇒ Exporter

Returns a new instance of Exporter.



22
23
24
# File 'lib/cure/export/exporter.rb', line 22

def initialize(output_dir)
  @output_dir = output_dir
end

Instance Attribute Details

#output_dirObject (readonly)

Returns the value of attribute output_dir.



20
21
22
# File 'lib/cure/export/exporter.rb', line 20

def output_dir
  @output_dir
end

Class Method Details

.export_result(results, output_dir) ⇒ Object

Parameters:

  • result (Array<Cure::Transform::TransformResult>)


15
16
17
18
# File 'lib/cure/export/exporter.rb', line 15

def self.export_result(results, output_dir)
  exporter = Exporter.new(output_dir)
  exporter.export_results(results)
end

Instance Method Details

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

Parameters:

  • rows (Array)
  • columns (Array)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cure/export/exporter.rb', line 44

def export(output_dir, file_name, rows, columns)
  file_name = "#{file_name}-#{Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S%-z")}"

  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

#export_results(result) ⇒ Object

Parameters:

  • result (Array<Cure::Transform::TransformResult>)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cure/export/exporter.rb', line 27

def export_results(result)
  export_ranges = config.template.dispatch.named_ranges

  export_ranges.each do |range|
    named_range = range["named_range"]
    unless result.has_key?(named_range)
      raise "Missing named range - #{range} from candidates [#{result.keys.join(", ")}]"
    end

    data = result[named_range]
    column_headers = data.column_headers.keys
    export(@output_dir, range["file_name"], data.transformed_rows, column_headers)
  end
end

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

Parameters:

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


64
65
66
67
68
69
70
71
# File 'lib/cure/export/exporter.rb', line 64

def write_to_file(file_path, file_name, file_extension, contents)
  file_location = "#{file_path}/#{file_name}"
  clean_dir(file_path)

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