Sha256: d577ba5e0122cf5915d6e773ee23547fbdc8232fb3298ebf86fb0ce04de78fa3

Contents?: true

Size: 1.75 KB

Versions: 31

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module SdrClient
  module Deposit
    # Updates a resource (metadata) in SDR
    class UpdateResource
      DRO_PATH = '/v1/resources/%<id>s'

      def self.run(metadata:, logger:, connection:, version_description: nil)
        new(metadata: metadata, logger: logger, connection: connection, version_description: version_description).run
      end

      # @param [Cocina::Models::DRO] metadata
      # @param [Hash<Symbol,String>] the result of the metadata call
      # @param [String] version_description
      def initialize(metadata:, logger:, connection:, version_description: nil)
        @metadata = metadata
        @logger = logger
        @connection = connection
        @version_description = version_description
      end

      # @param [Hash<Symbol,String>] the result of the metadata call
      # @return [String] job id for the background job result
      def run
        response = metadata_request
        UnexpectedResponse.call(response) unless response.status == 202

        logger.info("Response from server: #{response.body}")

        JSON.parse(response.body)['jobId']
      end

      private

      attr_reader :metadata, :logger, :connection, :version_description

      def metadata_request
        json = metadata.to_json
        logger.debug("Starting update metadata: #{json}")

        connection.put(path(metadata), json,
                       'Content-Type' => 'application/json',
                       'X-Cocina-Models-Version' => Cocina::Models::VERSION) do |req|
                         req.params['versionDescription'] = version_description if version_description
                       end
      end

      def path(metadata)
        format(DRO_PATH, id: metadata.externalIdentifier)
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
sdr-client-2.13.2 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.13.1 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.13.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.13.0.beta2 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.13.0.beta1 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.12.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.11.1 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.11.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.10.1 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.10.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.9.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.8.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.7.1 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.7.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.6.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.5.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.4.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.3.1 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.3.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.2.0 lib/sdr_client/deposit/update_resource.rb