Skip to content

Commit

Permalink
Implementing IIIFResource#validate_iiif_manifest_collections! for val…
Browse files Browse the repository at this point in the history
…idating Collection metadata
  • Loading branch information
jrgriffiniii committed Aug 14, 2018
1 parent f14b6c0 commit e9daf42
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 68 deletions.
1 change: 0 additions & 1 deletion app/models/exhibit_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def initialize(exhibit)
end

def reindex(*_args)
binding.pry
members.each_slice(50) do |slice|
IIIFIngestJob.perform_later slice, exhibit
end
Expand Down
23 changes: 22 additions & 1 deletion app/models/iiif_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,29 @@ class IIIFResource < Spotlight::Resources::IiifHarvester
after_destroy :cleanup_solr
before_save :set_noid

class InvalidIIIFManifestError < TypeError; end

def iiif_manifests
@iiif_manifests ||= ::IiifService.parse(url)
@iiif_manifests ||= validate_iiif_manifest_collections!
end

def validate_iiif_manifest_collections!
iiif_manifests = ::IiifService.parse(url)

# Check for valid metadata
iiif_manifests.to_a.each do |iiif_manifest|
iiif_manifest.with_exhibit(exhibit)

next unless iiif_manifest.as_json.key?("manifest") &&
iiif_manifest.as_json["manifest"].key?("data") &&
iiif_manifest.as_json["manifest"]["data"].key?("metadata")

collection_metadata = iiif_manifest.as_json["manifest"]["data"]["metadata"].select { |metadata| metadata["label"] == "Collections" }
invalid_collection_metadata = collection_metadata.select { |metadata| metadata["value"].first.is_a?(Hash) }
raise(InvalidIIIFManifestError, "Invalid Collection metadata found in the IIIF Manifest: #{url}") unless invalid_collection_metadata.empty?
end

@iiif_manifests = iiif_manifests
end

def cleanup_solr
Expand Down
135 changes: 69 additions & 66 deletions spec/models/iiif_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,8 @@
"@id": "https://hydra-dev.princeton.edu/users/auth/cas",
"label": "Login to Figgy using CAS",
"profile": "http://iiif.io/api/auth/0/login",
"service": {"hsh": nil}
"service": { "hsh": nil }
},
"structures": [
{
"@id":"https://hydra-dev.princeton.edu/concern/scanned_resources/1r66j1149/manifest/range/g70272255683880",
"@type":"sc:Range",
"label":"Logical",
"viewingHint":"top"
}
],
"structures": [
{
"@id": "https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest/range/g70237482003920",
Expand All @@ -104,52 +96,59 @@
"@id": "https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest/range/g70237482056940",
"@type": "sc:Range",
"label": "Chapter 1",
"canvases":[
"canvases": [
"https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest/canvas/sbn9996723",
"https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest/canvas/s9k41zd485"
]
},
{
"@id":"https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest/range/g70237482036180",
"@type":"sc:Range",
"label":"Chapter 2",
"canvases":[
"@id": "https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest/range/g70237482036180",
"@type": "sc:Range",
"label": "Chapter 2",
"canvases": [
"https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest/canvas/sww72bb48n",
"https://hydra-dev.princeton.edu/concern/scanned_resources/s9w032300r/manifest/canvas/sjs956f80z"
]
}
]
}
],
"metadata":[
{"label":"Date created","value":[{"@value":"1976"}]}
"metadata": [
{
"label": "Date created",
"value": [{ "@value": "1976" }]
},
{
"label": "Collections",
"value": [{ "id": { "id": "058c1862-30dc-431c-90b5-4e141282c7a1" }, "internal_resource": "Collection", "created_at": "11/29/17 12:46:50 PM UTC" }]
}
],
"seeAlso":[
{"@id": "https://bibdata.princeton.edu/bibliographic/1234567/jsonld","format": "application/ld+json"},
{"@id": "bla", "format":"text/xml"}
"seeAlso": [
{ "@id": "https://bibdata.princeton.edu/bibliographic/1234567/jsonld", "format": "application/ld+json" },
{ "@id": "bla", "format": "text/xml" }
],
"rendering": {"@id":"http://arks.princeton.edu/ark:88435/7w62fb79g", "format": "text/html"}
"rendering": { "@id": "http://arks.princeton.edu/ark:88435/7w62fb79g", "format": "text/html" }
}
end

let(:collection_manifest) do
{
"@id": "https://localhost.localdomain/collections/2b88qc199/manifest",
"@type":"sc:Collection",
"label":"princeton",
"viewingHint":"individuals",
"structures":[
"@type": "sc:Collection",
"label": "princeton",
"viewingHint": "individuals",
"structures": [
{
"@id":"https://localhost.localdomain/collections/2b88qc199/manifest/range/g70272079000020",
"@type":"sc:Range",
"label":"Logical",
"viewingHint":"top"
"@id": "https://localhost.localdomain/collections/2b88qc199/manifest/range/g70272079000020",
"@type": "sc:Range",
"label": "Logical",
"viewingHint": "top"
}
],
"metadata":[
"metadata": [
{
"label":"Exhibit",
"value":["princeton-best"]
"label": "Exhibit",
"value": ["princeton-best"]
}
],
"manifests": [
Expand All @@ -166,38 +165,38 @@

let(:collections_manifest) do
{
"@context":"http://iiif.io/api/presentation/2/context.json",
"@id":"https://localhost.localdomain/collections/manifest",
"@type":"sc:Collection",
"label":"Figgy Collections",
"description":"All collections which are a part of Figgy.",
"viewingHint":"individuals",
"structures":[
"@context": "http://iiif.io/api/presentation/2/context.json",
"@id": "https://localhost.localdomain/collections/manifest",
"@type": "sc:Collection",
"label": "Figgy Collections",
"description": "All collections which are a part of Figgy.",
"viewingHint": "individuals",
"structures": [
{
"@id":"https://localhost.localdomain/collections/manifest/range/g70272257673280",
"@type":"sc:Range",
"label":"Logical",
"viewingHint":"top"
"@id": "https://localhost.localdomain/collections/manifest/range/g70272257673280",
"@type": "sc:Range",
"label": "Logical",
"viewingHint": "top"
}
],
"collections":[
"collections": [
{
"@id":"https://localhost.localdomain/collections/2b88qc199/manifest",
"@type":"sc:Collection",
"label":"princeton",
"viewingHint":"individuals",
"structures":[
"@id": "https://localhost.localdomain/collections/2b88qc199/manifest",
"@type": "sc:Collection",
"label": "princeton",
"viewingHint": "individuals",
"structures": [
{
"@id":"https://localhost.localdomain/collections/2b88qc199/manifest/range/g70272079000020",
"@type":"sc:Range",
"label":"Logical",
"viewingHint":"top"
"@id": "https://localhost.localdomain/collections/2b88qc199/manifest/range/g70272079000020",
"@type": "sc:Range",
"label": "Logical",
"viewingHint": "top"
}
],
"metadata":[
"metadata": [
{
"label":"Exhibit",
"value":["princeton-best"]
"label": "Exhibit",
"value": ["princeton-best"]
}
]
}
Expand All @@ -206,22 +205,26 @@
end

before do
stub_request(:get, "https://localhost.localdomain/scanned_resources/1r66j1149/manifest"
stub_request(
:get,
"https://localhost.localdomain/scanned_resources/1r66j1149/manifest"
).to_return(body: JSON.generate(scanned_resource_manifest))
stub_request(:get, "https://localhost.localdomain/collections/2b88qc199/manifest"
stub_request(
:get,
"https://localhost.localdomain/collections/2b88qc199/manifest"
).to_return(body: JSON.generate(collection_manifest))
stub_request(:get, url
stub_request(
:get,
url
).to_return(body: JSON.generate(collections_manifest))
stub_request(:head, url
).to_return(headers: { 'Content-Type' => 'application/json; charset=utf-8' })
stub_request(
:head,
url
).to_return(headers: { 'Content-Type' => 'application/json; charset=utf-8' })
end

it "does not index collections" do
expect(resource.save).to be true

solr_doc = nil
resource.document_builder.to_solr { |x| solr_doc = x }
expect(solr_doc["readonly_collections_tesim"]).to be_empty
it "raises an error" do
expect { resource.save }.to raise_error(IIIFResource::InvalidIIIFManifestError, "Invalid Collection metadata found in the IIIF Manifest: #{url}")
end
end
end
Expand Down

0 comments on commit e9daf42

Please sign in to comment.