-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Job for importing an object from Purl (Stanford)
Still need to wrap this with webmock or similar and invoke it via a script
- Loading branch information
Showing
9 changed files
with
147 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'stanford' | ||
# Import works from Purl/stacks services at Stanford | ||
# | ||
# Example usage: | ||
# log = Hyrax::Operation.create!(user: current_user, | ||
# operation_type: "Import Purl Metadata") | ||
# ImportWorkFromPurlJob.perform_later(current_user, | ||
# 'abcd1234xxxx', | ||
# log) | ||
class ImportWorkFromPurlJob < ActiveJob::Base | ||
queue_as :ingest | ||
|
||
before_enqueue do |job| | ||
log = job.arguments.last | ||
log.pending_job(self) | ||
end | ||
|
||
# This copies metadata from the passed in attribute to all of the works that | ||
# are members of the given upload set | ||
# @param [User] user | ||
# @param [String] druid | ||
# @param [Hyrax::Operation] log | ||
def perform(user, druid, log) | ||
xml = Stanford::Importer::PurlRetriever.get(druid) | ||
parser = Stanford::Importer::PurlParser.new(xml) | ||
attributes = process_attributes(parser.attributes) | ||
model = model_to_create(attributes) | ||
|
||
CreateWorkJob.perform_now(user, model, attributes, log) | ||
end | ||
|
||
private | ||
|
||
def process_attributes(attributes) | ||
# We're pruning off :form_of_work, :record_origin, :created_attributes, :identifiers | ||
attributes = attributes.slice(*attributes_to_keep) | ||
# rename :location to :based_near | ||
attributes[:based_near] = attributes.delete(:location) | ||
|
||
process_collection(attributes) | ||
filenames = attributes.delete(:files) | ||
attributes[:remote_files] = filenames.map do |name| | ||
{ url: "https://stacks.stanford.edu/file/druid:#{attributes[:id]}/#{name}", | ||
file_name: name } | ||
end | ||
|
||
attributes | ||
end | ||
|
||
class_attribute :attributes_to_keep | ||
self.attributes_to_keep = [:title, | ||
:description, | ||
:subject, | ||
:language, | ||
:resource_type, | ||
:location, | ||
:rights, | ||
:visibility, | ||
:id, | ||
:collection, | ||
:files] | ||
|
||
def process_collection(attributes) | ||
# rename :collection to :member_of_collection_ids | ||
collection = attributes.delete(:collection) | ||
|
||
Collection.create!(collection) unless Collection.exists?(collection[:id]) | ||
attributes[:member_of_collection_ids] = [collection[:id]] | ||
end | ||
|
||
# Override this method if you have a different rubric for choosing the model | ||
# @param [Hash] attributes | ||
# @return String the model to create | ||
def model_to_create(attributes) | ||
Hyrax.config.model_to_create.call(attributes) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ module Importer | |
autoload :ModsParser | ||
autoload :PurlImporter | ||
autoload :PurlParser | ||
autoload :PurlRetriever | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Stanford | ||
module Importer | ||
class PurlRetriever | ||
def self.get(druid) | ||
new(druid).get | ||
end | ||
|
||
def initialize(druid) | ||
@druid = druid | ||
end | ||
|
||
def get | ||
conn.get("/#{@druid}.xml").body | ||
end | ||
|
||
def conn | ||
@conn ||= Faraday.new(url: 'https://purl.stanford.edu') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ImportWorkFromPurlJob do | ||
let(:user) { create(:user) } | ||
let(:log) { Hyrax::Operation.create!(user: user, operation_type: "Import Purl Metadata") } | ||
let(:druid) { 'bc390xk2647' } | ||
before do | ||
if ActiveFedora::Base.exists? druid | ||
ActiveFedora::Base.find(druid).destroy(eradicate: true) | ||
end | ||
Hyrax::Workflow::WorkflowImporter.load_workflows | ||
Hyrax::PermissionTemplate.create!(admin_set_id: Hyrax::DefaultAdminSetActor::DEFAULT_ID, workflow_name: 'default') | ||
end | ||
it "works" do | ||
expect do | ||
described_class.perform_now(user, druid, log) | ||
end.to change { GenericWork.count }.by(1) | ||
.and change { FileSet.count }.by(2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters