Module: BerkeleyLibrary::AV::Marc

Defined in:
lib/berkeley_library/av/marc.rb,
lib/berkeley_library/av/marc/util.rb

Defined Under Namespace

Modules: Util

Class Method Summary collapse

Class Method Details

.all_from_xml(xml) ⇒ MARC::XMLReader

Parses MARCXML.

Parameters:

  • xml (String)

    the XML to parse

Returns:

  • (MARC::XMLReader)

    the MARC records



20
21
22
23
# File 'lib/berkeley_library/av/marc.rb', line 20

def all_from_xml(xml)
  input = StringIO.new(xml.scrub)
  MARC::XMLReader.new(input)
end

.from_xml(xml) ⇒ MARC::Record?

Parses MARCXML.

Parameters:

  • xml (String)

    the XML to parse

Returns:

  • (MARC::Record, nil)

    the MARC record from the specified XML



11
12
13
14
# File 'lib/berkeley_library/av/marc.rb', line 11

def from_xml(xml)
  # noinspection RubyYardReturnMatch,RubyMismatchedReturnType
  all_from_xml(xml).first
end

.read(marc_path, external_encoding: 'MARC-8') ⇒ MARC::Record

Returns a MARC record read from the specified file (or the first record if the file contains multiple records).

Parameters:

  • marc_path (String)

    the path to a MARC file in XML or binary format

  • external_encoding (String) (defaults to: 'MARC-8')

    the encoding, for binary files

Returns:

  • (MARC::Record)

    the MARC record



45
46
47
48
# File 'lib/berkeley_library/av/marc.rb', line 45

def read(marc_path, external_encoding: 'MARC-8')
  # noinspection RubyYardReturnMatch
  reader_for(marc_path, external_encoding:).first
end

.reader_for(marc_path, external_encoding: 'MARC-8') ⇒ MARC::XMLReader, MARC::Reader

Returns a reader for the specified MARC file.

Parameters:

  • marc_path (String)

    the path to a MARC file in XML or binary format

  • external_encoding (String) (defaults to: 'MARC-8')

    the encoding, for binary files

Returns:

  • (MARC::XMLReader)

    if the file path ends in ‘.xml`

  • (MARC::Reader)

    if the file path ends in ‘.mrc`

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
# File 'lib/berkeley_library/av/marc.rb', line 31

def reader_for(marc_path, external_encoding: 'MARC-8')
  downcased_path = marc_path.downcase
  return MARC::XMLReader.new(marc_path) if downcased_path.end_with?('.xml')
  return MARC::Reader.new(marc_path, external_encoding:) if downcased_path.end_with?('.mrc')

  raise ArgumentError, "Unable to determine reader needed for MARC file #{marc_path.inspect}"
end