-
Notifications
You must be signed in to change notification settings - Fork 0
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
Validate ISBNs detected via regex #171
Merged
Merged
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
--no-private --protected app/**/*.rb - docs/**/*.md | ||
--private --protected app/**/*.rb - docs/**/*.md |
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 |
---|---|---|
|
@@ -17,12 +17,15 @@ def self.table_name_prefix | |
extend Detector::BulkChecker | ||
|
||
# Initialization process will run pattern checkers and strip invalid ISSN detections. | ||
# @param phrase String. Often a `Term.phrase`. | ||
# @return Nothing intentional. Data is written to Hash `@detections` during processing. | ||
# | ||
# @param phrase String. Often a `Term.phrase`. | ||
# @return nil. Data is written to Hash `@detections` during processing. Things technically get | ||
# returned here but it is a side effect and should not be relied on. | ||
def initialize(phrase) | ||
@detections = {} | ||
pattern_checker(phrase) | ||
strip_invalid_issns | ||
strip_invalid_isbns | ||
end | ||
|
||
# The record method will consult the set of regex-based detectors that are defined in | ||
|
@@ -53,13 +56,76 @@ def self.record(term) | |
def patterns | ||
{ | ||
barcode: /^39080[0-9]{9}$/, | ||
isbn: /\b(ISBN-*(1[03])* *(: ){0,1})*(([0-9Xx][- ]*){13}|([0-9Xx][- ]*){10})\b/, | ||
isbn: /\b(([0-9Xx][- ]*){13}|([0-9Xx][- ]*){10})\b/, | ||
issn: /\b[0-9]{4}-[0-9]{3}[0-9xX]\b/, | ||
pmid: /\b((pmid|PMID):\s?(\d{7,8}))\b/, | ||
doi: %r{\b10\.(\d+\.*)+/(([^\s.])+\.*)+\b} | ||
} | ||
end | ||
|
||
# strip_invalid_isbns coordinates the logic to remove ISBNs that are not valid from our list of detected ISBNs | ||
# | ||
# ISBNs cannot be validated via regex. Regex gives us a list of candidates that look like ISBNs. We remove invalid | ||
# ISBNs by following validation specifications defined in the standard. | ||
def strip_invalid_isbns | ||
return unless @detections[:isbn] | ||
|
||
@detections.delete(:isbn) unless valid_isbn?(@detections[:isbn]) | ||
end | ||
|
||
# valid_isbn? checks for 10 or 13 digit ISBNs and defers to appropriate methods for each | ||
# | ||
# @param candidate String. A string representation of a regex detected ISBN. | ||
# @return boolean | ||
def valid_isbn?(candidate) | ||
digits = candidate.delete('-').chars | ||
|
||
# check 10 digit | ||
if digits.length == 10 | ||
valid_isbn_10?(digits) | ||
# check 13 digit | ||
elsif digits.length == 13 | ||
valid_isbn_13?(digits) | ||
# This shouldn't happen, log an error. | ||
else | ||
Rails.logger.error("Non-10 or 13 digit sequence detected as ISBN: #{candidate}") | ||
Sentry.capture_message('Non-10 or 13 digit sequence detected as ISBN') | ||
false | ||
end | ||
end | ||
|
||
# valid_isbn_10? follows the ISBN 10 specification for validation | ||
# https://en.wikipedia.org/wiki/ISBN#ISBN-10_check_digits | ||
# | ||
# @param digits Array. An array of strings representing each character from a detected ISBN candidate. | ||
# @return boolean | ||
def valid_isbn_10?(digits) | ||
sum = 0 | ||
digits.each_with_index do |digit, index| | ||
digit = '10' if digit.casecmp('x').zero? | ||
sum += digit.to_i * (10 - index) | ||
end | ||
(sum % 11).zero? | ||
end | ||
|
||
# valid_isbn_13? follows the ISBN 13 specification for validation | ||
# https://en.wikipedia.org/wiki/ISBN#ISBN-13_check_digit_calculation | ||
# | ||
# @param digits Array. An array of strings representing each character from a detected ISBN candidate. | ||
# @return boolean | ||
def valid_isbn_13?(digits) | ||
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. Similar to the comment above, this is an example using reduce for input on whether one feels easier to understand on a quick read of the method. def valid_isbn_13?(digits)
sum = digits.map(&:to_i).each_with_index.reduce(0) do |acc, (digit, index)|
acc += digit * (index.even? ? 1 : 3)
end
(sum % 10).zero?
end |
||
sum = 0 | ||
digits.map(&:to_i).each_with_index do |digit, index| | ||
sum += digit * (index.even? ? 1 : 3) | ||
end | ||
|
||
(sum % 10).zero? | ||
end | ||
|
||
# strip_invalid_issns coordinates the logic to remove ISSNs that are not valid from our list of detected ISSNs | ||
# | ||
# ISSNs cannot be validated via regex. Regex gives us a list of candidates that look like ISSNs. We remove invalid | ||
# ISSNs by following validation specifications defined in the standard. | ||
def strip_invalid_issns | ||
return unless @detections[:issn] | ||
|
||
|
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.
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.
There's an alternate way to write this taking advantage of the reduce method. I wasn't sure which felt more clear and chose not to use reduce but wanted to show the other option to get input from code review as to which feels easier to understand (as they both do the same thing).
acc
is a conventional name for the accumulator. If another name would be clearer, there is nothing special aboutacc
.