Skip to content

Commit 8cebe48

Browse files
authored
Merge pull request #390 from MITLibraries/use-556-skipDelivery
Safe Primo link parsing
2 parents b00db4f + 8cc67ac commit 8cebe48

6 files changed

Lines changed: 27 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ may have unexpected consequences if applied to other TIMDEX UI apps.
129129
- `ORIGINS`: sets origins for CORS (currently used only for TACOS API calls).
130130
- `PLATFORM_NAME`: The value set is added to the header after the MIT Libraries logo. The logic and CSS for this comes from our theme gem.
131131
- `PRIMO_NDE_VID`: The Primo view ID for NDE Only required if `FEATURE_PRIMO_NDE_LINKS` is enabled. Ask Enterprise Systems for value.
132+
- `PRIMO_SKIP_DELIVERY`: default is `true`. Determines if `skipDelivery=true` or `skipDelivery=false` is added to Primo API calls. This affects performance and possibly responses.
132133
- `PRIMO_TIMEOUT`: The number of seconds before a Primo request times out (default 6).
133134
- `REQUESTS_PER_PERIOD` - number of requests that can be made for general throttles per `REQUEST_PERIOD`
134135
- `REQUEST_PERIOD` - time in minutes used along with `REQUESTS_PER_PERIOD`

app/models/normalize_primo_record.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def links
114114

115115
parsed_string = parse_link_string(@record['pnx']['links']['linktopdf'].first)
116116

117-
if parsed_string['U'].present?
117+
if parsed_string&.dig('U').present?
118118
links << { 'url' => parsed_string['U'],
119119
'kind' => 'Get PDF' }
120120
end
@@ -125,7 +125,7 @@ def links
125125

126126
parsed_string = parse_link_string(@record['pnx']['links']['linktohtml'].first)
127127

128-
if parsed_string['U'].present?
128+
if parsed_string&.dig('U').present?
129129
links << { 'url' => parsed_string['U'],
130130
'kind' => 'Read online' }
131131
end
@@ -323,6 +323,7 @@ def availability
323323
end
324324

325325
def other_availability
326+
return unless @record['delivery']
326327
return unless @record['delivery']['bestlocation']
327328
return unless @record['delivery']['holding']
328329

app/models/primo_search.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ def primo_vid
9797
ENV.fetch('PRIMO_VID', nil)
9898
end
9999

100+
# skip_delivery determines whether to include the delivery parameter in Primo API calls, which affects performance
101+
# and in theory results (but we have not observed differences yet). Setting it to true can speed up response times
102+
# but may exclude some availability information.
103+
def skip_delivery?
104+
ENV.fetch('PRIMO_SKIP_DELIVERY', 'true').to_s.downcase == 'true'
105+
end
106+
100107
# clean_term performs search term sanitization to prepare the term for querying the Primo API
101108
#
102109
# We are using the built in `URI.encode_www_form_component`. Previous versions (including in bento) had us doing
@@ -121,7 +128,8 @@ def search_url(term, per_page, offset = 0)
121128
primo_scope,
122129
'&limit=',
123130
per_page,
124-
'&pcAvailability=true'
131+
'&pcAvailability=true',
132+
"&skipDelivery=#{skip_delivery?}"
125133
]
126134

127135
# Add offset parameter for pagination (only if > 0)

test/models/normalize_primo_record_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ def cdi_record
8484
assert_empty normalized[:links]
8585
end
8686

87+
test 'handles non-$$ formatted pdf and html link strings without raising' do
88+
record = full_record.deep_dup
89+
record['pnx']['links']['linktopdf'] = ['https://example.com/plain-url.pdf']
90+
record['pnx']['links']['linktohtml'] = ['https://example.com/plain-url.html']
91+
92+
assert_nothing_raised { NormalizePrimoRecord.new(record, 'test').normalize }
93+
normalized = NormalizePrimoRecord.new(record, 'test').normalize
94+
link_kinds = normalized[:links].map { |l| l['kind'] }
95+
assert_not_includes link_kinds, 'Get PDF'
96+
assert_not_includes link_kinds, 'Read online'
97+
end
98+
8799
test 'parse_link_string creates expected data structure' do
88100
# Strings that don't start with $$ should not be processed
89101
link_string = 'https://example.com?param1=value1&param2=value2'

test/vcr_cassettes/primo_search_error.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/primo_search_success.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)