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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SourceStorage

Returns a new instance of SourceStorage.

Parameters:



11
12
13
# File 'lib/folio_client/source_storage.rb', line 11

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/folio_client/source_storage.rb', line 8

def client
  @client
end

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:



20
21
22
23
24
25
26
27
28
# File 'lib/folio_client/source_storage.rb', line 20

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?
  raise MultipleResourcesFound, "Expected 1 record for #{instance_hrid}, but found #{record_count}" if record_count > 1

  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

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:



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

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

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

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

  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