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, #with_querystring

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:



67
68
69
70
71
72
73
74
75
76
# File 'lib/dor/services/client/user_version.rb', line 67

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

Returns the object metadata.

Returns:

  • (Cocina::Models::DROWithMetadata)

    the object metadata

Raises:



41
42
43
44
45
46
47
48
# File 'lib/dor/services/client/user_version.rb', line 41

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

  build_cocina_from_response(resp, validate: false)
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) ⇒ Hash

Returns the solr document for the user version.

Returns:

  • (Hash)

    the solr document for the user version

Raises:



52
53
54
55
56
57
58
59
# File 'lib/dor/services/client/user_version.rb', line 52

def solr(version)
  resp = connection.get do |req|
    req.url "#{base_path}/#{version}/solr"
  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:



85
86
87
88
89
90
91
92
93
94
# File 'lib/dor/services/client/user_version.rb', line 85

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