-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The returned information from the Primo API is sent to the user in the same details block that other lookups use. We do not have a standard model for this, though. Updates to barcode lookup model New controller test Refactor barcode lookup method
- Loading branch information
1 parent
cc39da7
commit 0c80867
Showing
6 changed files
with
294 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
class LookupBarcode | ||
def info(barcode) | ||
xml = fetch(barcode) | ||
|
||
return if xml == 'Error' | ||
|
||
# return if xml.xpath('//numberOfRecords').text.to_i.zero? | ||
|
||
metadata = extract_metadata(xml) | ||
|
||
if metadata.reject { |_k, v| v.empty? }.present? | ||
metadata[:barcode] = barcode | ||
metadata[:link_resolver_url] = link_resolver_url(metadata) | ||
metadata | ||
else | ||
Rails.logger.debug { "Barcode lookup error. Barcode #{barcode} detected by Primo returned no data" } | ||
nil | ||
end | ||
end | ||
|
||
private | ||
|
||
def extract_metadata(xml) | ||
{ | ||
recordId: xml.xpath('//recordIdentifier').text, | ||
title: xml.xpath('//title').text, | ||
date: xml.xpath('//date').text, | ||
publisher: xml.xpath('//publisher').text, | ||
authors: xml.xpath('//contributor').text | ||
} | ||
end | ||
|
||
def url(barcode) | ||
"https://mit.alma.exlibrisgroup.com/view/sru/01MIT_INST?version=1.2&operation=searchRetrieve&recordSchema=dc&query=alma.all_for_ui=#{barcode}" | ||
end | ||
|
||
def fetch(barcode) | ||
resp = HTTP.headers(accept: 'application/xml').get(url(barcode)) | ||
|
||
if resp.status == 200 | ||
Nokogiri::XML(resp.to_s).remove_namespaces! | ||
else | ||
Rails.logger.debug do | ||
"Barcode lookup error. Barcode #{barcode} detected but Primo returned an error status" | ||
end | ||
Rails.logger.debug { "URL: #{url(barcode)}" } | ||
'Error' | ||
end | ||
end | ||
|
||
def link_resolver_url(metadata) | ||
"https://mit.primo.exlibrisgroup.com/discovery/fulldisplay?vid=01MIT_INST:MIT&docid=alma#{metadata[:recordId]}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class LookupBarcodeTest < ActiveSupport::TestCase | ||
test 'metadata object is returned with expected fields' do | ||
VCR.use_cassette('barcode 39080027236626') do | ||
metadata = LookupBarcode.new.info('39080027236626') | ||
|
||
expected_keys = %i[title date publisher authors link_resolver_url] | ||
|
||
expected_keys.each do |key| | ||
assert_includes(metadata.keys, key) | ||
end | ||
end | ||
end | ||
|
||
test 'link resolver URL returns a simple item URL' do | ||
VCR.use_cassette('barcode 39080027236626') do | ||
metadata = LookupBarcode.new.info('39080027236626') | ||
|
||
expected_url = 'https://mit.primo.exlibrisgroup.com/discovery/fulldisplay?vid=01MIT_INST:MIT&docid=alma990002933430106761' | ||
|
||
assert_equal(expected_url, metadata[:link_resolver_url]) | ||
end | ||
end | ||
|
||
test 'barcode not found' do | ||
VCR.use_cassette('barcode not found') do | ||
metadata = LookupBarcode.new.info('this-is-not-a-barcode') | ||
|
||
assert_nil(metadata) | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.