Class: Fibrio::Parsers::NDJSON

Inherits:
Base
  • Object
show all
Defined in:
lib/fibrio/parsers/ndjson.rb

Constant Summary

Constants inherited from Base

Base::WHITESPACE

Instance Method Summary collapse

Methods inherited from Base

#close, #initialize

Constructor Details

This class inherits a constructor from Fibrio::Parsers::Base

Instance Method Details

#run {|record| ... } ⇒ Object

Yields:

  • (record)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fibrio/parsers/ndjson.rb', line 9

def run(&emit)
  line_number = 0

  while (line = @source.next_line)
    line_number += 1
    next if line.strip.empty?

    begin
      emit.call(::JSON.parse(line))
    rescue ::JSON::ParserError => e
      raise ParseError.new("invalid JSON on line #{line_number}: #{e.message}", position: @source.position)
    end
  end
end