Sha256: 57774225e8a4e0a9433c864eed071f5b980cc8c913429e6463fc15e780850b91

Contents?: true

Size: 1.99 KB

Versions: 28

Compression:

Stored size: 1.99 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:, assign_doi: false)
        new(accession: accession,
            assign_doi: assign_doi,
            metadata: metadata,
            logger: logger,
            connection: connection).run
      end

      # @param [Boolean] accession should the accessionWF be started
      # @param [Boolean] assign_doi should a DOI be assigned to this item
      # @param [Cocina::Models::RequestDRO, Cocina::Models::RequestCollection] metadata
      # @param [Hash<Symbol,String>] the result of the metadata call
      def initialize(accession:, assign_doi:, metadata:, logger:, connection:)
        @accession = accession
        @assign_doi = assign_doi
        @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',
                        'X-Cocina-Models-Version' => Cocina::Models::VERSION)
      end

      def accession?
        @accession
      end

      def assign_doi?
        @assign_doi
      end

      def path
        params = { accession: accession? }
        params[:assign_doi] = true if assign_doi? # false is default
        DRO_PATH + '?' + params.map { |k, v| "#{k}=#{v}" }.join('&')
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
sdr-client-0.88.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.87.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.86.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.85.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.84.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.83.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.82.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.81.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.80.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.79.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.78.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.77.1 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.77.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.76.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.75.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.74.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.73.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.72.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.71.0 lib/sdr_client/deposit/create_resource.rb
sdr-client-0.69.1 lib/sdr_client/deposit/create_resource.rb