Sha256: 5d814b2ec8b618af87b281d60a15e45487711eca3068c873899bed0ad4ef4947

Contents?: true

Size: 1.5 KB

Versions: 12

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module SdrClient
  module Deposit
    # Creates a resource (metadata) in SDR
    class CreateResource
      DRO_PATH = '/v1/resources'

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

      # @param [Boolean] accession should the accessionWF be started
      # @param [Cocina::Models::RequestDRO, Cocina::Models::RequestCollection] metadata
      # @param [Hash<Symbol,String>] the result of the metadata call
      def initialize(accession:, metadata:, logger:, connection:)
        @accession = accession
        @metadata = metadata
        @logger = logger
        @connection = connection
      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 == 201

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

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

      private

      attr_reader :metadata, :logger, :connection

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

        connection.post(path, json, 'Content-Type' => 'application/json')
      end

      def accession?
        @accession
      end

      def path
        "#{DRO_PATH}?accession=#{accession?}"
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
sdr-client-0.55.1 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.55.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.54.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.53.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.52.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.51.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.51.0.beta.1 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.50.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.49.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.48.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.47.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.46.0 lib/sdr_client/deposit/create_resource.rb