Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Created May 6, 2015 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyf/4272657d50c4f5c7caf3 to your computer and use it in GitHub Desktop.
Save jeremyf/4272657d50c4f5c7caf3 to your computer and use it in GitHub Desktop.
Dependency Injection for Sipity
class Etd::Mapper
def initialize(work:, repository: default_repository)
self.work = work
self.repository = repository
end
def call
# do things
end
private
attr_accessor :work, :repository
def default_repository
QueryRepository.new
end
end
RSpec.describe Etd::Mapper do
let(:work) { double }
let(:repository) { QueryRepositoryInterface.new }
subject { described_class.new(work: work, repository: repository) }
it 'will map additional attributes' do
expect(repository).to receive(:work_attribute_key_value_pairs).with(work: work).and_return([['language', 'eng']])
subject.call
end
end
@banurekha
Copy link

module Sipity
  # :nodoc:
  module Mappers
    #Mapper to map etd work into descMetadata
    class EtdMapper
      CONTENT_MODEL_NAME = 'Etd'.freeze
      NAMESPACE = 'und:'.freeze
      BATCH_USER = 'curate_batch_user'.freeze
      TERM_URI = {
          dc: 'http://purl.org/dc/terms/',
          rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
          ms: 'http://www.ndltd.org/standards/metadata/etdms/1.1/',
          ths: 'http://id.loc.gov/vocabulary/relators/'
      }.freeze
      PID_KEY= 'pid'.freeze
      TYPE_KEY= 'type'.freeze
      CONTEXT_KEY= '@context'.freeze
      AF_MODEL_KEY= 'af-model'.freeze
      DESC_METADATA_KEY= 'metadata'.freeze
      ACCESS_RIGHTS_KEY= 'rights'.freeze
      PROPERTIES_METADATA_KEY= 'properties-meta'.freeze
      PROPERTIES_KEY = 'properties'.freeze
      READ_KEY= 'read'.freeze
      READ_GROUP_KEY= 'read-groups'.freeze
      EMBARGO_KEY= 'embargo-date'.freeze

      PUBLIC_ACCESS = 'public'.freeze
      ND_ONLY_ACCESS = 'restricted'.freeze

      def self.call(work)
        new(work).call
      end

      def initialize(work, repository: default_repository)
        puts "Repo: #{repository.inspect}"
        self.work = work
        self.repository = repository
      end

      def call
        build_json
      end

      def build_json
        Jbuilder.encode do |json|
          gather_work_metadata(json)
          #json.set!(ACCESS_RIGHTS_KEY, decode_access_rights)
          transform_attributes_to_metadata(json)
          json.set! PROPERTIES_METADATA_KEY.to_sym do
            json.set!('mime-type', 'text/xml')
          end
          json.set!(PROPERTIES_KEY, "<fields><depositor>#{BATCH_USER}</depositor></fields>")
        end
      end

      private

      attr_accessor :work, :repository

      def default_repository
        QueryRepository.new
      end

      def pid
        NAMESPACE + work.id
      end

      def attributes
        repository.work_attribute_key_value_pairs(work: work).to_h
      end

      def access_rights
        repository.work_access_right_codes(work: work)
      end

      def creators
        repository.scope_users_for_entity_and_roles(entity: work, roles: Models::Role::CREATING_USER).map(&:username)
      end

      def gather_work_metadata(json)
        json.set!(TYPE_KEY, "fobject")
        #json.set!(PID_KEY, pid)
        json.set!(AF_MODEL_KEY, CONTENT_MODEL_NAME)
      end

      def transform_attributes_to_metadata(json)
        json.set! DESC_METADATA_KEY do
          json.set! CONTEXT_KEY do
            TERM_URI.each { |key, uri|   json.set!(key, uri) }
          end
          attributes.each do |key, value|
            json.set!(extract_name_for(key), value)
          end
        end
      end

      def decode_access_rights
        #determine and add Public, Private, Embargo and ND only rights
        decode_access_rights = []
        access_rights.each do |access_right|
          case access_right
            when Models::AccessRight::OPEN_ACCESS
              decode_access_rights << { READ_GROUP_KEY => [PUBLIC], READ_KEY => [creators]}
            when Models::AccessRight::RESTRICTED_ACCESS
              decode_access_rights << {READ_GROUP_KEY => [ND_ONLY_ACCESS], READ_KEY => [creators]}
            when Models::AccessRight::EMBARGO_THEN_OPEN_ACCESS
              decode_access_rights << {EMBARGO_KEY => get_embargo_date, READ_KEY => [creators]}
            when Models::AccessRight::PRIVATE_ACCESS
              decode_access_rights << {READ_KEY => [creators]}
          end
        end
        decode_access_rights
      end

      def get_embargo_date
        embargo_dates = work.access_rights.pluck(:transition_date)
        embargo_dates.map{ |dt| dt.strftime('%Y-%m-%d') if dt.present?}
      end

      def extract_name_for(attribute)
        ETD_ATTRIBUTES[attribute]
      end
    end
  end
end


module Sipity
  module Mappers
    RSpec.describe EtdMapper do
      let(:access_right) {['access_code'] }
      let(:work) { double }
      let(:repository) { QueryRepositoryInterface.new }

      subject { described_class.new(work: work, repository: repository) }

      it 'will instantiate then call the instance' do
        expect(described_class).to receive(:new).and_return(double(call: true))
        described_class.call(work)
      end

      it 'will map additional attributes' do
        expect(repository).to receive(:work_attribute_key_value_pairs).with(work: work).and_return([['language', 'eng']])
        expect(repository).to receive(:work_access_right_codes).with(work: work).and_return(access_right)
        expect(work).to receive(:id).and_return('a_id')
        expect(subject.build_json).to eq([])
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment