Skip to content

Commit

Permalink
Implementing a Thor command for migrating ExhibitThumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgriffiniii committed Jun 22, 2018
1 parent 6c32413 commit 9266260
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@ Metrics/BlockLength:
Max: 31
Exclude:
- 'app/controllers/catalog_controller.rb'
- 'lib/commands/migrate.rb'
- 'migrate.thor'
- 'lib/tasks/dev.rake'
- 'spec/**/*'

Metrics/CyclomaticComplexity:
Exclude:
- 'lib/commands/migrate.rb'
- 'migrate.thor'

Metrics/PerceivedComplexity:
Exclude:
- 'lib/commands/migrate.rb'
- 'migrate.thor'

Style/BlockDelimiters:
Exclude:
- 'spec/**/*'
Expand Down Expand Up @@ -129,6 +141,14 @@ RSpec/ExampleWording:
IgnoredWords:
- only

RSpec/ExampleLength:
Exclude:
- 'spec/commands/migrate_spec.rb'
- 'spec/controllers/**/*'
- 'spec/models/**/*'
- 'spec/services/**/*'
- 'spec/workers/**/*'

RSpec/DescribeClass:
Exclude:
- 'spec/abilities/**/*'
Expand Down
68 changes: 68 additions & 0 deletions lib/commands/migrate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env ruby

require "thor"

class Migrate < Thor
desc "exhibit_thumbnails", "Migrate the IIIF Manifests to the latest IIIF image service points"
def exhibit_thumbnails
Spotlight::ExhibitThumbnail.all.each do |exhibit_thumbnail|
Rails.logger.debug "Retrieving the Solr Document for #{exhibit_thumbnail}..."
document = exhibit_thumbnail.document
next unless document && document["readonly_references_ssim"] && !document["readonly_references_ssim"].empty?

references_values = document["readonly_references_ssim"].first
references = JSON.parse(references_values)

next unless references["iiif_manifest_paths"]
iiif_manifest_url = references["iiif_manifest_paths"].values.first

Rails.logger.debug "Retrieving the IIIF Manifest for #{exhibit_thumbnail}..."
begin
response = Faraday.get(iiif_manifest_url)
unless response.success?
Rails.logger.warn("Failed to get #{iiif_manifest_url}")
next
end
rescue Faraday::Error::ConnectionFailed, Faraday::TimeoutError => error
Rails.logger.warn("Failed to get #{iiif_manifest_url}: #{error}")
next
end

iiif_manifest_values = response.body
iiif_manifest = JSON.parse(iiif_manifest_values)

sequence = iiif_manifest["sequences"].first

next if document["access_identifier_ssim"].blank?
access_identifier = document["access_identifier_ssim"].first

canvas = sequence["canvases"].find do |c|
c["local_identifier"] == access_identifier
end

next unless canvas
iiif_canvas_id = canvas["@id"]

images = canvas["images"]
image = images.first
resource = image["resource"]
service = resource["service"]

iiif_tilesource = service["@id"]

exhibit_thumbnail.iiif_manifest_url = iiif_manifest_url
exhibit_thumbnail.iiif_canvas_id = iiif_canvas_id
exhibit_thumbnail.iiif_tilesource = iiif_tilesource

Rails.logger.debug "Updating #{exhibit_thumbnail}..."
begin
exhibit_thumbnail.save
rescue StandardError => error
Rails.logger.warn "Failed to update ExhibitThumbnail for #{exhibit_thumbnail.iiif_manifest_url}: #{error}"
next
end
end
end
end

Migrate.start(ARGV)
1 change: 1 addition & 0 deletions migrate.thor
163 changes: 163 additions & 0 deletions spec/commands/migrate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
require 'rails_helper'
require Rails.root.join("lib", "commands", "migrate")

describe Migrate do
before do
WebMock.disable!
end

describe "exhibit_thumbnails" do
subject(:command) { described_class.new }

let(:document) do
{
"id" => "353205342bc9eb6a2330447a58ffadb2",
"readonly_references_ssim" => [
"{\"https://catalog.princeton.edu/catalog/5557079#view\":[\"Digital content below\"],\"iiif_manifest_paths\":{\"http://arks.princeton.edu/ark:/88435/bg257f113\":\"https://figgy.princeton.edu/concern/scanned_resources/d917e0d6-2894-4d1e-939b-93fdc0796c96/manifest\"}}"
],
"access_identifier_ssim" => [
"bg257f113"
]
}
end
let(:exhibit_thumbnail) { instance_double(Spotlight::ExhibitThumbnail) }
let(:exhibit_thumbnails) { [exhibit_thumbnail] }
let(:response) { instance_double(Faraday::Response) }
let(:manifest) do
{
sequences: [
{
"@type" => "sc:Sequence",
"@id" => "https://figgy.princeton.edu/concern/scanned_resources/fbc2d7b3-164b-4458-b2c3-9007c2ed08a5/manifest/sequence/normal",
canvases: [
{
"@type" => "sc:Canvas",
"@id" => "https://figgy.princeton.edu/concern/scanned_resources/fbc2d7b3-164b-4458-b2c3-9007c2ed08a5/manifest/canvas/ac8a822f-60db-4c97-a79e-2ed1b3676072",
label: "front cover",
local_identifier: "bg257f113",
rendering: [
{
"@id" => "https://figgy.princeton.edu/downloads/ac8a822f-60db-4c97-a79e-2ed1b3676072/file/d22f7d15-30ca-4f9c-88c3-987e54981840",
label: "Download the original file",
format: "image/tiff"
}
],
width: "4502",
height: "7200",
images: [
{
"@type" => "oa:Annotation",
motivation: "sc:painting",
resource: {
"@type" => "dctypes:Image",
"@id" => {
id: "ac8a822f-60db-4c97-a79e-2ed1b3676072"
},
height: "7200",
width: "4502",
format: "image/jpeg",
service: {
"@context": "http://iiif.io/api/image/2/context.json",
"@id" => "https://libimages1.princeton.edu/loris/figgy_prod/3f%2F26%2F5b%2F3f265b23fbce4298b251d84c19439396%2Fintermediate_file.jp2",
profile: "http://iiif.io/api/image/2/level2.json"
}
},
on: "https://figgy.princeton.edu/concern/scanned_resources/fbc2d7b3-164b-4458-b2c3-9007c2ed08a5/manifest/canvas/ac8a822f-60db-4c97-a79e-2ed1b3676072"
}
]
}
]
}
]
}
end

before do
allow(response).to receive(:body).and_return(manifest.to_json)

allow(exhibit_thumbnail).to receive(:document).and_return(document)
allow(exhibit_thumbnail).to receive(:iiif_manifest_url=)
allow(exhibit_thumbnail).to receive(:iiif_canvas_id=)
allow(exhibit_thumbnail).to receive(:iiif_tilesource=)
allow(exhibit_thumbnail).to receive(:save)
allow(Spotlight::ExhibitThumbnail).to receive(:all).and_return(exhibit_thumbnails)
end

it "migrates the Spotlight::ExhibitThumbnail IIIF Manifests" do
allow(response).to receive(:success?).and_return(true)
allow(Faraday).to receive(:get).and_return(response)

command.exhibit_thumbnails

expect(exhibit_thumbnail).to have_received(:iiif_manifest_url=).with("https://figgy.princeton.edu/concern/scanned_resources/d917e0d6-2894-4d1e-939b-93fdc0796c96/manifest")
expect(exhibit_thumbnail).to have_received(:iiif_canvas_id=).with("https://figgy.princeton.edu/concern/scanned_resources/fbc2d7b3-164b-4458-b2c3-9007c2ed08a5/manifest/canvas/ac8a822f-60db-4c97-a79e-2ed1b3676072")
expect(exhibit_thumbnail).to have_received(:iiif_tilesource=).with("https://libimages1.princeton.edu/loris/figgy_prod/3f%2F26%2F5b%2F3f265b23fbce4298b251d84c19439396%2Fintermediate_file.jp2")

expect(exhibit_thumbnail).to have_received(:save)
end

context "when the request for the IIIF Manifest results in a server-side error" do
let(:logger) { instance_double(ActiveSupport::Logger) }

before do
allow(logger).to receive(:debug)
allow(logger).to receive(:warn)
allow(Rails).to receive(:logger).and_return(logger)
end

it "does not migrate the Spotlight::ExhibitThumbnail and logs a warning" do
allow(response).to receive(:success?).and_return(false)
allow(Faraday).to receive(:get).and_return(response)

command.exhibit_thumbnails

expect(exhibit_thumbnail).not_to have_received(:save)
expect(logger).to have_received(:warn).with("Failed to get https://figgy.princeton.edu/concern/scanned_resources/d917e0d6-2894-4d1e-939b-93fdc0796c96/manifest")
end
end

context "when the request for the IIIF Manifest fails" do
let(:logger) { instance_double(ActiveSupport::Logger) }

before do
allow(Faraday).to receive(:get).and_raise(Faraday::Error::ConnectionFailed.new("Connection failure"))
allow(logger).to receive(:debug)
allow(logger).to receive(:warn)
allow(Rails).to receive(:logger).and_return(logger)
end

it "does not migrate the Spotlight::ExhibitThumbnail and logs a warning" do
command.exhibit_thumbnails

expect(exhibit_thumbnail).not_to have_received(:save)
expect(logger).to have_received(:warn).with("Failed to get https://figgy.princeton.edu/concern/scanned_resources/d917e0d6-2894-4d1e-939b-93fdc0796c96/manifest: Connection failure")
end
end

context "when saving the Spotlight::ExhibitThumbnail fails" do
let(:logger) { instance_double(ActiveSupport::Logger) }

before do
allow(response).to receive(:success?).and_return(true)
allow(Faraday).to receive(:get).and_return(response)

allow(logger).to receive(:debug)
allow(logger).to receive(:warn)
allow(Rails).to receive(:logger).and_return(logger)

allow(exhibit_thumbnail).to receive(:iiif_manifest_url).and_return("https://figgy.princeton.edu/concern/scanned_resources/d917e0d6-2894-4d1e-939b-93fdc0796c96/manifest")
end

it "logs a warning" do
allow(exhibit_thumbnail).to receive(:save).and_raise(ActiveRecord::RecordInvalid)
command.exhibit_thumbnails

expect(logger).to have_received(:warn).with("Failed to update ExhibitThumbnail for https://figgy.princeton.edu/concern/scanned_resources/d917e0d6-2894-4d1e-939b-93fdc0796c96/manifest: Record invalid")
end
end
end

after do
WebMock.enable!
end
end

0 comments on commit 9266260

Please sign in to comment.