Class: DlmeJsonResourceWriter

Inherits:
Traject::LineWriter
  • Object
show all
Includes:
LineWriterFixMixin
Defined in:
lib/dlme_json_resource_writer.rb

Overview

Write the traject output to newline delmitited json This writer also casts fields that should be singular (like `id`) from lists. Finally, it runs the DLME IR schema validator on each output to ensure the config is writing a compliant message.

Instance Method Summary collapse

Methods included from LineWriterFixMixin

#close

Instance Method Details

#serialize(context) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dlme_json_resource_writer.rb', line 16

def serialize(context)
  attributes = context.output_hash.dup
  adjusted = AdjustCardinality.call(attributes)
  errors = validate(adjusted)
  return JSON.generate(adjusted).unicode_normalize if errors.empty?

  raise "Transform produced invalid data.\n\n" \
    "The errors are: #{errors}\n\n" \
    "The data looked like this:\n" \
    "#{JSON.pretty_generate(adjusted)}"
end