Skip to content

Commit

Permalink
Add test and refactor
Browse files Browse the repository at this point in the history
This commit will add a test for #display_image.  Also, a refactor was
introduced to account for both Hyrax 2 and 3.  The method signature
of the `Hyrax.config.iiif_image_url_builder` lambda changed to add an
additional argument in Hyrax 3.

ref: https://github.com/samvera/hyrax/blob/2.x-stable/lib/hyrax/configuration.rb#L397-L399
ref: https://github.com/samvera/hyrax/blob/3.x-stable/lib/hyrax/configuration.rb#L250-L252
  • Loading branch information
kirkkwang committed Mar 22, 2023
1 parent f83b632 commit 8bbbd27
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
22 changes: 15 additions & 7 deletions app/presenters/iiif_print/iiif_manifest_presenter_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def sequence_rendering

module DisplayImagePresenterBehavior
def display_image
# return nil unless model.image?
return nil unless latest_file_id

IIIFManifest::DisplayImage
Expand All @@ -41,11 +40,8 @@ def display_image

def display_image_url(base_url)
if ENV['SERVERLESS_IIIF_URL'].present?
Hyrax.config.iiif_image_url_builder.call(
latest_file_id,
ENV['SERVERLESS_IIIF_URL'],
Hyrax.config.iiif_image_size_default
).gsub(%r{images/}, '')
# At the moment we are only concerned about Hyrax's default image url builder
iiif_image_url_builder(url_builder: Hyrax.config.iiif_image_url_builder)
else
super
end
Expand All @@ -72,7 +68,7 @@ def work?
false
end

private
private

def latest_file_id
if ENV['SERVERLESS_IIIF_URL'].present?
Expand All @@ -85,6 +81,18 @@ def latest_file_id
def serverless_latest_file_id
@latest_file_id ||= digest_sha1
end

def iiif_image_url_builder(url_builder:)
args = [
latest_file_id,
ENV['SERVERLESS_IIIF_URL'],
Hyrax.config.iiif_image_size_default
]
# In Hyrax 3, Hyrax.config.iiif_image_url_builder takes an additional argument
args << image_format(alpha_channels) if url_builder.arity == 4

url_builder.call(*args).gsub(%r{images/}, '')
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,33 @@
expect(presenter.search_service).to include("#{solr_document.id}/iiif_search")
end
end

# this method is inside of the DisplayImagePresenterBehavior module
describe '#display_image' do
let(:presenter) { Hyrax::IiifManifestPresenter::DisplayImagePresenter.new(solr_document) }
let(:id) { 'abc123' }

context 'when serverless_iiif is enabled' do
let(:url) { 'serverless_iiif_url' }

it 'renders a serverless url' do
allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with('SERVERLESS_IIIF_URL').and_return(url)
allow(presenter).to receive(:latest_file_id).and_return(id)
expect(presenter.display_image.iiif_endpoint.url).to eq "#{url}/#{id}"
expect(presenter.display_image.iiif_endpoint.profile).to eq "http://iiif.io/api/image/2/level2.json"
end
end

context 'when serverless_iiif is not enabled' do
let(:iiif_info_url_builder) { ->(file_id, base_url) { "#{base_url}/#{file_id}" } }

it 'does not render a serverless url' do
allow(presenter).to receive(:latest_file_id).and_return(id)
allow(Hyrax.config).to receive(:iiif_image_server?).and_return(true)
allow(Hyrax.config).to receive(:iiif_info_url_builder).and_return(iiif_info_url_builder)
expect(presenter.display_image.iiif_endpoint.url).to eq "localhost/#{id}"
end
end
end
end

0 comments on commit 8bbbd27

Please sign in to comment.