Sha256: fa0f1e396258777f8217ebdf88094a333b1a27700dfc9ec6cc9688c855af8b3d

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module SdrClient
  module Deposit
    # Uploads a resource (metadata) to the server
    class UploadResource
      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 [String] 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 [Hash<Symbol,String>] the result of the metadata call
      def run
        response = metadata_request
        unexpected_response(response) unless response.status == 201

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

        { druid: JSON.parse(response.body)['druid'], background_job: response.headers['Location'] }
      end

      private

      attr_reader :metadata, :logger, :connection

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

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

      def unexpected_response(response)
        raise "There was an error with your request: #{response.body}" if response.status == 400
        raise 'There was an error with your credentials. Perhaps they have expired?' if response.status == 401

        raise "unexpected response: #{response.status} #{response.body}"
      end

      def accession?
        @accession
      end

      def path
        path = DRO_PATH
        path += '?accession=true' if accession?
        path
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sdr-client-0.25.0 lib/sdr_client/deposit/upload_resource.rb
sdr-client-0.24.0 lib/sdr_client/deposit/upload_resource.rb
sdr-client-0.23.1 lib/sdr_client/deposit/upload_resource.rb
sdr-client-0.23.0 lib/sdr_client/deposit/upload_resource.rb