-
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.
Merge pull request #169 from MITLibraries/tco-72
Adds barcode detector and lookup
- Loading branch information
Showing
22 changed files
with
399 additions
and
22 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
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,66 @@ | ||
# frozen_string_literal: true | ||
|
||
# LookupBarcode takes a 14-digit integer (flagged by a regex within the Detector::StandardIdentifier class) and consults | ||
# the Primo API for the associated record. The structure of this class is pretty close to the other lookup models, with | ||
# an info method being the only public method. If Primo finds a record for the submitted barcode, the class returns some | ||
# metadata about the record, along with a link to the complete record using the discovery/fulldisplay path. | ||
class LookupBarcode | ||
# info takes a barcode as an argument and returns associated metadata about that item, provided Primo is able to | ||
# locate it. If no record is found for any reason, the method returns nil. | ||
# | ||
# @note While the barcode argument is technically a string, in reality it should be a 14-digit integer in order to | ||
# return anything meaningful. | ||
# @param barcode String | ||
# @return Hash or Nil | ||
def info(barcode) | ||
xml = fetch(barcode) | ||
|
||
return if xml == 'Error' | ||
|
||
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('//default:recordIdentifier', 'default' => 'http://www.loc.gov/zing/srw/').text, | ||
title: xml.xpath('//dc:title', 'dc' => 'http://purl.org/dc/elements/1.1/').text, | ||
date: xml.xpath('//dc:date', 'dc' => 'http://purl.org/dc/elements/1.1/').text, | ||
publisher: xml.xpath('//dc:publisher', 'dc' => 'http://purl.org/dc/elements/1.1/').text, | ||
authors: xml.xpath('//dc:contributor', 'dc' => 'http://purl.org/dc/elements/1.1/').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) | ||
else | ||
Rails.logger.debug do | ||
"Barcode lookup error. Barcode #{barcode} detected but Primo returned an error status" | ||
end | ||
Rails.logger.debug { "URL: #{url(barcode)}" } | ||
Sentry.capture_message('Primo API error after barcode detection') | ||
'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
5 changes: 5 additions & 0 deletions
5
db/migrate/20250110164046_add_barcode_to_metrics_algorithms.rb
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,5 @@ | ||
class AddBarcodeToMetricsAlgorithms < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :metrics_algorithms, :barcode, :integer | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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 |
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
Oops, something went wrong.