Sha256: 789ac5bb0f9981df8e549991eb8907134b796090ea413883022bbcd1d063e39f

Contents?: true

Size: 927 Bytes

Versions: 14

Compression:

Stored size: 927 Bytes

Contents

require 'marc'
require 'json'
require 'zlib'


# Read newline-delimited JSON file, where each line is a marc-in-json string.
# UTF-8 encoding is required.

class Traject::NDJReader
  include Enumerable

  def initialize(input_stream, settings)
    @settings = settings
    @input_stream = input_stream
    if input_stream.respond_to?(:path) && /\.gz\Z/.match(input_stream.path)
      @input_stream = Zlib::GzipReader.new(@input_stream, :external_encoding => "UTF-8")
    end
  end

  def logger
    @logger ||= (@settings[:logger] || Yell.new(STDERR, :level => "gt.fatal")) # null logger)
  end

  def each
    unless block_given?
      return enum_for(:each)
    end

    @input_stream.each_with_index do |json, i|
      begin
        yield MARC::Record.new_from_hash(JSON.parse(json))
      rescue Exception => e
        self.logger.error("Problem with JSON record on line #{i}: #{e.message}")
      end
    end
  end

end


Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
traject-3.8.2 lib/traject/ndj_reader.rb
traject-3.8.1 lib/traject/ndj_reader.rb
traject-3.8.0 lib/traject/ndj_reader.rb
traject-3.7.0 lib/traject/ndj_reader.rb
traject-3.6.0 lib/traject/ndj_reader.rb
traject-3.5.0 lib/traject/ndj_reader.rb
traject-3.4.0 lib/traject/ndj_reader.rb
traject-3.3.0 lib/traject/ndj_reader.rb
traject-3.2.0 lib/traject/ndj_reader.rb
traject-3.1.0 lib/traject/ndj_reader.rb
traject-3.1.0.rc1 lib/traject/ndj_reader.rb
traject-3.0.0 lib/traject/ndj_reader.rb
traject-3.0.0.alpha.2 lib/traject/ndj_reader.rb
traject-3.0.0.alpha.1 lib/traject/ndj_reader.rb