Class: TurbotRunner::Processor
- Inherits:
-
Object
- Object
- TurbotRunner::Processor
- Defined in:
- lib/turbot_runner/processor.rb
Instance Method Summary collapse
- #get_schema ⇒ Object
-
#initialize(runner, script_config, record_handler) ⇒ Processor
constructor
A new instance of Processor.
- #interrupt ⇒ Object
- #process(line) ⇒ Object
- #schema ⇒ Object
- #validate(record) ⇒ Object
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] = script_config[:identifying_fields] @record_handler = record_handler end |
Instance Method Details
#get_schema ⇒ Object
71 72 73 74 |
# File 'lib/turbot_runner/processor.rb', line 71 def get_schema hyphenated_name = @data_type.to_s.gsub("_", "-").gsub(" ", "-") File.("../../../schema/schemas/#{hyphenated_name}-schema.json", __FILE__) end |
#interrupt ⇒ Object
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 |
#schema ⇒ Object
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) = 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 .empty? = record.reject do |k, v| !.include?(k) || v.nil? || v == '' end if .empty? << "There were no values provided for any of the identifying fields: #{@identifying_fields.join(', ')}" end end end |