-
Notifications
You must be signed in to change notification settings - Fork 2
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 #263 from sul-dlss/metadata-service
Move MetadataService from dor-services-app
- Loading branch information
Showing
10 changed files
with
217 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class CatalogHandler | ||
def fetch(prefix, identifier) | ||
client = RestClient::Resource.new(Dor::Config.metadata.catalog.url, | ||
Dor::Config.metadata.catalog.user, | ||
Dor::Config.metadata.catalog.pass) | ||
params = "?#{prefix.chomp}=#{identifier.chomp}" | ||
client[params].get | ||
rescue RestClient::Exception => e | ||
raise BadResponseFromCatalog, "#{e.class} - when contacting (with BasicAuth hidden): #{Dor::Config.metadata.catalog.url}#{params}" | ||
end | ||
|
||
def label(metadata) | ||
mods = Nokogiri::XML(metadata) | ||
mods.root.add_namespace_definition('mods', 'http://www.loc.gov/mods/v3') | ||
mods.xpath('/mods:mods/mods:titleInfo[1]').xpath('mods:title|mods:nonSort').collect(&:text).join(' ').strip | ||
end | ||
|
||
def prefixes | ||
%w[catkey barcode] | ||
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,60 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cache' | ||
class MetadataError < RuntimeError; end | ||
|
||
class MetadataService | ||
class << self | ||
@@cache = Cache.new(nil, nil, 250, 300) | ||
|
||
def known_prefixes | ||
handlers.keys | ||
end | ||
|
||
def can_resolve?(identifier) | ||
(prefix, _identifier) = identifier.split(/:/, 2) | ||
handlers.key?(prefix.to_sym) | ||
end | ||
|
||
# TODO: Return a prioritized list | ||
def resolvable(identifiers) | ||
identifiers.select { |identifier| can_resolve?(identifier) } | ||
end | ||
|
||
def fetch(identifier) | ||
@@cache.fetch(identifier) do | ||
(prefix, identifier) = identifier.split(/:/, 2) | ||
handler = handler_for(prefix) | ||
handler.fetch(prefix, identifier) | ||
end | ||
end | ||
|
||
def label_for(identifier) | ||
(prefix, identifier) = identifier.split(/:/, 2) | ||
handler = handler_for(prefix) | ||
handler.label(handler.fetch(prefix, identifier)) | ||
end | ||
|
||
def handler_for(prefix) | ||
handler = handlers[prefix.to_sym] | ||
raise MetadataError, "Unknown metadata prefix: #{prefix}" if handler.nil? | ||
|
||
handler | ||
end | ||
|
||
private | ||
|
||
def handlers | ||
@handlers ||= {}.tap do |md_handlers| | ||
# There's only one. If additional handlers are added, will need to be registered here. | ||
register(CatalogHandler.new, md_handlers) | ||
end | ||
end | ||
|
||
def register(handler, md_handlers) | ||
handler.prefixes.each do |prefix| | ||
md_handlers[prefix.to_sym] = handler | ||
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
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,69 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://www.loc.gov/mods/v3" | ||
version="3.3" | ||
xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-3.xsd"> | ||
<titleInfo> | ||
<nonSort>The</nonSort> | ||
<title>isomorphism and thermal properties of the feldspars</title> | ||
</titleInfo> | ||
<name type="personal"> | ||
<namePart>Day, Arthur L. (Arthur Louis)</namePart> | ||
<namePart type="date">1869-</namePart> | ||
<role> | ||
<roleTerm authority="marcrelator" type="text">creator</roleTerm> | ||
</role> | ||
</name> | ||
<name type="personal"> | ||
<namePart>Allen, Eugene Thomas</namePart> | ||
<namePart type="date">1864-</namePart> | ||
</name> | ||
<name type="personal"> | ||
<namePart>Iddings, Joseph Paxson</namePart> | ||
<namePart type="date">1857-1920</namePart> | ||
</name> | ||
<name type="personal"> | ||
<namePart>Becker, George F. (George Ferdinand)</namePart> | ||
<namePart type="date">1847-1919</namePart> | ||
</name> | ||
<typeOfResource>text</typeOfResource> | ||
<originInfo> | ||
<place> | ||
<placeTerm type="code" authority="marccountry">dcu</placeTerm> | ||
</place> | ||
<place> | ||
<placeTerm type="text">Washington, D.C</placeTerm> | ||
</place> | ||
<publisher>Carnegie Institution of Washington</publisher> | ||
<dateIssued>1905</dateIssued> | ||
<dateIssued encoding="marc" keyDate="yes">1905</dateIssued> | ||
<issuance>monographic</issuance> | ||
</originInfo> | ||
<language> | ||
<languageTerm authority="iso639-2b" type="code">eng</languageTerm> | ||
</language> | ||
<physicalDescription> | ||
<form authority="marcform">print</form> | ||
<extent>95 p. illus., XXVI (i.e. 27) pl. 27 cm.</extent> | ||
</physicalDescription> | ||
<note displayLabel="statement of responsibility">Part I- Thermal study [by] Arthur L. Day and E. T. Allen. Part II- Optical study [by] J. P. Iddings. With an introduction by George F. Becker.</note> | ||
<subject authority="lcsh"> | ||
<topic>Crystallography</topic> | ||
</subject> | ||
<subject authority="lcsh"> | ||
<topic>Feldspar</topic> | ||
</subject> | ||
<classification authority="lcc">QD931 .D27</classification> | ||
<relatedItem type="series"> | ||
<titleInfo> | ||
<title>Carnegie Institution of Washington publication ; 31</title> | ||
</titleInfo> | ||
</relatedItem> | ||
<identifier type="lccn">05032416</identifier> | ||
<recordInfo> | ||
<recordContentSource authority="marcorg">DLC</recordContentSource> | ||
<recordCreationDate encoding="marc">770104</recordCreationDate> | ||
<recordChangeDate encoding="iso8601">19900306063726.0</recordChangeDate> | ||
<recordIdentifier>a45873</recordIdentifier> | ||
</recordInfo> | ||
</mods> |
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe MetadataService do | ||
before do | ||
@specdir = File.join(File.dirname(__FILE__), '..') | ||
end | ||
|
||
it 'raises an exception if an unknown metadata type is requested' do | ||
expect { described_class.fetch('foo:bar') }.to raise_exception(MetadataError) | ||
end | ||
|
||
describe 'Symphony handler' do | ||
before do | ||
@mods = File.read(File.join(@specdir, 'fixtures', 'mods_record.xml')) | ||
@mock_resource = double('catalog-resource', get: @mods) | ||
allow(@mock_resource).to receive(:[]).and_return(@mock_resource) | ||
expect(RestClient::Resource).to receive(:new).with(Dor::Config.metadata.catalog.url, | ||
Dor::Config.metadata.catalog.user, | ||
Dor::Config.metadata.catalog.pass).and_return(@mock_resource) | ||
end | ||
|
||
it 'fetches a record based on barcode' do | ||
expect(@mock_resource).to receive(:[]).with('?barcode=12345') | ||
expect(described_class.fetch('barcode:12345')).to be_equivalent_to(@mods) | ||
end | ||
|
||
it 'fetches a record based on catkey' do | ||
expect(@mock_resource).to receive(:[]).with('?catkey=12345') | ||
expect(described_class.fetch('catkey:12345')).to be_equivalent_to(@mods) | ||
end | ||
|
||
it 'returns the MODS title as the label' do | ||
expect(@mock_resource).to receive(:[]).with('?barcode=12345') | ||
expect(described_class.label_for('barcode:12345')).to eq('The isomorphism and thermal properties of the feldspars') | ||
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