Class: MARC::JSONLReader
- Inherits:
-
Object
- Object
- MARC::JSONLReader
- 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
-
#each ⇒ Object
Turn marc-in-json lines into actual marc records and yield them.
-
#initialize(file) ⇒ JSONLReader
constructor
A new instance of JSONLReader.
Constructor Details
#initialize(file) ⇒ JSONLReader
Returns a new instance of JSONLReader.
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
#each ⇒ Object
Turn marc-in-json lines into actual marc records and yield them
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 |