Class: RBook::ISBN
- Inherits:
-
Object
- Object
- RBook::ISBN
- Defined in:
- lib/rbook/isbn.rb
Overview
A collection of useful functions for dealing with ISBNs. Each function can be used directly - examples listed below. To use these examples you will need the following 2 lines at the top of your script:
require 'rubygems'
require 'rbook/isbn'
Class Method Summary collapse
-
.add_groups(isbn) ⇒ Object
adds hyphens to a valid isbn in the appropriate spots.
-
.calc_isbn10_check(isbn) ⇒ Object
Attempts to calculate the check digit on a supplied 9 digit string.
-
.calc_isbn13_check(isbn) ⇒ Object
Attempts to calculate the check digit on a supplied 12 digit string.
-
.convert_to_isbn10(convert_me) ⇒ Object
Attempts to convert a valid isbn13 into an isbn10.
-
.convert_to_isbn13(convert_me) ⇒ Object
Attempts to convert the supplied string into an ISBN13.
-
.valid_isbn10?(isbn) ⇒ Boolean
returns true if the supplied string is a valid isbn10.
-
.valid_isbn13?(isbn) ⇒ Boolean
returns true if the supplied string is a valid isbn13.
-
.valid_isbn?(isbn) ⇒ Boolean
Returns true if the supplied string is a valid isbn10 or isbn13.
Class Method Details
.add_groups(isbn) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rbook/isbn.rb', line 16 def self.add_groups(isbn) return nil unless isbn.class == String if valid_isbn10?(isbn) isbn = isbn.gsub("-","") isbn = isbn.insert(1, "-") isbn = isbn.insert(7, "-") isbn = isbn.insert(11, "-") return isbn elsif valid_isbn13?(isbn) isbn = isbn.gsub("-","") isbn = isbn.insert(3, "-") isbn = isbn.insert(5, "-") isbn = isbn.insert(11, "-") isbn = isbn.insert(15, "-") return isbn else return isbn end end |
.calc_isbn10_check(isbn) ⇒ Object
Attempts to calculate the check digit on a supplied 9 digit string.
If passed nil or a non string, nil is returned.
If the supplied string length isn’t chars 9 long, nil is returned.
If the supplied string is 9 chars long, the check digit is calculated and returned
ISBN.calc_isbn10_check("020161622")
#=> X
ISBN.calc_isbn10_check("020161622X")
#=> nil
ISBN.calc_isbn10_check("9780201616224")
#=> nil
ISBN.calc_isbn10_check("9780201616223")
#=> nil
ISBN.calc_isbn10_check("not an isbn")
#=> nil
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rbook/isbn.rb', line 117 def self.calc_isbn10_check(isbn) if isbn.nil? || isbn.class != String || isbn.length != 9 return nil else check = isbn[0,1].to_i check = check + isbn[1,1].to_i * 2 check = check + isbn[2,1].to_i * 3 check = check + isbn[3,1].to_i * 4 check = check + isbn[4,1].to_i * 5 check = check + isbn[5,1].to_i * 6 check = check + isbn[6,1].to_i * 7 check = check + isbn[7,1].to_i * 8 check = check + isbn[8,1].to_i * 9 check = check % 11 if check == 10 return "X" else return check.to_s end end end |
.calc_isbn13_check(isbn) ⇒ Object
Attempts to calculate the check digit on a supplied 12 digit string.
If passed nil or a non String, nil is returned.
If the supplied string isn’t 12 digits long, nil is returned.
For all other strings the check digit will be returned
ISBN.calc_isbn13_check("978020161622")
#=> 4
ISBN.calc_isbn13_check("9780201616224")
#=> nil
ISBN.calc_isbn13_check("020161622X")
#=> nil
ISBN.calc_isbn13_check("9780201616223")
#=> nil
ISBN.calc_isbn13_check("not an isbn")
#=> nil
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/rbook/isbn.rb', line 158 def self.calc_isbn13_check(isbn) if isbn.nil? || isbn.class != String || isbn.length != 12 return nil else checkdigit = isbn[0,1].to_i * 1 checkdigit = checkdigit + isbn[1,1].to_i * 3 checkdigit = checkdigit + isbn[2,1].to_i * 1 checkdigit = checkdigit + isbn[3,1].to_i * 3 checkdigit = checkdigit + isbn[4,1].to_i * 1 checkdigit = checkdigit + isbn[5,1].to_i * 3 checkdigit = checkdigit + isbn[6,1].to_i * 1 checkdigit = checkdigit + isbn[7,1].to_i * 3 checkdigit = checkdigit + isbn[8,1].to_i * 1 checkdigit = checkdigit + isbn[9,1].to_i * 3 checkdigit = checkdigit + isbn[10,1].to_i * 1 checkdigit = checkdigit + isbn[11,1].to_i * 3 remainder = checkdigit % 10 if remainder == 0 checkdigit = 0 else checkdigit = (10 - remainder) end return checkdigit.to_s end end |
.convert_to_isbn10(convert_me) ⇒ Object
Attempts to convert a valid isbn13 into an isbn10.
If supplied string is a valid isbn10, it is returned unchanged.
If if the string is a valid isbn13 and starts with 978, it is converted to an isbn10.
All other strings return nil.
ISBN.convert_to_isbn10("9780201616224")
#=> 020161622X
ISBN.convert_to_isbn10("020161622X")
#=> 020161622X
ISBN.convert_to_isbn10("9793472834833")
#=> nil
ISBN.convert_to_isbn10("not an isbn")
#=> nil
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/rbook/isbn.rb', line 204 def self.convert_to_isbn10(convert_me) return nil unless convert_me.class == String convert_me = convert_me.gsub("-","") if self.valid_isbn10?(convert_me) convert_me elsif self.valid_isbn13?(convert_me) && convert_me[0,3] == '978' convert_me[3,9] + self.calc_isbn10_check(convert_me[3,9]) else nil end end |
.convert_to_isbn13(convert_me) ⇒ Object
Attempts to convert the supplied string into an ISBN13.
If the string is a valid isbn13, it is returned unchanged.
If the string is a valid isbn10, it will be converted to isbn13.
All other strings will return nil.
ISBN.convert_to_isbn13("020161622X")
#=> 9780201616224
ISBN.convert_to_isbn13("9780201616224")
#=> 9780201616224
ISBN.convert_to_isbn13("9793472834833")
#=> nil
ISBN.convert_to_isbn13("not an isbn")
#=> nil
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/rbook/isbn.rb', line 232 def self.convert_to_isbn13(convert_me) return nil unless convert_me.class == String convert_me = convert_me.gsub("-","") if self.valid_isbn13?(convert_me) convert_me elsif self.valid_isbn10?(convert_me) '978'+convert_me[0,9] + self.calc_isbn13_check('978'+convert_me[0,9]) else nil end end |
.valid_isbn10?(isbn) ⇒ Boolean
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rbook/isbn.rb', line 60 def self.valid_isbn10?(isbn) return false unless isbn.class == String isbn = isbn.gsub("-","") if isbn.length != 10 false elsif isbn[/[^\dxX]/] false else checkdigit = self.calc_isbn10_check(isbn.to_s[0,9]) if checkdigit.eql?("X") isbn == isbn[0,9] + "x" || isbn == isbn[0,9] + "X" else isbn == isbn[0,9] + checkdigit end end end |
.valid_isbn13?(isbn) ⇒ Boolean
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rbook/isbn.rb', line 87 def self.valid_isbn13?(isbn) return false unless isbn.class == String isbn = isbn.gsub("-","") if isbn.length != 13 false elsif isbn[/[^\dxX]/] false else isbn == isbn[0,12] + self.calc_isbn13_check(isbn[0,12]) end end |
.valid_isbn?(isbn) ⇒ Boolean
46 47 48 |
# File 'lib/rbook/isbn.rb', line 46 def self.valid_isbn?(isbn) valid_isbn10?(isbn) || valid_isbn13?(isbn) end |