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
9 changes: 7 additions & 2 deletions app/models/primo_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ def primo_vid
ENV.fetch('PRIMO_VID', nil)
end

# Initial search term sanitization
# clean_term performs search term sanitization to prepare the term for querying the Primo API
#
# We are using the built in `URI.encode_www_form_component`. Previous versions (including in bento) had us doing
# character replacements. That strategy masked some problems in that it prevented errors, but did not return the
# expected results when compared to the Primo UI. This simpler strategy seems to handle all of the types of problems
# we previously were facing better, and very likely handles problems we were not aware of.
def clean_term(term)
term.strip.tr(' :,', '+').gsub(/\++/, '+')
URI.encode_www_form_component(term)
end

# Constructs the search URL with required parameters for Primo API
Expand Down
18 changes: 17 additions & 1 deletion test/models/primo_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,24 @@ class PrimoSearchTest < ActiveSupport::TestCase

test 'sanitizes search terms' do
search = PrimoSearch.new

# spaces, colons, commas which historically have been problems
clean_term = search.send(:clean_term, 'test: search, term')
assert_equal 'test+search+term', clean_term
assert_equal 'test%3A+search%2C+term', clean_term

# double quotes
clean_term = search.send(:clean_term, '"hello world"')
assert_equal '%22hello+world%22', clean_term

# our previous logic mangled this and resulted in bad results
clean_term = search.send(:clean_term, 'c++')
assert_equal 'c%2B%2B', clean_term

# full citation that has many of the features that have lead to problematic encoding
clean_term = search.send(:clean_term,
'Fuzuloparib with or without apatinib as maintenance therapy in newly diagnosed, advanced ovarian cancer (FZOCUS-1): A multicenter, randomized, double-blind, placebo-controlled phase 3 trial. Wu L, et al. CA Cancer J Clin. 2026.')
assert_equal 'Fuzuloparib+with+or+without+apatinib+as+maintenance+therapy+in+newly+diagnosed%2C+advanced+ovarian+cancer+%28FZOCUS-1%29%3A+A+multicenter%2C+randomized%2C+double-blind%2C+placebo-controlled+phase+3+trial.+Wu+L%2C+et+al.+CA+Cancer+J+Clin.+2026.',
clean_term
end

test 'sets timeout from ENV' do
Expand Down