Skip to content

Commit

Permalink
Merge pull request #80 from samvera/remove-apply-thumbnail-to-manifes…
Browse files Browse the repository at this point in the history
…t-from-record-property-builder

remove apply thumbnail to manifest and fix specs
  • Loading branch information
kirkkwang authored Oct 21, 2022
2 parents d1418b4 + 113a1d9 commit 5f47141
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 49 deletions.
5 changes: 3 additions & 2 deletions lib/iiif_manifest/manifest_builder/canvas_builder.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module IIIFManifest
class ManifestBuilder
class CanvasBuilder
attr_reader :record, :parent, :iiif_canvas_factory, :image_builder
attr_reader :record, :parent, :manifest, :iiif_canvas_factory, :image_builder

def initialize(record, parent, iiif_canvas_factory:, image_builder:)
def initialize(record, parent, manifest, iiif_canvas_factory:, image_builder:)
@record = record
@parent = parent
@manifest = manifest
@iiif_canvas_factory = iiif_canvas_factory
@image_builder = image_builder
apply_record_properties
Expand Down
4 changes: 2 additions & 2 deletions lib/iiif_manifest/manifest_builder/canvas_builder_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ def initialize(composite_builder:, canvas_builder_factory:)
@canvas_builder_factory = canvas_builder_factory
end

def from(work)
def from(work, *manifest)
composite_builder.new(
*file_set_presenters(work).map do |presenter|
canvas_builder_factory.new(presenter, work)
canvas_builder_factory.new(presenter, work, manifest.first)
end
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/iiif_manifest/manifest_builder/structure_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def sub_ranges

def canvas_builders
@canvas_builders ||= file_set_presenters.map do |file_set_presenter|
canvas_builder_factory.new(file_set_presenter, parent)
canvas_builder_factory.new(file_set_presenter, parent, nil)
end
end

Expand Down
29 changes: 26 additions & 3 deletions lib/iiif_manifest/v3/manifest_builder/canvas_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ module IIIFManifest
module V3
class ManifestBuilder
class CanvasBuilder
attr_reader :record, :parent, :iiif_canvas_factory, :content_builder,
attr_reader :record, :parent, :manifest, :iiif_canvas_factory, :content_builder,
:choice_builder, :iiif_annotation_page_factory, :thumbnail_builder_factory

def initialize(record,
parent,
manifest,
iiif_canvas_factory:,
content_builder:,
choice_builder:,
iiif_annotation_page_factory:,
thumbnail_builder_factory:)
@record = record
@parent = parent
@manifest = manifest
@iiif_canvas_factory = iiif_canvas_factory
@content_builder = content_builder
@choice_builder = choice_builder
Expand Down Expand Up @@ -53,24 +55,45 @@ def display_content
Array.wrap(record.display_content) if record.respond_to?(:display_content) && record.display_content.present?
end

def manifest_can_have_thumbnail
manifest.respond_to?(:thumbnail)
end

def apply_record_properties
canvas['id'] = path
canvas.label = ManifestBuilder.language_map(record.to_s) if record.to_s.present?
annotation_page['id'] = "#{path}/annotation_page/#{annotation_page.index}"
canvas.items = [annotation_page]
apply_thumbnail_to(canvas)
apply_thumbnail_to(manifest, canvas)
end

def apply_thumbnail_to(canvas)
def apply_thumbnail_to(manifest, canvas)
return unless iiif_endpoint

if display_image
apply_manifest_thumbnail(manifest, display_image)
canvas.thumbnail = Array(thumbnail_builder_factory.new(display_image).build)
elsif display_content.try(:first)
apply_manifest_thumbnail(manifest, display_content.first)
canvas.thumbnail = Array(thumbnail_builder_factory.new(display_content.first).build)
end
end

def collection?
manifest.is_a? IIIFManifest::Collection
end

def apply_manifest_thumbnail(manifest, display_type)
return unless manifest_can_have_thumbnail

if collection?
# if manifest.thumbnail is nil, make it an Array, then add more thumbnails into it
(manifest.thumbnail ||= []) << Array(thumbnail_builder_factory.new(display_type).build)
else
manifest.thumbnail ||= Array(thumbnail_builder_factory.new(display_type).build)
end
end

def annotation_page
@annotation_page ||= iiif_annotation_page_factory.new
end
Expand Down
4 changes: 4 additions & 0 deletions lib/iiif_manifest/v3/manifest_builder/iiif_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def homepage=(homepage)
inner_hash['homepage'] = homepage
end

def thumbnail
inner_hash['thumbnail']
end

def thumbnail=(thumbnail)
inner_hash['thumbnail'] = thumbnail
end
Expand Down
31 changes: 3 additions & 28 deletions lib/iiif_manifest/v3/manifest_builder/record_property_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(record,
def apply(manifest)
setup_manifest_from_record(manifest, record)
# Build the items array
canvas_builder.apply(manifest.items)
canvas_builder(manifest).apply(manifest.items)
manifest
end

Expand All @@ -36,8 +36,8 @@ def populate_rendering

private

def canvas_builder
canvas_builder_factory.from(record)
def canvas_builder(manifest)
canvas_builder_factory.from(record, manifest)
end

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/MethodLength
Expand All @@ -56,7 +56,6 @@ def setup_manifest_from_record(manifest, record)
manifest.rendering = populate_rendering if populate_rendering.present?
homepage = ::IIIFManifest.config.manifest_value_for(record, property: :homepage)
manifest.homepage = homepage if homepage.present?
apply_thumbnail_to(manifest)
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/MethodLength

Expand Down Expand Up @@ -99,30 +98,6 @@ def transform_field(field)
metadata_field['value'] = ManifestBuilder.language_map(field['value'])
metadata_field
end

def apply_thumbnail_to(manifest)
return unless iiif_endpoint

if display_image
manifest.thumbnail = Array(thumbnail_builder_factory.new(display_image).build)
elsif display_content
manifest.thumbnail = Array(thumbnail_builder_factory.new(display_content).build)
end
end

def display_image
return @display_image if defined?(@display_image)
@display_image = record.try(:member_presenters)&.first&.display_image
end

def display_content
return @display_content if defined?(@display_content)
@display_content = record.try(:member_presenters)&.first&.display_content
end

def iiif_endpoint
display_image.try(:iiif_endpoint) || Array(display_content).first.try(:iiif_endpoint)
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/iiif_manifest/v3/manifest_builder/structure_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def build_range
def canvas_builders
@canvas_builders ||= [] unless record.respond_to?(:file_set_presenters)
@canvas_builders ||= file_set_presenters.map do |file_set_presenter|
canvas_builder_factory.new(file_set_presenter, parent)
canvas_builder_factory.new(file_set_presenter, parent, nil)
end
@canvas_builders
end
Expand Down Expand Up @@ -69,7 +69,7 @@ def range_items
end

def canvas_range_item(range_item)
canvas_builder = canvas_builder_factory.new(range_item, parent)
canvas_builder = canvas_builder_factory.new(range_item, parent, nil)
{ 'type' => 'Canvas', 'id' => canvas_builder.path }
end

Expand Down
17 changes: 17 additions & 0 deletions spec/lib/iiif_manifest/v3/manifest_builder/canvas_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
described_class.new(
record,
parent,
manifest,
iiif_canvas_factory: IIIFManifest::V3::ManifestBuilder::IIIFManifest::Canvas,
content_builder: content_builder,
choice_builder: IIIFManifest::V3::ManifestBuilder::ChoiceBuilder,
Expand Down Expand Up @@ -57,6 +58,7 @@ def id
allow(content_builder).to receive(:new).and_return(built_content)
allow(thumbnail_builder).to receive(:build).and_return(iiif_thumbnail)
allow(thumbnail_builder_factory).to receive(:new).and_return(thumbnail_builder)
# allow(manifest).to receive(:thumbnail).and_return(iiif_thumbnail)
end

let(:iiif_body) do
Expand Down Expand Up @@ -102,6 +104,10 @@ def id
double('Thumbnail Builder')
end

let(:manifest) do
IIIFManifest::V3::ManifestBuilder::IIIFManifest.new
end

let(:built_content) do
IIIFManifest::V3::ManifestBuilder::ContentBuilder.new(
record.display_content,
Expand Down Expand Up @@ -150,6 +156,17 @@ def id
expect(values).to include "height" => "100px"
expect(values).to include "duration" => nil
end

it 'sets the manifest thumbnail as well' do
builder.canvas
expect(manifest.thumbnail).to be_an Array
manifest_thumbnail = manifest.thumbnail.first
expect(manifest_thumbnail).to be_a IIIFManifest::V3::ManifestBuilder::IIIFManifest::Thumbnail
expect(manifest_thumbnail['type']).to eq 'Image'
expect(manifest_thumbnail['width']).to eq 200
expect(manifest_thumbnail['height']).to eq 150
expect(manifest_thumbnail['duration']).to be_nil
end
end

context 'when the display content is empty for an item' do
Expand Down
11 changes: 0 additions & 11 deletions spec/lib/iiif_manifest/v3/manifest_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,6 @@ def display_content
expect(result['items'].first['items'].first['items'].first['body']['format']).to eq 'image/jpeg'
end

it 'has a thumbnail property' do
allow(book_presenter).to receive(:member_presenters).and_return([file_presenter])

thumbnail = result['thumbnail'].first
expect(thumbnail['id']).to eq 'test.host/images/image-77/full/!200,200/0/default.jpg'
expect(thumbnail['height']).to eq 150
expect(thumbnail['width']).to eq 200
expect(thumbnail['format']).to eq 'image/jpeg'
expect(thumbnail['service'].first).to be_kind_of IIIFManifest::V3::ManifestBuilder::IIIFService
end

it 'builds a structure if it can' do
allow(book_presenter).to receive(:file_set_presenters).and_return([file_presenter])
allow(book_presenter.ranges[0].ranges[0]).to receive(:file_set_presenters).and_return([file_presenter])
Expand Down

0 comments on commit 5f47141

Please sign in to comment.