class Zizia::Importer
The chief entry point for bulk import of records. `Importer` accepts a {Parser} on initialization and iterates through its {Parser#records}, importing each using a given {RecordImporter}.
@example Importing in bulk from a CSV file
parser = Zizia::Parser.for(file: File.new('path/to/import.csv')) Zizia::Importer.new(parser: parser).import if parser.validate
Attributes
parser[RW]
record_importer[RW]
Public Class Methods
new(parser:, record_importer: RecordImporter.new)
click to toggle source
@param parser [Parser] The parser to use as the source for import
records.
@param record_importer
[RecordImporter] An object to handle import of
each record
# File lib/zizia/importer.rb, line 34 def initialize(parser:, record_importer: RecordImporter.new) self.parser = parser self.record_importer = record_importer end
Public Instance Methods
import()
click to toggle source
Import each record in {#records}.
@return [void]
# File lib/zizia/importer.rb, line 48 def import no_records_message && return unless records.count.positive? start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) Rails.logger.info "[zizia] event: start_import, batch_id: #{record_importer.batch_id}, expecting to import #{records.count} records." records.each { |record| record_importer.import(record: record) } end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) elapsed_time = end_time - start_time Rails.logger.info "[zizia] event: finish_import, batch_id: #{record_importer.batch_id}, successful_record_count: #{record_importer.success_count}, failed_record_count: #{record_importer.failure_count}, elapsed_time: #{elapsed_time}, elapsed_time_per_record: #{elapsed_time / records.count}" end
no_records_message()
click to toggle source
Do not attempt to run an import if there are no records. Instead, just write to the log.
# File lib/zizia/importer.rb, line 40 def no_records_message Rails.logger.error "[zizia] event: empty_import, batch_id: #{record_importer.batch_id}" end