Class: FolioClient::SourceStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/folio_client/source_storage.rb

Overview

Lookup records in Folio Source Storage

Constant Summary collapse

FIELDS_TO_REMOVE =
%w[001 003].freeze

Instance Method Summary collapse

Instance Method Details

#fetch_marc_hash(instance_hrid:) ⇒ Hash

get marc bib data from folio given an instance HRID

Parameters:

  • instance_hrid (String)

    the key to use for MARC lookup

Returns:

  • (Hash)

    hash representation of the MARC. should be usable by MARC::Record.new_from_hash (from ruby-marc gem)

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/folio_client/source_storage.rb', line 13

def fetch_marc_hash(instance_hrid:)
  response_hash = client.get('/source-storage/source-records', { instanceHrid: instance_hrid })

  record_count = response_hash['totalRecords']
  raise ResourceNotFound, "No records found for #{instance_hrid}" if record_count.zero?

  if record_count > 1
    raise MultipleResourcesFound,
          "Expected 1 record for #{instance_hrid}, but found #{record_count}"
  end

  response_hash['sourceRecords'].first['parsedRecord']['content']
end

#fetch_marc_xml(instance_hrid: nil, barcode: nil) ⇒ String

get marc bib data as MARCXML from folio given an instance HRID rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

Parameters:

  • instance_hrid (String) (defaults to: nil)

    the instance HRID to use for MARC lookup

  • barcode (String) (defaults to: nil)

    the barcode to use for MARC lookup

Returns:

  • (String)

    MARCXML string

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/folio_client/source_storage.rb', line 35

def fetch_marc_xml(instance_hrid: nil, barcode: nil)
  if barcode.nil? && instance_hrid.nil?
    raise ArgumentError,
          'Either a barcode or a Folio instance HRID must be provided'
  end

  instance_hrid ||= client.fetch_hrid(barcode: barcode)

  if instance_hrid.blank?
    raise ResourceNotFound,
          "Catalog record not found. HRID: #{instance_hrid} | Barcode: #{barcode}"
  end

  marc_record = MARC::Record.new_from_hash(
    fetch_marc_hash(instance_hrid: instance_hrid)
  )
  updated_marc = MARC::Record.new
  updated_marc.leader = marc_record.leader
  marc_record.fields.each do |field|
    # explicitly remove all listed tags from the record
    next if FIELDS_TO_REMOVE.include?(field.tag)

    updated_marc.fields << field
  end
  # explicitly inject the instance_hrid into the 001 field
  updated_marc.fields << MARC::ControlField.new('001', instance_hrid)
  # explicitly inject FOLIO into the 003 field
  updated_marc.fields << MARC::ControlField.new('003', 'FOLIO')
  updated_marc.to_xml.to_s
end