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



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

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

#get_schemaObject



71
72
73
74
# File 'lib/turbot_runner/processor.rb', line 71

def get_schema
  hyphenated_name = @data_type.to_s.gsub("_", "-").gsub(" ", "-")
  File.expand_path("../../../schema/schemas/#{hyphenated_name}-schema.json", __FILE__)
end

#interruptObject



39
40
41
# File 'lib/turbot_runner/processor.rb', line 39

def interrupt
  @runner.interrupt
end

#process(line) ⇒ Object



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

def process(line)
  begin
    if line.strip == "RUN ENDED"
      @record_handler.handle_run_ended
      @runner.interrupt if @runner
    else
      record = JSON.parse(line)
      errors = validate(record)

      if errors.empty?
        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, errors)
        @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

#schemaObject



67
68
69
# File 'lib/turbot_runner/processor.rb', line 67

def schema
  @schema ||= get_schema
end

#validate(record) ⇒ Object



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/turbot_runner/processor.rb', line 43

def validate(record)
  errors = JSON::Validator.fully_validate(schema, record, :errors_as_objects => true)
  messages = errors.map do |error|
    case error[:message]
    when /The property '#\/' did not contain a required property of '(\w+)'/
      "Missing required attribute: #{Regexp.last_match(1)}"
    else
      error[:message]
    end
  end

  if messages.empty?
    identifying_attributes = record.reject do |k, v|
      !@identifying_fields.include?(k) || v.nil? || v == ''
    end

    if identifying_attributes.empty?
      messages << "There were no values provided for any of the identifying fields: #{@identifying_fields.join(', ')}"
    end
  end

  messages
end