Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #726 from pulibrary/pudl-collections-json
Browse files Browse the repository at this point in the history
Pulling collection info from IsPartOf relationship and pudl_collections.json config file
  • Loading branch information
Trey Pendragon committed Sep 13, 2016
2 parents bc0f516 + 603a3ad commit 035566c
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 10 deletions.
25 changes: 21 additions & 4 deletions app/jobs/ingest_mets_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ class IngestMETSJob < ActiveJob::Base

# @param [String] mets_file Filename of a METS file to ingest
# @param [String] user User to ingest as
# @param [Array<String>] collections Collection IDs the resources should be members of
def perform(mets_file, user, collections = [])
def perform(mets_file, user)
logger.info "Ingesting METS #{mets_file}"
@mets = METSDocument.new mets_file
@user = user
@collections = collections.map { |col_id| Collection.find(col_id) }

ingest
end
Expand All @@ -20,7 +18,7 @@ def ingest
resource.identifier = @mets.ark_id
resource.replaces = @mets.pudl_id
resource.source_metadata_identifier = @mets.bib_id
resource.member_of_collections = @collections
resource.member_of_collections = Array(@mets.collection_slugs).map { |slug| find_or_create_collection(slug) }
resource.apply_remote_metadata
resource.save!
logger.info "Created #{resource.class}: #{resource.id}"
Expand All @@ -38,6 +36,25 @@ def ingest
end
end

def find_or_create_collection(slug)
existing = Collection.where exhibit_id_ssim: slug
return existing.first if existing.first
col = Collection.new metadata_for_collection(slug)
col.apply_depositor_metadata @user
col.save!
col
end

def metadata_for_collection(slug)
collection_metadata.each do |c|
return { exhibit_id: slug, title: [c['title']], description: [c['blurb']] } if c['slug'] == slug
end
end

def collection_metadata
@collection_metadata ||= JSON.parse(File.read(File.join(Rails.root, 'config', 'pudl_collections.json')))
end

def attach_mets(resource)
mets_file_set = FileSet.new
mets_file_set.title = ['METS XML']
Expand Down
4 changes: 4 additions & 0 deletions app/models/mets_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def bib_id
@mets.xpath("/mets:mets/mets:dmdSec/mets:mdRef/@xlink:href").to_s.gsub(/.*\//, '')
end

def collection_slugs
@mets.xpath("/mets:mets/mets:structMap[@TYPE='RelatedObjects']//mets:div[@TYPE='IsPartOf']/@CONTENTIDS").to_s
end

def pudl_id
@mets.xpath("/mets:mets/mets:metsHdr/mets:metsDocumentID").first.content.gsub(/\.mets/, '')
end
Expand Down
Loading

0 comments on commit 035566c

Please sign in to comment.