module StdNum::ISSN

Validate and and normalize ISSNs

Public Class Methods

at_least_trying?(issn) click to toggle source

Does it even look like an ISSN?

# File lib/library_stdnums.rb, line 207
def self.at_least_trying? issn
  return !(reduce_to_basics(issn, 8))
end
checkdigit(issn, preprocessed = false) click to toggle source

Compute the checkdigit of an ISSN @param [String] issn The ISSN (we'll try to clean it up if possible) @param [Boolean] preprocessed Set to true if the number has already been through reduce_to_basic @return [String] the one-character checkdigit

# File lib/library_stdnums.rb, line 217
def self.checkdigit issn, preprocessed = false
  issn = reduce_to_basics issn, 8 unless preprocessed
  return nil unless issn

  digits = issn[0..6].split(//).map {|i| i.to_i}
  checkdigit = 0
  (0..6).each do |i|
    checkdigit += digits[i] * (8 - i)
  end
  checkdigit = checkdigit % 11
  return '0' if checkdigit == 0
  checkdigit = 11 - checkdigit
  return 'X' if checkdigit == 10
  return checkdigit.to_s
end
normalize(rawissn) click to toggle source

Make sure it's valid, remove the dashes, uppercase the X, and return @param [String] rawissn The ISSN to normalize @return [String, nil] the normalized ISSN, or nil on failure

# File lib/library_stdnums.rb, line 251
def self.normalize rawissn
  issn = reduce_to_basics rawissn, 8
  if issn and valid?(issn, true)
    return issn
  else
    return nil
  end
end
valid?(issn, preprocessed = false) click to toggle source

Check to see if the checkdigit is correct @param [String] issn The ISSN (we'll try to clean it up if possible) @param [Boolean] preprocessed Set to true if the number has already been through reduce_to_basic @return [Boolean] Whether or not the checkdigit is correct. Sneakily, return 'nil' for

values that don't even look like ISBNs, and 'false' for those that look possible but
don't normalize / have bad checkdigits
# File lib/library_stdnums.rb, line 240
def self.valid? issn, preprocessed = false
  issn = reduce_to_basics issn, 8 unless preprocessed
  return nil unless issn
  return issn[-1..-1] == self.checkdigit(issn, true)
end