-
Notifications
You must be signed in to change notification settings - Fork 170
/
bookland.rb
38 lines (29 loc) · 907 Bytes
/
bookland.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#encoding: ASCII
require 'barby/barcode/ean_13'
module Barby
#Bookland barcodes are EAN-13 barcodes with number system
#978 (hence "Bookland"). The data they encode is an ISBN
#with its check digit removed. This is a convenience class
#that takes an ISBN no instead of "pure" EAN-13 data.
class Bookland < EAN13
BOOKLAND_NUMBER_SYSTEM = '978'
attr_accessor :isbn
def initialize(isbn)
self.isbn = isbn
raise ArgumentError, 'data not valid' unless valid?
end
def data
BOOKLAND_NUMBER_SYSTEM+isbn_only
end
#Removes any non-digit characters, number system and check digit
#from ISBN, so "978-82-92526-14-9" would result in "829252614"
def isbn_only
s = isbn.gsub(/[^0-9]/, '')
if s.size > 10#Includes number system
s[3,9]
else#No number system, may include check digit
s[0,9]
end
end
end
end