-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
260 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class IIIFIngestJob < ActiveJob::Base | ||
# Ingest one or more IIIF manfiest URLs. Each manifest is ingested as its | ||
# own resource. | ||
def perform(urls) | ||
arr = urls.is_a?(Array) ? urls : Array(urls) | ||
arr.each do |url| | ||
ingest url | ||
end | ||
end | ||
|
||
# Ingest a single IIIF manifest URL as a resource. | ||
def ingest(url) | ||
IIIFResource.new(manifest_url: url).save | ||
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,42 @@ | ||
class IIIFResource < Spotlight::Resource | ||
# If a manifest_url if provided, it is retrieved, parsed and indexed | ||
def initialize(manifest_url: nil) | ||
super() | ||
return if manifest_url.blank? | ||
manifest = IIIFResource.parse_manifest(manifest_url) | ||
@title = manifest['label'] | ||
self.url = manifest_url | ||
|
||
self.data ||= {} | ||
self.data['thumbnail'] = manifest['thumbnail']['@id'] if manifest['thumbnail'] | ||
manifest['metadata'].each do |h| | ||
self.data[h['label'].parameterize('_')] = h['value'].map { |v| v["@value"] } | ||
end | ||
end | ||
|
||
def title_field | ||
:"#{solr_fields.prefix}spotlight_title#{solr_fields.string_suffix}" | ||
end | ||
|
||
def to_solr | ||
solr_doc = super | ||
solr_doc[title_field] = @title | ||
|
||
data.each do |k, v| | ||
solr_doc[(k + solr_fields.string_suffix).to_sym] = v | ||
end | ||
|
||
solr_doc | ||
end | ||
|
||
def solr_fields | ||
Spotlight::Engine.config.solr_fields | ||
end | ||
|
||
# Retrieve a IIIF manifest and parse the resulting JSON | ||
def self.parse_manifest(manifest_url) | ||
conn = Faraday.new(manifest_url) | ||
conn.headers['Accept'] = 'application/json' | ||
JSON.parse conn.get(manifest_url).body | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,22 @@ | ||
require 'rails_helper' | ||
|
||
describe IIIFIngestJob do | ||
let(:url1) { 'http://example.com/1/manifest' } | ||
let(:url2) { 'http://example.com/2/manifest' } | ||
let(:resource) { IIIFResource.new } | ||
|
||
it 'ingests a single url' do | ||
allow_any_instance_of(IIIFResource).to receive(:save) | ||
expect(IIIFResource).to receive(:new).with(manifest_url: url1).and_return(resource) | ||
|
||
described_class.new.perform(url1) | ||
end | ||
|
||
it 'ingests each of an array of urls' do | ||
allow_any_instance_of(IIIFResource).to receive(:save) | ||
expect(IIIFResource).to receive(:new).with(manifest_url: url1).and_return(resource) | ||
expect(IIIFResource).to receive(:new).with(manifest_url: url2).and_return(resource) | ||
|
||
described_class.new.perform([url1, url2]) | ||
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,79 @@ | ||
require 'rails_helper' | ||
|
||
describe IIIFResource do | ||
context 'with mock http interactions' do | ||
let(:url) { 'http://example.com/1/manifest' } | ||
let(:json) { '{ | ||
"@context":"http://iiif.io/api/presentation/2/context.json", | ||
"@id":"http://example.com/1/manifest", | ||
"@type":"sc:Manifest", | ||
"label":"Sample Manifest", | ||
"thumbnail":{ | ||
"@id":"http://example.com/loris/1.jp2/full/100,/0/default.jpg", | ||
"service":{ | ||
"@context":"http://iiif.io/api/image/2/context.json", | ||
"@id":"https://example.com/loris/1.jp2", | ||
"profile":"http://iiif.io/api/image/2/level2.json" }}, | ||
"metadata":[ | ||
{ "label": "Creator", "value": [{ "@value": "Author, Alice, 1954-" }] }, | ||
{ "label": "Date created", "value": [{ "@value": "1985" }] } | ||
]}' | ||
} | ||
|
||
before do | ||
resp = double('response') | ||
allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(resp) | ||
allow(resp).to receive(:body).and_return(json) | ||
end | ||
|
||
describe '#initialize' do | ||
it 'loads metadata from the IIIF manifest' do | ||
resource = described_class.new(manifest_url: url) | ||
expect(resource.url).to eq(url) | ||
expect(resource.data['thumbnail']).to eq('http://example.com/loris/1.jp2/full/100,/0/default.jpg') | ||
expect(resource.data['creator']).to eq(['Author, Alice, 1954-']) | ||
end | ||
end | ||
|
||
describe '#parse_manifest' do | ||
it 'retrieves and parses an IIIF manifest' do | ||
expect_any_instance_of(Faraday::Connection).to receive(:get).with(url) | ||
manifest = described_class.parse_manifest(url) | ||
expect(manifest['@id']).to eq(url) | ||
expect(manifest['label']).to eq('Sample Manifest') | ||
expect(manifest['thumbnail']['@id']).to eq('http://example.com/loris/1.jp2/full/100,/0/default.jpg') | ||
end | ||
end | ||
|
||
describe '#to_solr' do | ||
subject { described_class.new(manifest_url: url) } | ||
before do | ||
exhibit = Spotlight::Exhibit.new | ||
allow(exhibit).to receive(:blacklight_config).and_return(Blacklight::Configuration.new) | ||
subject.exhibit = exhibit | ||
end | ||
|
||
it 'indexes iiif metadata' do | ||
solr_doc = subject.to_solr | ||
expect(solr_doc[:spotlight_title_ssim]).to eq('Sample Manifest') | ||
expect(solr_doc[:thumbnail_ssim]).to eq('http://example.com/loris/1.jp2/full/100,/0/default.jpg') | ||
expect(solr_doc[:creator_ssim]).to eq(['Author, Alice, 1954-']) | ||
expect(solr_doc[:date_created_ssim]).to eq(['1985']) | ||
end | ||
end | ||
end | ||
|
||
context 'with recorded http interactions', vcr: { cassette_name: 'iiif_manifest' } do | ||
let(:url) { 'https://hydra-dev.princeton.edu/concern/scanned_resources/1r66j1149/manifest' } | ||
it 'ingests a iiif manifest' do | ||
exhibit = Spotlight::Exhibit.create title: 'Exhibit A' | ||
resource = IIIFResource.new manifest_url: url | ||
resource.exhibit = exhibit | ||
expect(resource.save).to be true | ||
|
||
reloaded = IIIFResource.last | ||
expect(reloaded.url).to eq url | ||
expect(reloaded.data['date_created']).to eq ['1976'] | ||
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,9 @@ | ||
require 'rails_helper' | ||
|
||
describe User do | ||
let(:email) { 'user@example.com' } | ||
subject { User.new email: email } | ||
it 'has an email address' do | ||
expect(subject.to_s).to eq email | ||
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,8 @@ | ||
require 'vcr' | ||
|
||
VCR.configure do |c| | ||
c.cassette_library_dir = 'spec/cassettes' | ||
c.hook_into :webmock | ||
c.ignore_localhost = true | ||
c.configure_rspec_metadata! | ||
end |