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'
Since the shortest possible string is 7 digits followed by a checksum digit
for an ISSN, we'll make sure they're at least that long. Still imperfect
(would fine "5------", for example) but should work in most cases.

#reduce_to_basics(rawnum, valid_sizes = nil) ⇒ String?

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)

Parameters:

rawnum(String)
—

The raw string containing (hopefully) an ISSN/ISBN

valid_sizes(Integer, Array<Integer>, nil)(defaults to: nil)
—

An integer or array of integers of valid sizes

Returns:

(String, nil)
—

the reduced and verified number, or nil if there's no match at the right size

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

# File 'lib/library_stdnums.rb', line 42defreduce_to_basicsrawnum,valid_sizes=nilreturnnilifrawnum.nil?num=extractNumberrawnum# Does it even look like a number?returnnilunlessnum# Return what we've got if we don't care about the sizereturnnumunlessvalid_sizes# Check for valid size(s)[valid_sizes].flatten.eachdo|s|returnnumifnum.size==send# Didn't check out size-wise. Return nilreturnnilend