folio_api_client 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd0f25ee382d9468806b7ae7c39a0d7801f43d5e45c7f7bd248565673fe9da23
4
- data.tar.gz: e05cb433eb2134c574d2de93a3e9de4cdad6a4c9bd9c6ae3f67483179f64e855
3
+ metadata.gz: b95e817f47b4694776baae831bcad7784810d06e7533577f27f8bcf4ea216b06
4
+ data.tar.gz: 63291ad25685d7c4ba71dd2399104b8925241da84eca2483f8bd63b4567b4454
5
5
  SHA512:
6
- metadata.gz: f68f5dab0e5080f62b307c8f72bf2fb75e5d4033f7fb70fdfdd99f749edf821ae2c9d1c697e31f70f8af4e1be60aa55c930c9c9bd494348fbff9fe1e7337e563
7
- data.tar.gz: 6cdc0f1d8a571a11599ae906f985f0e2901dedcbc07b8f26f71f316337507693dd22ea0f8a841bbd6083ce6436c5b7dbebe252f4f54d685bb1366c66ec29ebdf
6
+ metadata.gz: '018541167173b30bbf0c938c10465888581fd87d8c64885fd59dfcc3ea4a2305e93d75533f55c752b7eb495ee2b72be7b8514246caffe7bdebbe56b18f5f44ee'
7
+ data.tar.gz: 7812af3dbf43c33aa37c490e8a0a084de82842b64bcdc86571c0eb9e713ad293884f7ff03098d26d15acd75bbcb4a73a89d7f2c2d4eee6e1f32eccf1233405bf
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2025 The Trustees of Columbia University in the City of New York
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md CHANGED
@@ -49,7 +49,13 @@ client.find_item_record(barcode: 'some-barcode')
49
49
  client.find_location_record(location_id: 'some-location-id')
50
50
  client.find_holdings_record(holdings_record_id: 'some-holdings-record-id')
51
51
  client.find_instance_record(instance_record_id: 'some-instance-record-id')
52
- client.find_marc_record(instance_record_id: 'some-instance-record-id') # returns a MARC::Record
52
+ client.find_instance_record(instance_record_hrid: 'some-instance-record-hrid')
53
+ client.find_source_record(instance_record_id: 'some-instance-record-id')
54
+ client.find_source_record(instance_record_hrid: 'some-instance-record-hrid')
55
+
56
+ # Convert a FOLIO MARC source record to a marc gem MARC::Record object:
57
+ source_record = client.find_source_record(instance_record_id: 'some-instance-record-id')
58
+ marc_record = MARC::Record.new_from_hash(source_record['parsedRecord']['content'])
53
59
  ```
54
60
 
55
61
  See [https://dev.folio.org/reference/api/](https://dev.folio.org/reference/api/) for the full list of available FOLIO API endpoints.
@@ -11,40 +11,71 @@ class FolioApiClient
11
11
  'Only expected one item with this barcode, but found more than one.'
12
12
  end
13
13
 
14
- item_record_id = item_search_results.first['id']
15
- self.get("/item-storage/items/#{item_record_id}")
14
+ item_search_results.first
16
15
  end
17
16
 
18
17
  def find_location_record(location_id:)
19
18
  self.get("/locations/#{location_id}")
20
19
  end
21
20
 
21
+ def find_loan_type_record(loan_type_id:)
22
+ self.get("/loan-types/#{loan_type_id}")
23
+ end
24
+
22
25
  def find_holdings_record(holdings_record_id:)
23
26
  self.get("/holdings-storage/holdings/#{holdings_record_id}")
24
27
  end
25
28
 
26
- def find_instance_record(instance_record_id:)
27
- self.get("/instance-storage/instances/#{instance_record_id}")
29
+ def find_instance_record(instance_record_id: nil, instance_record_hrid: nil)
30
+ instance_search_results = self.get(
31
+ '/instance-storage/instances',
32
+ instance_record_query(instance_record_id: instance_record_id, instance_record_hrid: instance_record_hrid)
33
+ )['instances']
34
+ return nil if instance_search_results.empty?
35
+
36
+ if instance_search_results.length > 1
37
+ raise FolioApiClient::Exceptions::UnexpectedMultipleRecordsFoundError,
38
+ 'Only expected one instance with this '\
39
+ "#{instance_record_id ? 'instance_record_id' : 'instance_record_hrid'}, "\
40
+ 'but found more than one.'
41
+ end
42
+
43
+ instance_search_results.first
28
44
  end
29
45
 
30
- # Find a MARC::Record by its instance record id or instance record hrid
31
- def find_marc_record(instance_record_id: nil, instance_record_hrid: nil)
46
+ # Find a source record by its instance record id or instance record hrid.
47
+ # @return [Hash] A Source Record (which can hold data for a MARC record).
48
+ def find_source_record(instance_record_id: nil, instance_record_hrid: nil)
32
49
  source_record_search_results = self.get(
33
50
  '/source-storage/source-records',
34
- marc_record_query(instance_record_id: instance_record_id, instance_record_hrid: instance_record_hrid)
51
+ source_record_query(instance_record_id: instance_record_id, instance_record_hrid: instance_record_hrid)
35
52
  )
36
53
  return nil if source_record_search_results['totalRecords'].zero?
37
54
 
38
- bib_record_marc_hash = source_record_search_results['sourceRecords'].first['parsedRecord']['content']
39
- MARC::Record.new_from_hash(bib_record_marc_hash)
55
+ if source_record_search_results['totalRecords'] > 1
56
+ raise FolioApiClient::Exceptions::UnexpectedMultipleRecordsFoundError,
57
+ 'Only expected one record with this '\
58
+ "#{instance_record_id ? 'instance_record_id' : 'instance_record_hrid'}, "\
59
+ 'but found more than one.'
60
+ end
61
+
62
+ source_record_search_results['sourceRecords'].first
40
63
  end
41
64
 
42
- def marc_record_query(instance_record_id: nil, instance_record_hrid: nil)
65
+ def source_record_query(instance_record_id: nil, instance_record_hrid: nil)
43
66
  return { instanceId: instance_record_id } if instance_record_id
44
67
  return { instanceHrid: instance_record_hrid } if instance_record_hrid
45
68
 
46
69
  raise FolioApiClient::Exceptions::MissingQueryFieldError,
47
70
  'Missing query field. Must supply either an instance_record_id or instance_record_hrid.'
48
71
  end
72
+
73
+ def instance_record_query(instance_record_id: nil, instance_record_hrid: nil)
74
+ return { query: "id==#{instance_record_id}", limit: 2 } if instance_record_id
75
+ return { query: "hrid==#{instance_record_hrid}", limit: 2 } if instance_record_hrid
76
+
77
+ raise FolioApiClient::Exceptions::MissingQueryFieldError,
78
+ 'Missing query field. Must supply either an instance_record_id or instance_record_hrid.'
79
+ end
49
80
  end
50
81
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class FolioApiClient
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -38,7 +38,7 @@ class FolioApiClient
38
38
  end
39
39
 
40
40
  def retrieve_new_auth_token
41
- response = connection.post('/authn/login', { username: config.username, password: config.password }.to_json)
41
+ response = connection.post('/authn/login', JSON.generate({ username: config.username, password: config.password }))
42
42
  response_data = JSON.parse(response.body)
43
43
  response_data['okapiToken']
44
44
  end
@@ -68,12 +68,12 @@ class FolioApiClient
68
68
  end
69
69
 
70
70
  def exec_request_with_body(method, path, body = nil, content_type: 'application/json')
71
- body = body.to_json if content_type == 'application/json' && !body.is_a?(String)
71
+ body = JSON.generate(body) if content_type == 'application/json' && !body.is_a?(String)
72
72
  response = with_token_refresh_attempt_when_unauthorized do
73
73
  connection.send(method, path, body, { 'x-okapi-token': config.token, 'content-type': content_type })
74
74
  end
75
75
 
76
- response.body.nil? ? nil : JSON.parse(response.body)
76
+ response.body.nil? || response.body == '' ? nil : JSON.parse(response.body)
77
77
  end
78
78
 
79
79
  def post(path, body = nil, content_type: 'application/json')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: folio_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric O'Hanlon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-04-30 00:00:00.000000000 Z
11
+ date: 2025-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -63,6 +63,7 @@ files:
63
63
  - ".rspec"
64
64
  - ".rubocop.yml"
65
65
  - ".ruby-version"
66
+ - LICENSE
66
67
  - README.md
67
68
  - Rakefile
68
69
  - lib/folio_api_client.rb
@@ -71,7 +72,8 @@ files:
71
72
  - lib/folio_api_client/finders.rb
72
73
  - lib/folio_api_client/version.rb
73
74
  homepage: https://www.github.com/cul/folio_api_client
74
- licenses: []
75
+ licenses:
76
+ - Apache-2.0
75
77
  metadata:
76
78
  homepage_uri: https://www.github.com/cul/folio_api_client
77
79
  source_code_uri: https://www.github.com/cul/folio_api_client