Class: Dor::Services::Client::UserVersion

Inherits:
VersionedService show all
Defined in:
lib/dor/services/client/user_version.rb

Overview

API calls that are about user versions

Defined Under Namespace

Classes: Version

Constant Summary

Constants inherited from VersionedService

VersionedService::EXCEPTION_CLASS, VersionedService::JSON_API_MIME_TYPE

Instance Method Summary collapse

Methods inherited from VersionedService

#async_result

Constructor Details

#initialize(connection:, version:, object_identifier:) ⇒ UserVersion

Returns a new instance of UserVersion.

Parameters:

  • object_identifier (String)

    the pid for the object



23
24
25
26
# File 'lib/dor/services/client/user_version.rb', line 23

def initialize(connection:, version:, object_identifier:)
  super(connection: connection, version: version)
  @object_identifier = object_identifier
end

Instance Method Details

#create(object_version:) ⇒ Version

Create a user version for an object

Parameters:

  • object_version (String)

    the version of the object to create a user version for

Returns:

Raises:



73
74
75
76
77
78
79
80
81
82
# File 'lib/dor/services/client/user_version.rb', line 73

def create(object_version:)
  resp = connection.post do |req|
    req.url "#{api_version}/objects/#{object_identifier}/user_versions"
    req.headers['Content-Type'] = 'application/json'
    req.body = { version: object_version }.to_json
  end
  raise_exception_based_on_response!(resp, object_identifier) unless resp.success?

  Version.new(**JSON.parse(resp.body).symbolize_keys!)
end

#find(version) ⇒ Cocina::Models::DROWithMetadata, Dor::Services::Client::InvalidCocina

Returns the object version or an InvalidCocina instance that behaves similarly to a Cocina object.

Returns:

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dor/services/client/user_version.rb', line 42

def find(version)
  resp = connection.get do |req|
    req.url "#{base_path}/#{version}"
  end

  if resp.success?
    build_cocina_from_response(JSON.parse(resp.body), headers: resp.headers, validate: false)
  elsif resp.status == 409 # Signals the Cocina is present but invalid
    build_invalid_cocina_from_response(resp)
  else
    raise_exception_based_on_response!(resp)
  end
end

#inventoryArray

Returns a list of the user versions.

Returns:

  • (Array)

    a list of the user versions

Raises:



30
31
32
33
34
35
36
37
# File 'lib/dor/services/client/user_version.rb', line 30

def inventory
  resp = connection.get do |req|
    req.url base_path
  end
  raise_exception_based_on_response!(resp, object_identifier) unless resp.success?

  JSON.parse(resp.body).fetch('user_versions').map { |params| Version.new(**params.symbolize_keys!) }
end

#solr(version, validate: true) ⇒ Hash

Returns the solr document for the user version.

Returns:

  • (Hash)

    the solr document for the user version

Raises:



58
59
60
61
62
63
64
65
# File 'lib/dor/services/client/user_version.rb', line 58

def solr(version, validate: true)
  resp = connection.get do |req|
    req.url "#{base_path}/#{version}/solr?validate=#{validate}"
  end
  raise_exception_based_on_response!(resp) unless resp.success?

  JSON.parse(resp.body)
end

#update(user_version:) ⇒ Version

Updates a user version

rubocop:disable Metrics/AbcSize

Parameters:

  • user_version (Version)

    the updated user version

Returns:

Raises:



91
92
93
94
95
96
97
98
99
100
# File 'lib/dor/services/client/user_version.rb', line 91

def update(user_version:)
  resp = connection.patch do |req|
    req.url "#{api_version}/objects/#{object_identifier}/user_versions/#{user_version.userVersion}"
    req.headers['Content-Type'] = 'application/json'
    req.body = user_version.to_h.except(:userVersion).compact.to_json
  end
  raise_exception_based_on_response!(resp, object_identifier) unless resp.success?

  Version.new(**JSON.parse(resp.body).symbolize_keys!)
end