Class: FolioClient::SourceStorage
- Inherits:
-
Object
- Object
- FolioClient::SourceStorage
- 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
-
#fetch_marc_hash(instance_hrid:) ⇒ Hash
get marc bib data from folio given an instance HRID.
-
#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.
Instance Method Details
#fetch_marc_hash(instance_hrid:) ⇒ Hash
get marc bib data from folio given an instance HRID
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
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 .nil? && instance_hrid.nil? raise ArgumentError, 'Either a barcode or a Folio instance HRID must be provided' end instance_hrid ||= client.fetch_hrid(barcode: ) if instance_hrid.blank? raise ResourceNotFound, "Catalog record not found. HRID: #{instance_hrid} | 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 |