Sha256: a6e538f0b8f3fe0468c7d6690dd12026213312aeb60c9a6b4938174a409d9226

Contents?: true

Size: 1.92 KB

Versions: 46

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require 'logger'

module SdrClient
  module Deposit
    # Constructs the deposit metadata for the DRO
    class MetadataBuilder
      # @param [Request] metadata information about the object
      # @param [Class] grouping_strategy class whose run method groups an array of uploads
      # Additional metadata includes access, preserve, shelve, md5, sha1
      # @param [Logger] logger the logger to use
      def initialize(metadata:, grouping_strategy:, logger:)
        @metadata = metadata
        @logger = logger
        @grouping_strategy = grouping_strategy
      end

      # @param [UploadFiles] upload_responses the uploaded file information
      # @return [Request] the metadata with fileset information added in.
      def with_uploads(upload_responses)
        file_sets = build_filesets(uploads: upload_responses)
        metadata.with_file_sets(file_sets)
      end

      private

      attr_reader :metadata, :files, :logger, :grouping_strategy

      # @param [Array<SdrClient::Deposit::Files::DirectUploadResponse>] uploads the uploaded files to attach.
      # @return [Array<SdrClient::Deposit::FileSet>] the uploads transformed to filesets
      def build_filesets(uploads:)
        grouped_uploads = grouping_strategy.run(uploads: uploads)
        grouped_uploads.map.with_index(1) do |upload_group, i|
          FileSet.new(uploads: upload_group,
                      uploads_metadata: metadata_group(upload_group),
                      label: label(i))
        end
      end

      def label(index)
        case metadata.type
        when BOOK_TYPE
          "Page #{index}"
        else
          "Object #{index}"
        end
      end

      # Get the metadata for the files belonging to a fileset
      def metadata_group(upload_group)
        upload_group.each_with_object({}) do |upload, obj|
          obj[upload.filename] = metadata.for(upload.filename)
        end
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
sdr-client-0.50.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.49.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.48.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.47.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.46.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.45.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.44.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.43.1 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.43.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.42.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.41.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.40.1 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.40.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.39.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.38.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.37.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.36.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.35.1 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.35.0 lib/sdr_client/deposit/metadata_builder.rb
sdr-client-0.34.0 lib/sdr_client/deposit/metadata_builder.rb