Class: MARC::JSONLReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/marc/jsonl_reader.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ JSONLReader

Returns a new instance of JSONLReader.

Parameters:

  • file (String, IO)

    A filename, or open File/IO type object, from which to read



12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#eachObject

Turn marc-in-json lines into actual marc records and yield them

Yield Returns:

  • (MARC::Record)

    record created from each line of the file



26
27
28
29
30
31
# 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