Sha256: 431cd9d47b37327299216d58ae88ccf72eb853099ecd9201ef6dab92c403bef7

Contents?: true

Size: 1.99 KB

Versions: 58

Compression:

Stored size: 1.99 KB

Contents

module Hyrax
  module Arkivo
    CREATOR_TYPES = ['author', 'interviewer', 'director', 'scriptwriter',
                     'inventor', 'composer', 'cartographer', 'programmer', 'artist',
                     'bookAuthor'].freeze

    CONTRIBUTOR_TYPES = ['contributor', 'editor', 'translator', 'seriesEditor',
                         'interviewee', 'producer', 'castMember', 'sponsor', 'counsel',
                         'attorneyAgent', 'recipient', 'performer', 'wordsBy', 'commenter',
                         'presenter', 'guest', 'podcaster', 'reviewedAuthor', 'cosponsor'].freeze

    class MetadataMunger
      def initialize(metadata)
        @metadata = metadata
        @munged = {}
      end

      # @return [Hash]
      def call
        normalize_keys_and_values
        rename_key(from: 'url', to: 'related_url')
        rename_key(from: 'tags', to: 'keyword')
        extract_creator_and_contributor_from_creators
        @munged
      end

      private

        def normalize_keys_and_values
          # First, normalize camelCase symbols to underscore strings
          @metadata.each do |key, value|
            @munged[key.to_s.underscore] = Array.wrap(value)
          end
        end

        def rename_key(from:, to:)
          @munged[to] = @munged.delete(from) if @munged.key?(from)
        end

        def extract_creator_and_contributor_from_creators
          creator_names = []
          contributor_names = []
          @munged['creators'].each do |entry|
            entry['name'] ||= "#{entry.delete('lastName')}, #{entry.delete('firstName')}".strip
            creator_names << entry['name'] if Hyrax::Arkivo::CREATOR_TYPES.include?(entry['creatorType'])
            contributor_names << entry['name'] if Hyrax::Arkivo::CONTRIBUTOR_TYPES.include?(entry['creatorType'])
          end
          @munged['creator'] = creator_names if creator_names.present?
          @munged['contributor'] = contributor_names if contributor_names.present?
          @munged.delete('creators')
        end
    end
  end
end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
hyrax-2.9.6 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.9.5 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.9.4 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.9.3 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.9.2 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.9.1 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.9.0 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.8.0 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.7.2 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.7.1 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.7.0 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.6.0 lib/hyrax/arkivo/metadata_munger.rb
hyrax-3.0.0.pre.rc1 lib/hyrax/arkivo/metadata_munger.rb
hyrax-3.0.0.pre.beta3 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.5.1 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.5.0 lib/hyrax/arkivo/metadata_munger.rb
hyrax-3.0.0.pre.beta2 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.4.1 lib/hyrax/arkivo/metadata_munger.rb
hyrax-3.0.0.pre.beta1 lib/hyrax/arkivo/metadata_munger.rb
hyrax-2.4.0 lib/hyrax/arkivo/metadata_munger.rb