class MARC::JSONLReader

Read marc-in-json documents from a ‘.jsonl` file – also called “newline-delimited JSON”, which is a file with one JSON document on each line.

Public Class Methods

new(file) click to toggle source

@param [String, IO] file A filename, or open File/IO type object, from which to read

# File lib/marc/jsonl_reader.rb, line 12
def initialize(file)
  if file.is_a?(String)
    raise ArgumentError.new("File '#{file}' can't be found") unless File.exist?(file)
    raise ArgumentError.new("File '#{file}' can't be opened for reading") unless File.readable?(file)
    @handle = File.new(file)
  elsif file.respond_to?(:read, 5)
    @handle = file
  else
    raise ArgumentError, "must pass in path or file"
  end
end

Public Instance Methods

each() { |new_from_hash(parse)| ... } click to toggle source

Turn marc-in-json lines into actual marc records and yield them @yieldreturn [MARC::Record] record created from each line of the file

# File lib/marc/jsonl_reader.rb, line 26
def each
  return enum_for(:each) unless block_given?
  @handle.each do |line|
    yield MARC::Record.new_from_hash(JSON.parse(line))
  end
end