Module: StdNum::ISSN

Extended by:
Helpers
Defined in:
lib/library_stdnums.rb

Overview

Validate and and normalize ISSNs

Constant Summary

Constants included from Helpers

Helpers::STDNUMPAT, Helpers::STDNUMPAT_MULTIPLE

Class Method Summary collapse

Methods included from Helpers

extractNumber, extract_multiple_numbers, reduce_to_basics

Class Method Details

.at_least_trying?(issn) ⇒ Boolean

Does it even look like an ISSN?

Returns:

  • (Boolean)


196
197
198
# File 'lib/library_stdnums.rb', line 196

def self.at_least_trying? issn
  return !(reduce_to_basics(issn, 8))
end

.checkdigit(issn, preprocessed = false) ⇒ String

Compute the checkdigit of an ISSN

Parameters:

  • issn (String)

    The ISSN (we'll try to clean it up if possible)

  • preprocessed (Boolean) (defaults to: false)

    Set to true if the number has already been through reduce_to_basic

Returns:

  • (String)

    the one-character checkdigit



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/library_stdnums.rb', line 206

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) ⇒ String?

Make sure it's valid, remove the dashes, uppercase the X, and return

Parameters:

  • rawissn (String)

    The ISSN to normalize

Returns:

  • (String, nil)

    the normalized ISSN, or nil on failure



240
241
242
243
244
245
246
247
# File 'lib/library_stdnums.rb', line 240

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) ⇒ Boolean

Check to see if the checkdigit is correct

Parameters:

  • issn (String)

    The ISSN (we'll try to clean it up if possible)

  • preprocessed (Boolean) (defaults to: false)

    Set to true if the number has already been through reduce_to_basic

Returns:

  • (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



229
230
231
232
233
# File 'lib/library_stdnums.rb', line 229

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