-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
app/models/blacklight_iiif_search/iiif_search_response.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# frozen_string_literal: true | ||
|
||
# corresponds to a IIIF Annotation List | ||
# LOCAL OVERRIDE to modify logic in #resources method | ||
# remove when https://github.com/boston-library/blacklight_iiif_search/issues/6 solved | ||
module BlacklightIiifSearch | ||
class IiifSearchResponse | ||
include BlacklightIiifSearch::Ignored | ||
|
||
attr_reader :solr_response, :controller, :iiif_config | ||
|
||
## | ||
# @param [Blacklight::Solr::Response] solr_response | ||
# @param [SolrDocument] parent_document | ||
# @param [CatalogController] controller | ||
def initialize(solr_response, parent_document, controller) | ||
@solr_response = solr_response | ||
@parent_document = parent_document | ||
@controller = controller | ||
@iiif_config = controller.iiif_search_config | ||
@resources = [] | ||
@hits = [] | ||
end | ||
|
||
## | ||
# constructs the IIIF::Presentation::AnnotationList | ||
# @return [IIIF::OrderedHash] | ||
def annotation_list | ||
list_id = controller.request.original_url | ||
anno_list = IIIF::Presentation::AnnotationList.new('@id' => list_id) | ||
anno_list['@context'] = %w( | ||
http://iiif.io/api/presentation/2/context.json | ||
http://iiif.io/api/search/1/context.json | ||
) | ||
anno_list['resources'] = resources | ||
anno_list['hits'] = @hits | ||
anno_list['within'] = within | ||
anno_list['prev'] = paged_url(solr_response.prev_page) if solr_response.prev_page | ||
anno_list['next'] = paged_url(solr_response.next_page) if solr_response.next_page | ||
anno_list['startIndex'] = 0 unless solr_response.total_pages > 1 | ||
anno_list.to_ordered_hash(force: true, include_context: false) | ||
end | ||
|
||
## | ||
# Return an array of IiifSearchAnnotation objects | ||
# @return [Array] | ||
def resources | ||
@total = 0 | ||
solr_response['highlighting'].each do |id, hl_hash| | ||
hit = { '@type': 'search:Hit', 'annotations': [] } | ||
document = solr_response.documents.find { |v| v[:id] == id } | ||
if hl_hash.empty? | ||
@total += 1 | ||
annotation = IiifSearchAnnotation.new(document, | ||
solr_response.params['q'], | ||
0, nil, controller, | ||
@parent_document) | ||
@resources << annotation.as_hash | ||
hit[:annotations] << annotation.annotation_id | ||
else | ||
hl_hash.each_value do |hl_array| | ||
hl_array.each_with_index do |hl, hl_index| | ||
# iterate over highlighted segments, | ||
# using highlighted text as "query" passed to IiifSearchAnnotation.new | ||
# assume that <em></em> is the highlight tag | ||
hl.scan(/<em>[\S]*<\/em>/)&.map { |v| v.gsub(/<[\/]?em>/, '') }&.each do |hl_string| | ||
@total += 1 | ||
annotation = IiifSearchAnnotation.new(document, | ||
hl_string, | ||
hl_index, hl, controller, | ||
@parent_document) | ||
@resources << annotation.as_hash | ||
hit[:annotations] << annotation.annotation_id | ||
end | ||
end | ||
end | ||
end | ||
@hits << hit | ||
end | ||
@resources | ||
end | ||
|
||
## | ||
# @return [IIIF::Presentation::Layer] | ||
def within | ||
within_hash = IIIF::Presentation::Layer.new | ||
within_hash['ignored'] = ignored | ||
if solr_response.total_pages > 1 | ||
within_hash['first'] = paged_url(1) | ||
within_hash['last'] = paged_url(solr_response.total_pages) | ||
else | ||
within_hash['total'] = @total | ||
end | ||
within_hash | ||
end | ||
|
||
## | ||
# create a URL for the previous/next page of results | ||
# @return [String] | ||
def paged_url(page_index) | ||
controller.solr_document_iiif_search_url(clean_params.merge(page: page_index)) | ||
end | ||
|
||
## | ||
# remove ignored or irrelevant params from the params hash | ||
# @return [ActionController::Parameters] | ||
def clean_params | ||
remove = ignored.map(&:to_sym) | ||
controller.iiif_search_params.except(*%i(page solr_document_id) + remove) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters