-
Notifications
You must be signed in to change notification settings - Fork 3
/
csv_extractor.rb
58 lines (48 loc) · 1.21 KB
/
csv_extractor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
56
57
58
# frozen_string_literal: true
require 'csv'
module Chronicle
module ETL
class CSVExtractor < Chronicle::ETL::Extractor
include Extractors::Helpers::InputReader
register_connector do |r|
r.identifier = :csv
r.description = 'CSV'
end
setting :headers, default: true
def prepare
@csvs = prepare_sources
end
def extract
@csvs.each do |csv|
csv.read.each do |row|
yield Chronicle::ETL::Extraction.new(data: row.to_h)
end
end
end
def results_count
@csvs.reduce(0) do |total_rows, csv|
row_count = csv.readlines.size
csv.rewind
total_rows + row_count
end
end
private
def all_rows
@csvs.reduce([]) do |all_rows, csv|
all_rows + csv.to_a.map(&:to_h)
end
end
def prepare_sources
@csvs = []
read_input do |csv_data|
csv_options = {
headers: @config.headers.is_a?(String) ? @config.headers.split(',') : @config.headers,
converters: :all
}
@csvs << CSV.new(csv_data, **csv_options)
end
@csvs
end
end
end
end