Skip to content

Commit

Permalink
♻️ Refactor and fix annotation count
Browse files Browse the repository at this point in the history
This commit will refactor #annotation_list by breaking it down a bit and
also adjusts and compensates the json for removing the invalid hit.
  • Loading branch information
kirkkwang committed Jul 13, 2023
1 parent ec4b03c commit 2ff9965
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
30 changes: 25 additions & 5 deletions app/models/iiif_print/iiif_search_response_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@ module IiifSearchResponseDecorator
# @see https://github.com/scientist-softserv/louisville-hyku/commit/67467e5cf9fdb755f54419f17d3c24c87032d0af
def annotation_list
json_results = super
resources = json_results&.[]('resources')

resources.delete_if do |resource|
resource["on"].include?(IiifPrint::BlacklightIiifSearch::AnnotationDecorator::INVALID_MATCH_TEXT)
# Break down the json_results for easy access
resources = json_results['resources']
hits = json_results['hits']
within = json_results['within']

# Check and process invalid hit
remove_invalid_hit(resources, hits, within) if resources
add_metadata_match(resources)

json_results
end

def remove_invalid_hit(resources, hits, within)
invalid_hit = resources.detect { |resource| resource["on"].include?(IiifPrint::BlacklightIiifSearch::AnnotationDecorator::INVALID_MATCH_TEXT) }

if invalid_hit
# Delete invalid hit from resources, remove first hit (which is from the invalid hit), decrement total within
resources.delete(invalid_hit)
hits.shift
within['total'] -= 1
end
end

resources&.each do |result_hit|
def add_metadata_match(resources)
resources.each do |result_hit|
next if result_hit['resource'].present?

# Add resource details if not present
result_hit['resource'] = {
"@type": "cnt:ContentAsText",
"chars": "Metadata match, see sidebar for details"
}
end
json_results
end
end
end
15 changes: 13 additions & 2 deletions lib/iiif_print/blacklight_iiif_search/annotation_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module IiifPrint
module BlacklightIiifSearch
module AnnotationDecorator
INVALID_MATCH_TEXT = "xywh=INVALID,INVALID,INVALID,INVALID".freeze
INVALID_MATCH_TEXT = "#xywh=INVALID,INVALID,INVALID,INVALID".freeze
##
# Create a URL for the annotation
# use a Hyrax-y URL syntax:
Expand Down Expand Up @@ -30,7 +30,7 @@ def canvas_uri_for_annotation
def coordinates
return default_coords if query.blank?

sanitized_query = query.match(additional_query_terms_regex)[1].strip
sanitized_query = sanitize_query
coords_json = fetch_and_parse_coords

coords_check_result = check_coords_json_and_properties(coords_json, sanitized_query)
Expand All @@ -49,6 +49,10 @@ def coordinates
"#xywh=#{coords_array.join(',')}"
end

def sanitize_query
query.match(additional_query_terms_regex)[1].strip
end

##
# return the JSON word-coordinates file contents
# @return [JSON]
Expand Down Expand Up @@ -119,6 +123,13 @@ def file_set_id
def additional_query_terms_regex
/(.*)(?= AND (\(.+\)|\w+)$)/
end

##
# @return [IIIF::Presentation::Resource]
def text_resource_for_annotation
IIIF::Presentation::Resource.new('@type' => 'cnt:ContentAsText',
'chars' => sanitize_query)
end
end
end
end

0 comments on commit 2ff9965

Please sign in to comment.