Skip to content

Commit 4add3a2

Browse files
committed
Fixes issue with navigating pnx
The results normalizer passed doc['pnx'] to the record normalizer, which does not work because there's metadata we need outside of doc['pnx']. I also ran rubocop and removed the checks for the 'pnx' field, which seemed excessive.
1 parent 349c2e6 commit 4add3a2

File tree

4 files changed

+36
-54
lines changed

4 files changed

+36
-54
lines changed

app/models/normalize_primo_record.rb

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def title
3838
end
3939
end
4040

41-
def creators
41+
def creators
4242
return [] unless @record['pnx']['display']['creator'] || @record['pnx']['display']['contributor']
4343

4444
author_list = []
@@ -84,7 +84,7 @@ def format
8484
# the one that is most predictably useful to us. The record_link is constructed.
8585
def links
8686
links = []
87-
87+
8888
# Use dedup URL as the full record link if available, otherwise use record link
8989
if dedup_url.present?
9090
links << { 'url' => dedup_url, 'kind' => 'full record' }
@@ -93,9 +93,7 @@ def links
9393
end
9494

9595
# Add openurl if available
96-
if openurl.present?
97-
links << { 'url' => openurl, 'kind' => 'openurl' }
98-
end
96+
links << { 'url' => openurl, 'kind' => 'openurl' } if openurl.present?
9997

10098
# Return links if we found any
10199
links.any? ? links : []
@@ -167,7 +165,7 @@ def record_link
167165
end
168166

169167
def numbering
170-
return unless @record['pnx'] && @record['pnx']['addata']
168+
return unless @record['pnx']['addata']
171169
return unless @record['pnx']['addata']['volume']
172170

173171
if @record['pnx']['addata']['issue'].present?
@@ -178,7 +176,7 @@ def numbering
178176
end
179177

180178
def chapter_numbering
181-
return unless @record['pnx'] && @record['pnx']['addata']
179+
return unless @record['pnx']['addata']
182180
return unless @record['pnx']['addata']['btitle']
183181
return unless @record['pnx']['addata']['date'] && @record['pnx']['addata']['pages']
184182

@@ -192,7 +190,7 @@ def sanitize_authors(authors)
192190

193191
def author_link(author)
194192
[ENV.fetch('MIT_PRIMO_URL'),
195-
'/discovery/search?query=creator,exact,',
193+
'/discovery/search?query=creator,exact,',
196194
encode_author(author),
197195
'&tab=', ENV.fetch('PRIMO_TAB'),
198196
'&search_scope=all&vid=',
@@ -219,7 +217,7 @@ def openurl
219217
return unless @record['delivery'] && @record['delivery']['almaOpenurl']
220218

221219
# Check server match
222-
openurl_server = ENV['ALMA_OPENURL'][8, 4]
220+
openurl_server = ENV.fetch('ALMA_OPENURL', nil)[8, 4]
223221
record_openurl_server = @record['delivery']['almaOpenurl'][8, 4]
224222
if openurl_server == record_openurl_server
225223
construct_primo_openurl
@@ -236,13 +234,13 @@ def construct_primo_openurl
236234
# Search API to redirect to the Primo UI. This is done for UX purposes,
237235
# as the regular Alma link resolver URLs redirect to a plaintext
238236
# disambiguation page.
239-
primo_openurl_base = [ENV['MIT_PRIMO_URL'],
237+
primo_openurl_base = [ENV.fetch('MIT_PRIMO_URL', nil),
240238
'/discovery/openurl?institution=',
241-
ENV['EXL_INST_ID'],
239+
ENV.fetch('EXL_INST_ID', nil),
242240
'&vid=',
243-
ENV['PRIMO_VID'],
241+
ENV.fetch('PRIMO_VID', nil),
244242
'&'].join
245-
primo_openurl = @record['delivery']['almaOpenurl'].gsub(ENV['ALMA_OPENURL'], primo_openurl_base)
243+
primo_openurl = @record['delivery']['almaOpenurl'].gsub(ENV.fetch('ALMA_OPENURL', nil), primo_openurl_base)
246244

247245
# The ctx params appear to break Primo openurls, so we need to remove them.
248246
params = Rack::Utils.parse_nested_query(primo_openurl)
@@ -256,34 +254,39 @@ def thumbnail
256254
# A record can have multiple ISBNs, so we are assuming here that
257255
# the thumbnail URL can be constructed from the first occurrence
258256
isbn = @record['pnx']['addata']['isbn'].first
259-
[ENV['SYNDETICS_PRIMO_URL'], '&isbn=', isbn, '/sc.jpg'].join
257+
[ENV.fetch('SYNDETICS_PRIMO_URL', nil), '&isbn=', isbn, '/sc.jpg'].join
260258
end
261259

262260
def publisher
263261
return unless @record['pnx']['addata'] && @record['pnx']['addata']['pub']
262+
264263
@record['pnx']['addata']['pub'].first
265264
end
266265

267266
def best_location
268267
return unless @record['delivery']
269268
return unless @record['delivery']['bestlocation']
269+
270270
loc = @record['delivery']['bestlocation']
271271
["#{loc['mainLocation']} #{loc['subLocation']}", loc['callNumber']]
272272
end
273273

274274
def subjects
275275
return [] unless @record['pnx']['display']['subject']
276+
276277
@record['pnx']['display']['subject']
277278
end
278279

279280
def best_availability
280281
return unless best_location
282+
281283
@record['delivery']['bestlocation']['availabilityStatus']
282284
end
283285

284286
def other_availability?
285287
return unless @record['delivery']['bestlocation']
286288
return unless @record['delivery']['holding']
289+
287290
@record['delivery']['holding'].length > 1
288291
end
289292

@@ -292,25 +295,26 @@ def other_availability?
292295
def frbrized?
293296
return unless @record['pnx']['facets']
294297
return unless @record['pnx']['facets']['frbrtype']
298+
295299
@record['pnx']['facets']['frbrtype'].join == '5'
296300
end
297301

298302
def dedup_url
299303
return unless frbrized?
300304
return unless @record['pnx']['facets']['frbrgroupid'] &&
301305
@record['pnx']['facets']['frbrgroupid'].length == 1
302-
306+
303307
frbr_group_id = @record['pnx']['facets']['frbrgroupid'].join
304-
base = [ENV['MIT_PRIMO_URL'], '/discovery/search?'].join
305-
308+
base = [ENV.fetch('MIT_PRIMO_URL', nil), '/discovery/search?'].join
309+
306310
query = {
307311
query: "any,contains,#{@query}",
308-
tab: ENV['PRIMO_TAB'],
309-
search_scope: ENV['PRIMO_SCOPE'],
312+
tab: ENV.fetch('PRIMO_TAB', nil),
313+
search_scope: ENV.fetch('PRIMO_SCOPE', nil),
310314
sortby: 'date_d',
311-
vid: ENV['PRIMO_VID'],
315+
vid: ENV.fetch('PRIMO_VID', nil),
312316
facet: "frbrgroupid,include,#{frbr_group_id}"
313317
}.to_query
314318
[base, query].join
315319
end
316-
end
320+
end

app/models/normalize_primo_results.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ def normalize
99
return [] unless @results&.dig('docs')
1010

1111
@results['docs'].filter_map do |doc|
12-
next unless doc['pnx']
13-
14-
NormalizePrimoRecord.new(doc['pnx'], @query).normalize
12+
NormalizePrimoRecord.new(doc, @query).normalize
1513
end
1614
end
1715

1816
def total_results
1917
@results&.dig('info', 'total') || 0
2018
end
21-
end
19+
end

test/models/normalize_primo_record_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def minimal_record
6363

6464
# First link should be the record link
6565
assert_equal 'full record', normalized['links'].first['kind']
66-
66+
6767
# Second link should be the Alma openurl
6868
assert_equal 'openurl', normalized['links'].second['kind']
6969
end
@@ -230,10 +230,10 @@ def minimal_record
230230
test 'includes expected link types when available' do
231231
normalized = NormalizePrimoRecord.new(full_record, 'test query').normalize
232232
link_kinds = normalized['links'].map { |link| link['kind'] }
233-
233+
234234
assert_includes link_kinds, 'full record'
235235
assert_includes link_kinds, 'openurl'
236-
assert_equal 2, normalized['links'].length # Only full record and openurl
236+
assert_equal 2, normalized['links'].length # Only full record and openurl
237237
end
238238

239239
# Additional coverage tests for existing methods
@@ -308,9 +308,9 @@ def minimal_record
308308

309309
test 'uses btitle when jtitle is not present for container' do
310310
record = full_record.deep_dup
311-
record['pnx']['addata'].delete('jtitle') # Remove jtitle
311+
record['pnx']['addata'].delete('jtitle') # Remove jtitle
312312
record['pnx']['addata']['btitle'] = ['Book Title Only']
313-
313+
314314
normalized = NormalizePrimoRecord.new(record, 'test').normalize
315315
assert_equal 'Book Title Only', normalized['container']
316316
end
@@ -328,4 +328,4 @@ def minimal_record
328328
openurl_link = normalized['links'].find { |link| link['kind'] == 'openurl' }
329329
assert_not_nil openurl_link
330330
end
331-
end
331+
end

test/models/normalize_primo_results_test.rb

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class NormalizePrimoResultsTest < ActiveSupport::TestCase
44
def sample_results
55
{
66
'docs' => [
7-
{ 'pnx' => JSON.parse(File.read(Rails.root.join('test/fixtures/primo/full_record.json'))) },
8-
{ 'pnx' => JSON.parse(File.read(Rails.root.join('test/fixtures/primo/minimal_record.json'))) }
7+
JSON.parse(File.read(Rails.root.join('test/fixtures/primo/full_record.json'))),
8+
JSON.parse(File.read(Rails.root.join('test/fixtures/primo/minimal_record.json')))
99
],
1010
'info' => {
1111
'total' => 10
@@ -53,26 +53,6 @@ def empty_results
5353
assert_empty normalized
5454
end
5555

56-
# I hope this never happens, but just in case!
57-
test 'handles docs without pnx field' do
58-
results_with_bad_docs = {
59-
'docs' => [
60-
{ 'pnx' => JSON.parse(File.read(Rails.root.join('test/fixtures/primo/full_record.json'))) },
61-
{ 'other_field' => 'no pnx here' },
62-
{ 'pnx' => JSON.parse(File.read(Rails.root.join('test/fixtures/primo/minimal_record.json'))) }
63-
],
64-
'info' => { 'total' => 3 }
65-
}
66-
67-
normalizer = NormalizePrimoResults.new(results_with_bad_docs, 'test query')
68-
normalized = normalizer.normalize
69-
70-
# Should only normalize the 2 docs with pnx fields
71-
assert_equal 2, normalized.length
72-
assert_equal 'Testing the Limits of Knowledge', normalized.first['title']
73-
assert_equal 'unknown title', normalized.second['title']
74-
end
75-
7656
test 'returns total results count' do
7757
normalizer = NormalizePrimoResults.new(sample_results, 'test query')
7858
assert_equal 10, normalizer.total_results
@@ -87,4 +67,4 @@ def empty_results
8767
normalizer = NormalizePrimoResults.new(nil, 'test query')
8868
assert_equal 0, normalizer.total_results
8969
end
90-
end
70+
end

0 commit comments

Comments
 (0)