-
Notifications
You must be signed in to change notification settings - Fork 0
Implement full-text options links using SRU #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jazairi
wants to merge
4
commits into
main
Choose a base branch
from
use-663-sru
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -20,11 +20,15 @@ class InvalidAlmaId < StandardError; end | |
|
|
||
| # lookup is the primary method of interacting with this model. | ||
| # | ||
| # It will receive an Alma ID, validate it, look it up in the Alma SRU, and return a formatted result. | ||
| # It will receive an Alma ID, validate it, look it up in the Alma SRU, and return availability info and Alma-E status. | ||
| # | ||
| # It accepts an "alma_client" argument for use when testing, but this is not used in normal operations. | ||
| # | ||
| # Returns a hash with: | ||
| # - :availability => formatted availability statements array | ||
| # - :alma_e => boolean indicating if record is Alma-E | ||
| def self.lookup(raw_identifier, alma_client: nil) | ||
| return [] unless enabled? | ||
| return { availability: [], alma_e: false } unless enabled? | ||
|
|
||
| # Validate the raw identifier received. This will raise an InvalidAlmaId if validation fails. | ||
| raise InvalidAlmaId unless valid_alma_id?(raw_identifier) | ||
|
|
@@ -41,24 +45,22 @@ def self.lookup(raw_identifier, alma_client: nil) | |
| parse_response(alma_http.timeout(6).get(url), identifier) | ||
| rescue InvalidAlmaId | ||
| Rails.logger.debug("Invalid Alma ID: #{raw_identifier}") | ||
|
|
||
| [] | ||
| { availability: [], alma_e: false } | ||
| rescue LookupFailure => e | ||
| Rails.logger.debug("Alma lookup failure: #{e}") | ||
|
|
||
| [] | ||
| { availability: [], alma_e: false } | ||
| rescue HTTP::Error | ||
| Sentry.capture_message('Alma SRU connection failure') | ||
| Rails.logger.error('Alma SRU connection error') | ||
|
|
||
| [] | ||
| { availability: [], alma_e: false } | ||
| end | ||
|
|
||
| # parse_response receives the raw response from the Alma SRU endpoint. | ||
| # | ||
| # For any non-200 response, it raises a LookupFailure. | ||
| # | ||
| # Other responses (in XML format) are parsed by Nokogiri, and we pluck content with an `AVA` tag. | ||
| # Other responses (in XML format) are parsed by Nokogiri to extract both AVA (holdings) and AVE (electronic) data. | ||
| # Returns a hash with :availability and :alma_e keys. | ||
| def self.parse_response(raw_response, reference_identifier) | ||
| raise LookupFailure, raw_response.status unless raw_response.status == 200 | ||
|
|
||
|
|
@@ -76,7 +78,15 @@ def self.parse_response(raw_response, reference_identifier) | |
|
|
||
| # Reduce list to a single item if multiples exist | ||
| results[0] += ' and other locations' if results.length > 1 | ||
| results.first(1) | ||
| availability = results.first(1) | ||
|
|
||
| # Check for AVE tags to determine if record is Alma-E | ||
| alma_e = alma_e?(parsed) | ||
|
|
||
| { | ||
| availability: availability, | ||
| alma_e: alma_e | ||
| } | ||
|
qltysh[bot] marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| end | ||
|
|
||
| # ava_to_hash takes an XML element that represents a single availability record | ||
|
|
@@ -112,6 +122,26 @@ def self.fetch_controlfield(parsed_xml) | |
| parsed_xml.xpath("//holding:controlfield[@tag='001']", NAMESPACE)&.text | ||
| end | ||
|
|
||
| # alma_e? receives a parsed XML document (Nokogiri::XML::Document) and returns true if the record | ||
| # contains AVE (electronic) datafields. We fall back on the 959 subfield b for legacy records. | ||
| def self.alma_e?(parsed_xml) | ||
| return true if parsed_xml.xpath("//holding:datafield[@tag='AVE']", NAMESPACE).any? | ||
|
|
||
| legacy_net_access?(parsed_xml) | ||
| end | ||
|
|
||
| # Some records omit AVE but still indicate electronic access in local 959$b=NET. After consulting | ||
| # with Metadata and Enterprise Systems, we learned that this a deprecated practice from before the | ||
| # Alma migration. | ||
| # | ||
| # It is still unclear whether Primo is determining electronic access from this subfield, but it's | ||
| # the only electronic access indicator we can find in the record other than AVE. We can revisit | ||
| # this approach if it proves ineffective. | ||
| def self.legacy_net_access?(parsed_xml) | ||
| parsed_xml.xpath("//holding:datafield[@tag='959']/holding:subfield[@code='b']", NAMESPACE) | ||
| .any? { |node| node.text.to_s.strip.casecmp('NET').zero? } | ||
| end | ||
|
|
||
| # format_availability receives a hash representing a single availability | ||
| # statement, and formats it for human readability. Values for "e" and "q" are | ||
| # required, while "c" and "d" are optional. | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| <% if AlmaSru.enabled? && @availability.present? %> | ||
| <div class="availability"> | ||
| <% @availability.each do |statement| %> | ||
| <p><%= link_to(sanitize(statement, tags: %w[i strong], attributes: %w[class aria-hidden]), | ||
| "#{PrimoLinkBuilder.new(record_id: params[:doc_id], context: 'L').full_record_link}#getit_link1_0", | ||
| data: {content_piece: 'Availability Link' }) %></p> | ||
| <% end %> | ||
| </div> | ||
| <% if AlmaSru.enabled? %> | ||
| <% if @alma_e %> | ||
| <% fulltext_url = PrimoLinkBuilder.new(record_id: params[:doc_id], context: 'L').full_record_link + '#nui.getit.service_viewit' %> | ||
| <%= link_to 'Full-text options', fulltext_url, class: 'button alma-fulltext-options', data: { content_piece: 'Full-text Options' } %> | ||
| <% end %> | ||
| <% if @availability.present? %> | ||
| <div class="availability"> | ||
| <% @availability.each do |statement| %> | ||
| <p><%= link_to(sanitize(statement, tags: %w[i strong], attributes: %w[class aria-hidden]), | ||
| "#{PrimoLinkBuilder.new(record_id: params[:doc_id], context: 'L').full_record_link}#getit_link1_0", | ||
| data: {content_piece: 'Availability Link' }) %></p> | ||
| <% end %> | ||
| </div> | ||
| <% end %> | ||
| <% end %> |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method has too many lines. [16/10] [rubocop:Metrics/MethodLength]