Module: StdNum::ISSN
- Extended by:
- Helpers
- Defined in:
- lib/library_stdnums.rb
Overview
Validate and and normalize ISSNs
Constant Summary
Constants included from Helpers
Class Method Summary collapse
-
.at_least_trying?(isbn) ⇒ Boolean
Does it even look like an ISSN?.
-
.checkdigit(issn, preprocessed = false) ⇒ String
Compute the checkdigit of an ISSN.
-
.normalize(rawissn) ⇒ String?
Make sure it's valid, remove the dashes, uppercase the X, and return.
-
.valid?(issn, preprocessed = false) ⇒ Boolean
Check to see if the checkdigit is correct.
Methods included from Helpers
extractNumber, reduce_to_basics
Class Method Details
.at_least_trying?(isbn) ⇒ Boolean
Does it even look like an ISSN?
184 185 186 |
# File 'lib/library_stdnums.rb', line 184 def self. isbn return !(reduce_to_basics(issn, 8)) end |
.checkdigit(issn, preprocessed = false) ⇒ String
Compute the checkdigit of an ISSN
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/library_stdnums.rb', line 194 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
228 229 230 231 232 233 234 235 |
# File 'lib/library_stdnums.rb', line 228 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
217 218 219 220 221 |
# File 'lib/library_stdnums.rb', line 217 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 |