Class: TurbotRunner::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/turbot_runner/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(runner, script_config, record_handler) ⇒ Processor

Returns a new instance of Processor.



5
6
7
8
9
10
# File 'lib/turbot_runner/processor.rb', line 5

def initialize(runner, script_config, record_handler)
  @runner = runner
  @data_type = script_config[:data_type]
  @identifying_fields = script_config[:identifying_fields]
  @record_handler = record_handler
end

Instance Method Details

#interruptObject



45
46
47
# File 'lib/turbot_runner/processor.rb', line 45

def interrupt
  @runner.interrupt
end

#process(line) ⇒ Object



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
# File 'lib/turbot_runner/processor.rb', line 12

def process(line)
  begin
    if line.strip == "SNAPSHOT ENDED" || line.strip == "RUN ENDED" # latter is legacy
      @record_handler.handle_snapshot_ended(@data_type)
      @runner.interrupt if @runner
    else
      record = Openc::JsonSchema.convert_dates(schema_path, JSON.parse(line))

      record_to_validate = record.select {|k, v| k != 'retrieved_at'}

      error_message = Validator.validate(
        @data_type,
        record_to_validate,
        @identifying_fields
      )

      if error_message.nil?
        begin
          @record_handler.handle_valid_record(record, @data_type)
        rescue InterruptRun
          @runner.interrupt if @runner
        end
      else
        @record_handler.handle_invalid_record(record, @data_type, error_message)
        @runner.interrupt_and_mark_as_failed if @runner
      end
    end
  rescue JSON::ParserError
    @record_handler.handle_invalid_json(line)
    @runner.interrupt_and_mark_as_failed if @runner
  end
end

#schema_pathObject



49
50
51
# File 'lib/turbot_runner/processor.rb', line 49

def schema_path
  TurbotRunner.schema_path(@data_type)
end