Skip to content

Commit

Permalink
Merge pull request #1400 from MITLibraries/pw-132-ampersands-in-authors
Browse files Browse the repository at this point in the history
Update Primo author link encoding
  • Loading branch information
JPrevost authored Jan 21, 2025
2 parents 2d99b5e + 9e51fd8 commit fbdea98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
12 changes: 9 additions & 3 deletions app/models/normalize_primo_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ def sanitize_authors(authors)
authors.map { |author| author.strip.gsub(/\$\$Q.*$/, '') }
end

# author_link constructs a link to Primo as an exact creator search
def author_link(author)
[ENV['MIT_PRIMO_URL'], '/discovery/search?query=creator,exact,',
author, '&tab=', ENV['PRIMO_MAIN_VIEW_TAB'], '&search_scope=all&vid=',
ENV['PRIMO_VID']].join
[ENV.fetch('MIT_PRIMO_URL', nil), '/discovery/search?query=creator,exact,',
encode_author(author), '&tab=', ENV.fetch('PRIMO_MAIN_VIEW_TAB', nil), '&search_scope=all&vid=',
ENV.fetch('PRIMO_VID', nil)].join
end

# encode_author ensures author components are URI encoded
def encode_author(author)
URI.encode_uri_component(author)
end

def year
Expand Down
34 changes: 17 additions & 17 deletions test/models/normalize_primo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def popcorn
VCR.use_cassette('popcorn primo',
allow_playback_repeats: true) do
raw_query = SearchPrimo.new.search('popcorn',
ENV['PRIMO_BOOK_SCOPE'], 5)
NormalizePrimo.new.to_result(raw_query, ENV['PRIMO_BOOK_SCOPE'],
ENV.fetch('PRIMO_BOOK_SCOPE', nil), 5)
NormalizePrimo.new.to_result(raw_query, ENV.fetch('PRIMO_BOOK_SCOPE', nil),
'popcorn')
end
end
Expand All @@ -15,8 +15,8 @@ def monkeys
VCR.use_cassette('monkeys primo articles',
allow_playback_repeats: true) do
raw_query = SearchPrimo.new.search('monkeys',
ENV['PRIMO_ARTICLE_SCOPE'], 5)
NormalizePrimo.new.to_result(raw_query, ENV['PRIMO_ARTICLE_SCOPE'],
ENV.fetch('PRIMO_ARTICLE_SCOPE', nil), 5)
NormalizePrimo.new.to_result(raw_query, ENV.fetch('PRIMO_ARTICLE_SCOPE', nil),
'monkeys')
end
end
Expand All @@ -28,8 +28,8 @@ def missing_fields
VCR.use_cassette('missing fields primo',
allow_playback_repeats: true) do
raw_query = SearchPrimo.new.search('Chʻomsŭkʻi, kkŭt ŏmnŭn tojŏn',
ENV['PRIMO_BOOK_SCOPE'], 5)
NormalizePrimo.new.to_result(raw_query, ENV['PRIMO_BOOK_SCOPE'],
ENV.fetch('PRIMO_BOOK_SCOPE', nil), 5)
NormalizePrimo.new.to_result(raw_query, ENV.fetch('PRIMO_BOOK_SCOPE', nil),
'Chʻomsŭkʻi, kkŭt ŏmnŭn tojŏn')
end
end
Expand All @@ -38,8 +38,8 @@ def missing_fields
VCR.use_cassette('no results primo',
allow_playback_repeats: true) do
raw_query = SearchPrimo.new.search('popcornandorangejuice',
ENV['PRIMO_BOOK_SCOPE'], 5)
query = NormalizePrimo.new.to_result(raw_query, ENV['PRIMO_BOOK_SCOPE'],
ENV.fetch('PRIMO_BOOK_SCOPE', nil), 5)
query = NormalizePrimo.new.to_result(raw_query, ENV.fetch('PRIMO_BOOK_SCOPE', nil),
'popcornandorangejuice')
assert_equal(0, query['total'])
end
Expand Down Expand Up @@ -79,47 +79,47 @@ def missing_fields
result = popcorn['results'].first
assert_not_equal 'Rudolph, J.$$QRudolph, J.', result.authors.first.first
assert_equal ['Rudolph, J.',
'https://mit.primo.exlibrisgroup.com/discovery/search?query=creator,exact,Rudolph, J.&tab=all&search_scope=all&vid=FAKE_PRIMO_VID'],
'https://mit.primo.exlibrisgroup.com/discovery/search?query=creator,exact,Rudolph%2C%20J.&tab=all&search_scope=all&vid=FAKE_PRIMO_VID'],
result.authors.first
end

test 'multiple authors in a single element are appropriately parsed' do
result = monkeys['results'].second
assert_not_equal ['Beran, Michael J ; Smith, J. David'], result.authors.first.first
assert_equal ['Beran, Michael J',
'https://mit.primo.exlibrisgroup.com/discovery/search?query=creator,exact,Beran, Michael J&tab=all&search_scope=all&vid=FAKE_PRIMO_VID'],
'https://mit.primo.exlibrisgroup.com/discovery/search?query=creator,exact,Beran%2C%20Michael%20J&tab=all&search_scope=all&vid=FAKE_PRIMO_VID'],
result.authors.first
assert_equal ['Smith, J. David',
'https://mit.primo.exlibrisgroup.com/discovery/search?query=creator,exact,Smith, J. David&tab=all&search_scope=all&vid=FAKE_PRIMO_VID'],
'https://mit.primo.exlibrisgroup.com/discovery/search?query=creator,exact,Smith%2C%20J.%20David&tab=all&search_scope=all&vid=FAKE_PRIMO_VID'],
result.authors.second
end

test 'cleans up single author data in the expected Alma format' do
normalizer = NormalizePrimoCommon.new('record', ENV['PRIMO_BOOK_SCOPE'])
normalizer = NormalizePrimoCommon.new('record', ENV.fetch('PRIMO_BOOK_SCOPE', nil))
authors = ['Evans, Bill$$QEvans, Bill']
assert_equal ['Evans, Bill'], normalizer.sanitize_authors(authors)
end

test 'cleans up multiple author data in the expected Alma format' do
normalizer = NormalizePrimoCommon.new('record', ENV['PRIMO_BOOK_SCOPE'])
normalizer = NormalizePrimoCommon.new('record', ENV.fetch('PRIMO_BOOK_SCOPE', nil))
authors = ['Blakey, Art$$QBlakey, Art', 'Shorter, Wayne$$QShorter, Wayne']
assert_equal ['Blakey, Art', 'Shorter, Wayne'], normalizer.sanitize_authors(authors)
end

test 'cleans up multiple author data in the expected CDI format' do
normalizer = NormalizePrimoCommon.new('record', ENV['PRIMO_ARTICLE_SCOPE'])
normalizer = NormalizePrimoCommon.new('record', ENV.fetch('PRIMO_ARTICLE_SCOPE', nil))
authors = ['Blakey, Art ; Shorter, Wayne']
assert_equal ['Blakey, Art', 'Shorter, Wayne'], normalizer.sanitize_authors(authors)
end

test 'does not attempt to clean up acceptable single author data' do
normalizer = NormalizePrimoCommon.new('record', ENV['PRIMO_BOOK_SCOPE'])
normalizer = NormalizePrimoCommon.new('record', ENV.fetch('PRIMO_BOOK_SCOPE', nil))
authors = ['Redman, Joshua']
assert_equal ['Redman, Joshua'], normalizer.sanitize_authors(authors)
end

test 'does not attempt to clean up acceptable multiple author data' do
normalizer = NormalizePrimoCommon.new('record', ENV['PRIMO_BOOK_SCOPE'])
normalizer = NormalizePrimoCommon.new('record', ENV.fetch('PRIMO_BOOK_SCOPE', nil))
authors = ['Redman, Joshua', 'Mehldau, Brad']
assert_equal ['Redman, Joshua', 'Mehldau, Brad'], normalizer.sanitize_authors(authors)
end
Expand All @@ -132,7 +132,7 @@ def missing_fields

test 'can handle bad results' do
assert_raises NormalizePrimo::InvalidResults do
NormalizePrimo.new.to_result('', ENV['PRIMO_BOOK_SCOPE'], 'popcorn')
NormalizePrimo.new.to_result('', ENV.fetch('PRIMO_BOOK_SCOPE', nil), 'popcorn')
end
end
end

0 comments on commit fbdea98

Please sign in to comment.