-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from pulibrary/complete
ARK minting
- Loading branch information
Showing
11 changed files
with
212 additions
and
21 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
class IdentifierService | ||
def self.mint_or_update(persister:, resource:) | ||
if resource.identifier.present? | ||
update_metadata resource | ||
else | ||
mint_identifier resource | ||
persister.save resource: resource | ||
end | ||
end | ||
|
||
private_class_method def self.update_metadata(resource) | ||
return if minter_user == "apitest" | ||
minter.modify(Array.wrap(resource.identifier).first, metadata(resource)) | ||
end | ||
|
||
private_class_method def self.mint_identifier(resource) | ||
resource.identifier = minter.mint(metadata(resource)).id | ||
end | ||
|
||
private_class_method def self.metadata(resource) | ||
{ | ||
dc_publisher: 'Princeton University Library', | ||
dc_title: resource.title.join('; '), | ||
dc_type: 'Text', | ||
target: url_for(resource) | ||
} | ||
end | ||
|
||
private_class_method def self.url_for(resource) | ||
return Rails.application.routes.url_helpers.scanned_resource_url(resource) if resource.try(:source_metadata_identifier).blank? | ||
return "https://pulsearch.princeton.edu/catalog/#{resource.source_metadata_identifier.first}#view" if PulMetadataServices::Client.bibdata?(resource.source_metadata_identifier.first) | ||
"http://findingaids.princeton.edu/collections/#{resource.source_metadata_identifier.first.tr('_', '/')}" | ||
end | ||
|
||
private_class_method def self.minter_user | ||
Ezid::Client.config.user | ||
end | ||
|
||
private_class_method def self.minter | ||
Ezid::Identifier | ||
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,6 @@ | ||
# frozen_string_literal: true | ||
Ezid::Client.configure do |conf| | ||
conf.default_shoulder = 'ark:/99999/fk4' unless ENV['EZID_DEFAULT_SHOULDER'] | ||
conf.user = 'apitest' unless ENV['EZID_USER'] | ||
conf.password = 'apitest' unless ENV['EZID_PASSWORD'] | ||
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,99 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
|
||
RSpec.describe IdentifierService do | ||
let(:ark) { 'ark:88435/x1234567' } | ||
let(:minter) { class_double('Ezid::Identifier') } | ||
let(:identifier) { instance_double('Ezid::Identifier') } | ||
let(:base_metadata) { { dc_publisher: 'Princeton University Library', dc_title: 'Title', dc_type: 'Text' } } | ||
let(:persister) { Valkyrie.config.metadata_adapter.persister } | ||
|
||
before do | ||
Rails.application.routes.default_url_options[:host] = 'example.com' | ||
end | ||
|
||
context "when there is an existing identifier" do | ||
let(:metadata) { base_metadata.merge(target: "https://pulsearch.princeton.edu/catalog/123456#{obj.id}#view") } | ||
let(:obj) { FactoryGirl.build :scanned_resource, source_metadata_identifier: '123456', identifier: ark } | ||
|
||
before do | ||
stub_bibdata(bib_id: '123456') | ||
allow(described_class).to receive(:minter).and_return(minter) | ||
allow(described_class).to receive(:minter_user).and_return('pudiglib') | ||
allow(minter).to receive(:modify) | ||
end | ||
|
||
it "updates the ark" do | ||
described_class.mint_or_update(resource: obj, persister: persister) | ||
expect(minter).to have_received(:modify).with(ark, metadata) | ||
expect(obj.identifier.first).to eq(ark) | ||
end | ||
end | ||
|
||
context "when the identifier is blank" do | ||
before do | ||
allow(described_class).to receive(:minter).and_return(minter) | ||
allow(minter).to receive(:mint).and_return(identifier) | ||
allow(identifier).to receive(:id).and_return(ark) | ||
end | ||
|
||
context "with a bibdata source_metadata_identifier" do | ||
let(:bib) { '123456' } | ||
let(:metadata) { base_metadata.merge(target: "https://pulsearch.princeton.edu/catalog/#{bib}#view") } | ||
let(:obj) { FactoryGirl.build :scanned_resource, source_metadata_identifier: bib } | ||
|
||
before do | ||
stub_bibdata(bib_id: '123456') | ||
end | ||
|
||
it "links to OrangeLight" do | ||
described_class.mint_or_update(resource: obj, persister: persister) | ||
expect(minter).to have_received(:mint).with(metadata) | ||
end | ||
end | ||
|
||
context "with a pulfa source_metadata_identifier" do | ||
let(:cid) { 'MC016/c9616' } | ||
let(:metadata) { base_metadata.merge(target: "http://findingaids.princeton.edu/collections/#{cid}") } | ||
let(:obj) { FactoryGirl.build :scanned_resource, source_metadata_identifier: cid } | ||
|
||
before do | ||
stub_pulfa(pulfa_id: 'MC016/c9616') | ||
end | ||
|
||
it "links to OrangeLight" do | ||
described_class.mint_or_update(resource: obj, persister: persister) | ||
expect(minter).to have_received(:mint).with(metadata) | ||
end | ||
end | ||
|
||
context "without a source_metadata_identifier" do | ||
let(:metadata) { base_metadata.merge(target: "http://example.com/concern/scanned_resources/#{obj.id}") } | ||
let(:obj) { FactoryGirl.create :scanned_resource, id: '1234567', source_metadata_identifier: nil } | ||
it "links to OrangeLight" do | ||
described_class.mint_or_update(resource: obj, persister: persister) | ||
expect(minter).to have_received(:mint).with(metadata) | ||
end | ||
end | ||
end | ||
|
||
context "integration test" do | ||
let(:metadata) { base_metadata.merge(target: "http://example.com/concern/scanned_resources/#{obj.id}") } | ||
let(:obj) { FactoryGirl.create :scanned_resource, id: '1234567', source_metadata_identifier: nil } | ||
let(:shoulder) { '99999/fk4' } | ||
let(:blade) { '123456' } | ||
|
||
before do | ||
stub_ezid(shoulder: shoulder, blade: blade) | ||
end | ||
|
||
it "mints an ARK" do | ||
described_class.mint_or_update(resource: obj, persister: persister) | ||
expect(obj.identifier.first).to eq("ark:/#{shoulder}#{blade}") | ||
end | ||
|
||
it "uses a test account" do | ||
expect(described_class.send(:minter_user)).to eq('apitest') | ||
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,12 @@ | ||
# frozen_string_literal: true | ||
module EzidStubbing | ||
def stub_ezid(shoulder:, blade:) | ||
stub_request(:post, "https://ezid.cdlib.org/shoulder/ark:/#{shoulder}") | ||
.to_return(status: 200, body: "id: ark:/#{shoulder}#{blade}", headers: {}) | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include EzidStubbing | ||
end | ||
Ezid::Client.configure do |conf| conf.logger = Logger.new(File::NULL); 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