Skip to content

Commit

Permalink
Avoid null values in manifest for width/height/duration when not pres…
Browse files Browse the repository at this point in the history
…ent (#59)

Delegate #key? to appease rubocop

Co-authored-by: Dananji Withana <dwithana@iu.edu>
Co-authored-by: Michael Johnson <mj82@iu.edu>
  • Loading branch information
3 people authored Apr 7, 2022
1 parent ee11332 commit 62cc688
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/iiif_manifest/manifest_builder/iiif_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize
@inner_hash = initial_attributes
end

delegate :[]=, :[], :as_json, :to_json, :has_key?, to: :inner_hash
delegate :[]=, :[], :as_json, :to_json, :has_key?, :key?, to: :inner_hash

def initial_attributes
{}
Expand Down
6 changes: 3 additions & 3 deletions lib/iiif_manifest/v3/manifest_builder/content_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def initialize(display_content, iiif_annotation_factory:, body_builder_factory:)

def apply(canvas)
annotation['target'] = canvas['id']
canvas['width'] = annotation.body['width']
canvas['height'] = annotation.body['height']
canvas['duration'] = annotation.body['duration']
canvas['width'] = annotation.body['width'] if annotation.body['width'].present?
canvas['height'] = annotation.body['height'] if annotation.body['height'].present?
canvas['duration'] = annotation.body['duration'] if annotation.body['duration'].present?
# Assume first item in canvas is an annotation page
canvas.items.first.items += [annotation]
end
Expand Down
70 changes: 70 additions & 0 deletions spec/lib/iiif_manifest/v3/manifest_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def ranges
end
end

class AudioBook < Book
end

class ManifestRange
attr_reader :label, :ranges, :file_set_presenters
def initialize(label:, ranges: [], file_set_presenters: [])
Expand All @@ -67,10 +70,28 @@ def display_image
IIIFManifest::DisplayImage.new(id, width: 100, height: 100, format: 'image/jpeg')
end
end

class AudioFilePresenter
attr_reader :id, :label
def initialize(id: 'test-22', label: 'Disc 1')
@id = id
@label = label
end

def to_s
label
end

def display_content
IIIFManifest::V3::DisplayContent.new(id, type: 'Sound', duration: 360_000, format: 'audio/mp4')
end
end
end

after do
Object.send(:remove_const, :DisplayImagePresenter)
Object.send(:remove_const, :AudioFilePresenter)
Object.send(:remove_const, :AudioBook)
Object.send(:remove_const, :Book)
end

Expand Down Expand Up @@ -144,6 +165,36 @@ def display_image
expect(sub_range['items'][0]['type']).to eq 'Canvas'
expect(sub_range['items'][0]['id']).to eq 'http://test.host/books/book-77/manifest/canvas/test-22'
end

context 'with audio file' do
let(:presenter_class) { AudioBook }
let(:file_presenter) { AudioFilePresenter.new }

it 'returns items' do
allow(book_presenter).to receive(:file_set_presenters).and_return([file_presenter])

result

expect(result['items'].length).to eq 1
expect(result['items'].first['type']).to eq 'Canvas'
expect(result['items'].first['id']).to eq 'http://test.host/books/book-77/manifest/canvas/test-22'
expect(result['items'].first.key?('height')).to eq false
expect(result['items'].first.key?('width')).to eq false
expect(result['items'].first['duration']).to eq 360_000
expect(result['items'].first['items'].first['type']).to eq 'AnnotationPage'
expect(result['items'].first['items'].first['id']).not_to be_empty
expect(result['items'].first['items'].first['items'].length).to eq 1
expect(result['items'].first['items'].first['items'].first['type']).to eq 'Annotation'
expect(result['items'].first['items'].first['items'].first['motivation']).to eq 'painting'
expect(result['items'].first['items'].first['items'].first['target']).to eq result['items'].first['id']
expect(result['items'].first['items'].first['items'].first['body']['type']).to eq 'Sound'
expect(result['items'].first['items'].first['items'].first['body']['id']).not_to be_empty
expect(result['items'].first['items'].first['items'].first['body'].key?('height')).to eq false
expect(result['items'].first['items'].first['items'].first['body'].key?('width')).to eq false
expect(result['items'].first['items'].first['items'].first['body']['duration']).to eq 360_000
expect(result['items'].first['items'].first['items'].first['body']['format']).to eq 'audio/mp4'
end
end
end

context 'when there is no sequence_rendering method' do
Expand Down Expand Up @@ -454,6 +505,25 @@ def display_content
expect(content_annotation_body['duration']).to eq 100
expect(content_annotation_body['label']).to eq('@none' => ['High'])
end

context 'with audio file' do
let(:content) do
IIIFManifest::V3::DisplayContent.new(SecureRandom.uuid, duration: 100,
type: 'Sound',
format: 'audio/mp4',
label: 'High')
end

it 'returns items' do
expect(content_annotation_body['type']).to eq 'Sound'
expect(content_annotation_body['id']).not_to be_empty
expect(content_annotation_body.key?('height')).to eq false
expect(content_annotation_body.key?('width')).to eq false
expect(content_annotation_body['format']).to eq 'audio/mp4'
expect(content_annotation_body['duration']).to eq 100
expect(content_annotation_body['label']).to eq('@none' => ['High'])
end
end
end

context 'with multiple files' do
Expand Down

0 comments on commit 62cc688

Please sign in to comment.