-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson_extractor.rb
56 lines (47 loc) · 1.17 KB
/
json_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
# frozen_string_literal: true
module Chronicle
module ETL
class JSONExtractor < Chronicle::ETL::Extractor
include Extractors::Helpers::InputReader
register_connector do |r|
r.identifier = :json
r.description = 'JSON'
end
setting :jsonl, default: true, type: :boolean
setting :path, default: nil, type: :string
def prepare
@jsons = []
load_input do |input|
data = parse_data(input)
@jsons += [data].flatten
end
end
def extract
@jsons.each do |json|
yield Chronicle::ETL::Extraction.new(data: json)
end
end
def results_count
@jsons.count
end
private
def parse_data(data)
parsed_data = JSON.parse(data)
if @config.path
parsed_data.dig(*@config.path.split('.'))
else
parsed_data
end
rescue JSON::ParserError
raise Chronicle::ETL::ExtractionError, 'Could not parse JSON'
end
def load_input(&block)
if @config.jsonl
read_input_as_lines(&block)
else
read_input(&block)
end
end
end
end
end