Sha256: 1411d750d533e0a2c034f292b0d6710de8e5b1e4b90e7beaaf80147d1b224b17

Contents?: true

Size: 1.8 KB

Versions: 17

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'sdr_client/deposit/file_metadata_builder_operations/mime_type'
require 'sdr_client/deposit/file_metadata_builder_operations/md5'
require 'sdr_client/deposit/file_metadata_builder_operations/sha1'

module SdrClient
  module Deposit
    # Build basic metadata for files, iterating over a series of operations
    # The available options are here: https://github.com/sul-dlss/sdr-client/blob/v0.8.1/lib/sdr_client/deposit/file.rb#L8-L10
    class FileMetadataBuilder
      OPERATIONS = [
        FileMetadataBuilderOperations::MimeType,
        FileMetadataBuilderOperations::MD5,
        FileMetadataBuilderOperations::SHA1
      ].freeze
      private_constant :OPERATIONS

      # @param (see #initialize)
      # @return (see #build)
      def self.build(files:, files_metadata:, basepath:)
        new(files: files, files_metadata: files_metadata.dup, basepath: basepath).build
      end

      # @param [Array<String>] files the list of relative filepaths for which to generate metadata
      def initialize(files:, files_metadata:, basepath:)
        @files = files
        @files_metadata = files_metadata
        @basepath = basepath
      end

      # @return [Hash<String, Hash<String, String>>] a map of relative filepaths to a map of metadata
      def build
        files.each do |filepath|
          OPERATIONS.each do |operation|
            result = operation.for(filepath: absolute_filepath_for(filepath))
            next if result.nil?

            files_metadata[filepath] ||= {}
            files_metadata[filepath][operation::NAME] = result
          end
        end
        files_metadata
      end

      private

      attr_reader :files, :files_metadata, :basepath

      def absolute_filepath_for(filepath)
        ::File.join(basepath, filepath)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
sdr-client-2.11.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.10.1 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.10.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.9.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.8.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.7.1 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.7.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.6.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.5.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.4.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.3.1 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.3.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.2.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.1.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.0.1 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-2.0.0 lib/sdr_client/deposit/file_metadata_builder.rb
sdr-client-1.0.0 lib/sdr_client/deposit/file_metadata_builder.rb