Class: FolioClient::RecordsEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/folio_client/records_editor.rb

Overview

Edit MARC JSON records in Folio

Instance Method Summary collapse

Instance Method Details

#edit_marc_json(hrid:) {|record_json| ... } ⇒ Object

TODO:

If this is a problem in practice, see if it's possible to have Folio respond in a more standard way; or, workaround with error handling.

Note:

in limited manual testing, optimistic locking behaved like so when two edit attempts collided:

  • One updating client would eventually raise a timeout. This updating client would actually write successfully, and version the record.
  • The other updating client would raise a StandardError, with a message like 'duplicate key value violates unique constraint "idx_records_matched_id_gen"'. This client would fail to write.
  • As opposed to the expected behavior of the "winner" getting a 200 ok response, and the "loser" getting a 409 conflict response.

Given an HRID, retrieves the associated MARC JSON, yields it to the caller as a hash, and attempts to re-save it, using optimistic locking to prevent accidental overwrite, in case another user or process has updated the record in the time between retrieval and attempted save.

Parameters:

  • hrid (String)

    the HRID of the MARC record to be edited and saved

Yield Parameters:

  • record_json (Hash)

    a hash representation of the MARC JSON for the HRID; the updated hash will be saved when control is returned from the block.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/folio_client/records_editor.rb', line 20

def edit_marc_json(hrid:)
  instance_info = client.fetch_instance_info(hrid: hrid)

  version = instance_info['_version']
  external_id = instance_info['id']

  record_json = client.get('/records-editor/records', { externalId: external_id })

  parsed_record_id = record_json['parsedRecordId']
  # setting this field on the JSON we send back is what will allow optimistic locking to catch stale updates
  record_json['relatedRecordVersion'] = version
  record_json['_actionType'] = 'edit'

  original_record_json = record_json.deep_dup

  yield record_json

  client.put("/records-editor/records/#{parsed_record_id}", record_json) unless record_json == original_record_json
end