Class: FolioClient::RecordsEditor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ RecordsEditor

Returns a new instance of RecordsEditor.

Parameters:



8
9
10
# File 'lib/folio_client/records_editor.rb', line 8

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/folio_client/records_editor.rb', line 5

def client
  @client
end

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.



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

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"]
  record_json["relatedRecordVersion"] = version # setting this field on the JSON we send back is what will allow optimistic locking to catch stale updates

  yield record_json

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