Skip to content

Commit

Permalink
Adding IIIF resource and ingest job
Browse files Browse the repository at this point in the history
  • Loading branch information
escowles committed Jan 13, 2016
1 parent 9670db7 commit c3c8dee
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 10 deletions.
3 changes: 1 addition & 2 deletions app/jobs/iiif_ingest_job.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
class IIIFIngestJob < ActiveJob::Base

# Ingest one or more IIIF manfiest URLs. Each manifest is ingested as its
# own resource.
def perform(urls)
arr = urls.kind_of?(Array) ? urls : Array(urls)
arr = urls.is_a?(Array) ? urls : Array(urls)
arr.each do |url|
ingest url
end
Expand Down
38 changes: 30 additions & 8 deletions app/models/iiif_resource.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
class IIIFResource < Spotlight::Resource

# If a manifest_url if provided, it is retrieved, parsed and indexed
def initialize(manifest_url: nil)
manifest = manifest_url.blank? ? {} : parse_manifest(manifest_url)
data['manifest'] = manifest_url
data['label'] = manifest['label']
data['thubmnail'] = manifest['thumbnail']['@id'] if manifest['thumbnail']
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|
data[h['label'].parameterize('_')] = h['value'].map { |v| v["@value"] }
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 parse_manifest(manifest_url)
def self.parse_manifest(manifest_url)
conn = Faraday.new(manifest_url) do |builder|
builder.request :json
builder.headers['Content-Type'] = 'application/json'
end
JSON.parse conn.get(manifest_url)
end
Expand Down
22 changes: 22 additions & 0 deletions spec/jobs/iiif_ingest_job_spec.rb
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
61 changes: 61 additions & 0 deletions spec/models/iiif_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'rails_helper'

describe IIIFResource 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
allow_any_instance_of(Faraday::Connection).to receive(:get).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

0 comments on commit c3c8dee

Please sign in to comment.