Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def load_gdt_results

# Handle errors
@errors = extract_errors(response)
@pagination = Analyzer.new(@enhanced_query, response).pagination if @errors.nil?
@results = extract_results(response)
return unless @errors.nil?

@pagination = Analyzer.new(@enhanced_query, response).pagination
raw_results = extract_results(response)
@results = NormalizeTimdexResults.new(raw_results, @enhanced_query[:q]).normalize
@filters = extract_filters(response)
end

Expand Down Expand Up @@ -77,8 +80,11 @@ def load_timdex_results
response = query_timdex(query)

@errors = extract_errors(response)
@pagination = Analyzer.new(@enhanced_query, response).pagination if @errors.nil?
@results = extract_results(response)
return unless @errors.nil?

@pagination = Analyzer.new(@enhanced_query, response).pagination
raw_results = extract_results(response)
@results = NormalizeTimdexResults.new(raw_results, @enhanced_query[:q]).normalize
end

def active_filters
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def format_highlight_label(field_name)
end

def view_online(result)
return unless result['sourceLink'].present?
return unless result['source_link'].present?

link_to 'View online', result['sourceLink'], class: 'button button-primary'
link_to 'View online', result['source_link'], class: 'button button-primary'
end

def view_record(record_id)
Expand Down
12 changes: 7 additions & 5 deletions app/models/normalize_primo_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Transforms a PNX doc from Primo Search API into a normalized record.
# Transforms a Primo Search API result into a normalized record.
class NormalizePrimoRecord
def initialize(record, query)
@record = record
Expand All @@ -7,22 +7,24 @@ def initialize(record, query)

def normalize
{
# Core fields
'title' => title,
'creators' => creators,
'source' => source,
'year' => year,
'format' => format,
'links' => links,
'citation' => citation,
'container' => container_title,
'identifier' => record_id,
'summary' => summary,
'numbering' => numbering,
'chapter_numbering' => chapter_numbering,
'thumbnail' => thumbnail,
'publisher' => publisher,
'location' => best_location,
'subjects' => subjects,
# Primo-specific fields
'container' => container_title,
'numbering' => numbering,
'chapter_numbering' => chapter_numbering,
'thumbnail' => thumbnail,
'availability' => best_availability,
'other_availability' => other_availability?
}
Expand Down
142 changes: 142 additions & 0 deletions app/models/normalize_timdex_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Transforms a TIMDEX result into a normalized record.
class NormalizeTimdexRecord
def initialize(record, query)
@record = record
@query = query
end

def normalize
{
# Core fields
'title' => title,
'creators' => creators,
'source' => source,
'year' => year,
'format' => format,
'links' => links,
'citation' => citation,
'identifier' => identifier,
'summary' => summary,
'publisher' => publisher,
'location' => location,
'subjects' => subjects,
# TIMDEX-specific fields
'content_type' => content_type,
'dates' => dates,
'contributors' => contributors,
'highlight' => highlight,
'source_link' => source_link
}
end

private

def title
@record['title'] || 'Unknown title'
end

def creators
return [] unless @record['contributors']

# Convert TIMDEX contributors to Primo-style creators format
@record['contributors']
.select { |c| %w[Creator Author].include?(c['kind']) }
.map { |creator| { 'value' => creator['value'], 'link' => nil } }
end

def source
return 'Unknown source' unless @record['source']

@record['source']
end

def year
# Extract year from dates
return nil unless @record['dates']

pub_date = @record['dates'].find { |date| date['kind'] == 'Publication date' }
return pub_date['value']&.match(/\d{4}/)&.to_s if pub_date

# Fallback to any date with a year
@record['dates'].each do |date|
year_match = date['value']&.match(/\d{4}/)
return year_match.to_s if year_match
end
end

def format
return '' unless @record['contentType']

@record['contentType'].map { |type| type['value'] }.join(' ; ')
end

def links
links = []

# Add source link if available
if @record['sourceLink']
links << {
'kind' => 'full record',
'url' => @record['sourceLink'],
'text' => 'View full record'
}
end

links
end

def citation
@record['citation'] || nil
end

def summary
return nil unless @record['summary']

@record['summary'].is_a?(Array) ? @record['summary'].join(' ') : @record['summary']
end

def publisher
# Extract from contributors or other fields
return nil unless @record['contributors']

publisher = @record['contributors'].find { |c| c['kind'] == 'Publisher' }
publisher&.dig('value')
end

def location
return nil unless @record['locations']

@record['locations'].map { |loc| loc['value'] }.compact.join('; ')
end

def subjects
return [] unless @record['subjects']

@record['subjects'].map { |subject| subject['value'] }
end

def identifier
@record['timdexRecordId']
end

# TIMDEX-specific methods
def content_type
@record['contentType']
end

def dates
@record['dates']
end

def contributors
@record['contributors']
end

def highlight
@record['highlight']
end

def source_link
@record['sourceLink']
end
end
15 changes: 15 additions & 0 deletions app/models/normalize_timdex_results.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Batch normalization for TIMDEX API results
class NormalizeTimdexResults
def initialize(results, query)
@results = results
@query = query
end

def normalize
return [] unless @results.is_a?(Array)

@results.filter_map do |doc|
NormalizeTimdexRecord.new(doc, @query).normalize
end
end
end
4 changes: 2 additions & 2 deletions app/views/search/_result.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<li class="result">
<div class="result-content">
<h3 class="record-title">
<span class="sr">Title: </span><%= link_to(result['title'], record_path(result['timdexRecordId'])) %>
<span class="sr">Title: </span><%= link_to(result['title'], record_path(result['identifier'])) %>
</h3>

<p class="pub-info">
<span><%= result['contentType']&.each { |type| type['value'] }&.join(' ; ') %></span>
<span><%= result['content_type']&.each { |type| type['value'] }&.join(' ; ') %></span>
<span>
<% result['dates']&.each do |date| %>
<%= date['value'] if date['kind'] == 'Publication date' %>
Expand Down
18 changes: 11 additions & 7 deletions app/views/search/_result_geo.html.erb
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
<li class="result">
<div class="result-content">
<h3 class="record-title">
<span class="sr">Title: </span><%= link_to(result_geo['title'], record_path(result_geo['timdexRecordId'])) %>
<span class="sr">Title: </span><%= link_to(result_geo['title'], record_path(result_geo['identifier'])) %>
</h3>

<div class="data-info">
<%= render partial: 'shared/geo_data_info', locals: { metadata: result_geo } %>
</div>

<% if result_geo['contributors'] %>
<% if result_geo['creators'].present? || result_geo['contributors'].present? %>
<span class="sr">Contributors: </span>
<ul class="list-inline truncate-list contributors">
<%= render partial: 'shared/contributors', locals: { contributors: result_geo['contributors'] } %>
<!-- Use normalized creators if available, otherwise fall back to raw contributors -->
<% contributors = result_geo['creators'].present? ? result_geo['creators'] : result_geo['contributors'] %>
<% if contributors %>
<%= render partial: 'shared/contributors', locals: { contributors: contributors } %>
<% end %>
</ul>
<% end %>

<% if result_geo['summary'] %>
<% if result_geo['summary'].present? %>
<p class="result-summary truncate-list">
<span class="sr">Summary: </span><%= result_geo['summary'].join(' ') %>
<span class="sr">Summary: </span><%= result_geo['summary'] %>
</p>
<% end %>

<% if result_geo['highlight'] %>
<div class="result-highlights">
<%= render partial: 'search/highlights', locals: { result: result_geo } %>
<%= render partial: 'search/highlights', locals: { result: { 'highlight' => result_geo['highlight'] } } %>
</div>
<% end %>

<div class="result-record">
<%= view_record(result_geo['timdexRecordId']) %>
<%= view_record(result_geo['identifier']) %>
</div>
</div>
</li>
65 changes: 14 additions & 51 deletions app/views/search/_result_primo.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
<div class="result-content">
<h3 class="record-title">
<span class="sr">Title: </span>
<% if result_primo['links']&.find { |link| link['kind'] == 'full record' } %>
<%= link_to(result_primo['title'], result_primo['links'].find { |link| link['kind'] == 'full record' }['url']) %>
<% if result['links']&.find { |link| link['kind'] == 'full record' } %>
<%= link_to(result['title'], result['links'].find { |link| link['kind'] == 'full record' }['url']) %>
<% else %>
<%= result_primo['title'] %>
<%= result['title'] %>
<% end %>
</h3>

<p class="pub-info">
<span><%= result_primo['format'] %></span>
<span><%= result_primo['year'] %></span>
<span><%= result['format'] %></span>
<span><%= result['year'] %></span>
</p>

<% if result_primo['creators'].present? %>
<% if result['creators'].present? %>
<span class="sr">Contributors: </span>
<ul class="list-inline truncate-list contributors">
<% result_primo['creators'].each do |creator| %>
<% result['creators'].each do |creator| %>
<li>
<% if creator[:link] %>
<%= link_to creator[:value], creator[:link] %>
Expand All @@ -29,49 +29,12 @@
</ul>
<% end %>

<% if result_primo['container'].present? %>
<p class="pub-info">
<span class="sr">Published in: </span>
<em><%= result_primo['container'] %></em>
</p>
<% end %>

<% if result_primo['citation'].present? %>
<p class="citation">
<span class="sr">Citation: </span>
<%= result_primo['citation'] %>
</p>
<% end %>

<% if result_primo['summary'].present? %>
<p class="summary">
<span class="sr">Summary: </span>
<%= truncate(result_primo['summary'], length: 300) %>
</p>
<% end %>

<% if result_primo['subjects'].present? %>
<p class="subjects">
<span class="sr">Subjects: </span>
<%= result_primo['subjects'].join('; ') %>
</p>
<% end %>

<% if result_primo['links'].present? %>
<ul class="list-inline links">
<% result_primo['links'].each do |link| %>
<li>
<%= link_to link['kind'].titleize, link['url'], class: 'link-button' %>
</li>
<div class="result-get">
<% if result['links'].present? %>
<% result['links'].each do |link| %>
<%= link_to link['kind'].titleize, link['url'], class: 'link-button' %>
<% end %>
</ul>
<% end %>

<% if result_primo['availability'].present? %>
<p class="availability">
<span class="sr">Availability: </span>
<span class="availability-status"><%= result_primo['availability'] %></span>
</p>
<% end %>
<% end %>
</div>
</div>
</li>
</li>
9 changes: 5 additions & 4 deletions app/views/search/results.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
<% if @results.present? && @errors.blank? %>
<h2 class="hd-3 results-context"><%= results_summary(@pagination[:hits]) %> returned</h2>
<ol class="results-list" start="<%= @pagination[:start] %>">
<% if @active_tab == 'primo' %>
<%= render(partial: 'search/result_primo', collection: @results) %>
<% else %>
<%= render(partial: 'search/result', collection: @results) %>
<% case @active_tab %>
<% when 'primo' %>
<%= render(partial: 'search/result_primo', collection: @results, as: :result) %>
<% when 'timdex' %>
<%= render(partial: 'search/result', collection: @results, as: :result) %>
<% end %>
</ol>
<% elsif @errors.blank? %>
Expand Down
Loading