Module: FolioApiClient::Finders

Included in:
FolioApiClient
Defined in:
lib/folio_api_client/finders.rb

Instance Method Summary collapse

Instance Method Details

#find_holdings_record(holdings_record_id:) ⇒ Object



22
23
24
# File 'lib/folio_api_client/finders.rb', line 22

def find_holdings_record(holdings_record_id:)
  self.get("/holdings-storage/holdings/#{holdings_record_id}")
end

#find_instance_record(instance_record_id:) ⇒ Object



26
27
28
# File 'lib/folio_api_client/finders.rb', line 26

def find_instance_record(instance_record_id:)
  self.get("/instance-storage/instances/#{instance_record_id}")
end

#find_item_record(barcode:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/folio_api_client/finders.rb', line 5

def find_item_record(barcode:)
  item_search_results = self.get('/item-storage/items', { query: "barcode==#{barcode}", limit: 2 })['items']
  return nil if item_search_results.empty?

  if item_search_results.length > 1
    raise FolioApiClient::Exceptions::UnexpectedMultipleRecordsFoundError,
          'Only expected one item with this barcode, but found more than one.'
  end

  item_record_id = item_search_results.first['id']
  self.get("/item-storage/items/#{item_record_id}")
end

#find_location_record(location_id:) ⇒ Object



18
19
20
# File 'lib/folio_api_client/finders.rb', line 18

def find_location_record(location_id:)
  self.get("/locations/#{location_id}")
end

#find_marc_record(instance_record_id: nil, instance_record_hrid: nil) ⇒ Object

Find a MARC::Record by its instance record id or instance record hrid



31
32
33
34
35
36
37
38
39
40
# File 'lib/folio_api_client/finders.rb', line 31

def find_marc_record(instance_record_id: nil, instance_record_hrid: nil)
  source_record_search_results = self.get(
    '/source-storage/source-records',
    marc_record_query(instance_record_id: instance_record_id, instance_record_hrid: instance_record_hrid)
  )
  return nil if source_record_search_results['totalRecords'].zero?

  bib_record_marc_hash = source_record_search_results['sourceRecords'].first['parsedRecord']['content']
  MARC::Record.new_from_hash(bib_record_marc_hash)
end

#marc_record_query(instance_record_id: nil, instance_record_hrid: nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/folio_api_client/finders.rb', line 42

def marc_record_query(instance_record_id: nil, instance_record_hrid: nil)
  return { instanceId: instance_record_id } if instance_record_id
  return { instanceHrid: instance_record_hrid } if instance_record_hrid

  raise FolioApiClient::Exceptions::MissingQueryFieldError,
        'Missing query field.  Must supply either an instance_record_id or instance_record_hrid.'
end