Module: StdNum::Helpers
Overview
Helper methods common to ISBN/ISSN
Constant Summary
- STDNUMPAT =
The pattern we use to try and find an ISBN/ISSN. Ditch everthing before the first digit, then take all the digits/hyphens, optionally followed by an 'X'
/^.*?(\d[\d\-]+[xX]?)/
Instance Method Summary (collapse)
-
- (String) extractNumber(str)
Extract the most likely looking number from the string.
-
- (String?) reduce_to_basics(rawnum, valid_sizes = nil)
Given any string, extract what looks like the most likely ISBN/ISSN of the given size(s), or nil if nothing matches at the correct size.
Instance Method Details
- (String) extractNumber(str)
Extract the most likely looking number from the string. This will be the first string of digits-and-hyphens-and-maybe-a-trailing-X, with the hypens removed
15 16 17 18 19 |
# File 'lib/library_stdnums.rb', line 15 def extractNumber str match = STDNUMPAT.match str return nil unless match return match[1].gsub(/\-/, '').upcase end |
- (String?) reduce_to_basics(rawnum, valid_sizes = nil)
Given any string, extract what looks like the most likely ISBN/ISSN of the given size(s), or nil if nothing matches at the correct size. for this type (e.g., 10 or 13 for ISBN, 8 for ISSN)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/library_stdnums.rb', line 27 def reduce_to_basics rawnum, valid_sizes = nil return nil if rawnum.nil? num = extractNumber rawnum # Does it even look like a number? return nil unless num # Return what we've got if we don't care about the size return num unless valid_sizes # Check for valid size(s) [valid_sizes].flatten.each do |s| return num if num.size == s end # Didn't check out size-wise. Return nil return nil end |