Class: FolioClient::Inventory
- Inherits:
-
Object
- Object
- FolioClient::Inventory
- Defined in:
- lib/folio_client/inventory.rb
Overview
Lookup items in the Folio inventory
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
-
#fetch_external_id(hrid:) ⇒ String?
get instance external ID from HRID.
-
#fetch_hrid(barcode:) ⇒ String?
get instance HRID from barcode.
-
#fetch_instance_info(external_id: nil, hrid: nil) ⇒ Hash
Retrieve basic information about a instance record.
-
#has_instance_status?(hrid:, status_id:) ⇒ Boolean
True if instance status matches the uuid param, false otherwise.
-
#initialize(client) ⇒ Inventory
constructor
A new instance of Inventory.
Constructor Details
#initialize(client) ⇒ Inventory
Returns a new instance of Inventory.
9 10 11 |
# File 'lib/folio_client/inventory.rb', line 9 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
6 7 8 |
# File 'lib/folio_client/inventory.rb', line 6 def client @client end |
Instance Method Details
#fetch_external_id(hrid:) ⇒ String?
get instance external ID from HRID
32 33 34 35 36 37 38 39 |
# File 'lib/folio_client/inventory.rb', line 32 def fetch_external_id(hrid:) instance_response = client.get("/search/instances", {query: "hrid==#{hrid}"}) record_count = instance_response["totalRecords"] raise ResourceNotFound, "No matching instance found for #{hrid}" if instance_response["totalRecords"] == 0 raise MultipleResourcesFound, "Expected 1 record for #{hrid}, but found #{record_count}" if record_count > 1 instance_response.dig("instances", 0, "id") end |
#fetch_hrid(barcode:) ⇒ String?
get instance HRID from barcode
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/folio_client/inventory.rb', line 16 def fetch_hrid(barcode:) # find the instance UUID for this barcode instance = client.get("/search/instances", {query: "items.barcode==#{}"}) instance_uuid = instance.dig("instances", 0, "id") return nil unless instance_uuid # next lookup the instance given the instance_uuid so we can fetch the hrid result = client.get("/inventory/instances/#{instance_uuid}") result.dig("hrid") end |
#fetch_instance_info(external_id: nil, hrid: nil) ⇒ Hash
Retrieve basic information about a instance record. Example usage: get the external ID and _version for update using
optimistic locking when the HRID is available: `fetch_instance_info(hrid: 'a1234').slice('id', '_version')`
(or vice versa if the external ID is available).
48 49 50 51 52 53 54 |
# File 'lib/folio_client/inventory.rb', line 48 def fetch_instance_info(external_id: nil, hrid: nil) raise ArgumentError, "must pass exactly one of external_id or HRID" unless external_id.present? || hrid.present? raise ArgumentError, "must pass exactly one of external_id or HRID" if external_id.present? && hrid.present? external_id ||= fetch_external_id(hrid: hrid) client.get("/inventory/instances/#{external_id}") end |
#has_instance_status?(hrid:, status_id:) ⇒ Boolean
Returns true if instance status matches the uuid param, false otherwise.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/folio_client/inventory.rb', line 60 def has_instance_status?(hrid:, status_id:) # get the instance record and its statusId instance = client.get("/inventory/instances", {query: "hrid==#{hrid}"}) raise ResourceNotFound, "No matching instance found for #{hrid}" if instance["totalRecords"] == 0 instance_status_id = instance.dig("instances", 0, "statusId") return false unless instance_status_id return true if instance_status_id == status_id false end |