Class: Chronicle::ETL::JSONLoader

Inherits:
Loader
  • Object
show all
Includes:
Loaders::Helpers::StdoutHelper
Defined in:
lib/chronicle/etl/loaders/json_loader.rb

Instance Attribute Summary

Attributes included from Registry::SelfRegistering

#connector_registration

Instance Method Summary collapse

Methods included from Loaders::Helpers::StdoutHelper

#create_stdout_temp_file, #output_to_stdout?, #write_to_stdout, #write_to_stdout_from_temp_file

Methods included from Registry::SelfRegistering

#register_connector

Methods included from Loaders::Helpers::EncodingHelper

#force_utf8

Methods included from Configurable

included

Constructor Details

#initialize(*args) ⇒ JSONLoader

Returns a new instance of JSONLoader.



21
22
23
24
# File 'lib/chronicle/etl/loaders/json_loader.rb', line 21

def initialize(*args)
  super
  @first_line = true
end

Instance Method Details

#finishObject



67
68
69
70
71
72
73
74
# File 'lib/chronicle/etl/loaders/json_loader.rb', line 67

def finish
  # Close the array unless we're doing line-separated JSON
  @output_file.puts("\n]") unless @config.line_separated

  write_to_stdout_from_temp_file(@output_file) if output_to_stdout?

  @output_file.close
end

#load(record) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chronicle/etl/loaders/json_loader.rb', line 37

def load(record)
  serialized = record.to_h

  # When dealing with raw data, we can get improperly encoded strings
  # (eg from sqlite database columns). We force conversion to UTF-8
  # before converting into JSON
  # encoded = serialized.transform_values do |value|
  #   next value unless value.is_a?(String)

  #   force_utf8(value)
  # end
  encoded = deeply_force_utf8(serialized)

  line = encoded.to_json
  # For line-separated output, we just put json + newline
  if @config.line_separated
    line = "#{line}\n"
  # Otherwise, we add a comma and newline and then add record to the
  # array we created in #start (unless it's the first line).
  else
    line = ",\n#{line}" unless @first_line
  end

  @output_file.write(line)

  @first_line = false
  # rescue StandardError => e
  #   binding.pry
end

#startObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/chronicle/etl/loaders/json_loader.rb', line 26

def start
  @output_file =
    if output_to_stdout?
      create_stdout_temp_file
    else
      File.open(@config.output, 'w+')
    end

  @output_file.puts("[\n") unless @config.line_separated
end