IsbnUtils 0.0.0.2 → 0.0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/Isbn10DigitValidator.rb +1 -1
- data/lib/Isbn13DigitValidator.rb +12 -4
- data/lib/IsbnValidatorFactory.rb +6 -7
- metadata +1 -1
data/lib/Isbn10DigitValidator.rb
CHANGED
data/lib/Isbn13DigitValidator.rb
CHANGED
@@ -6,22 +6,30 @@ class Isbn13DigitValidator < IsbnValidator
|
|
6
6
|
def initialize(isbn13)
|
7
7
|
@isbn = isbn13
|
8
8
|
end
|
9
|
+
|
10
|
+
def checkDigit
|
11
|
+
@strippedIsbn = @isbn.gsub(/[^0-9a-z]/i,'')
|
12
|
+
if @strippedIsbn.length != 13
|
13
|
+
raise "error not a 13 digit isbn"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
9
17
|
|
10
18
|
|
11
|
-
def
|
19
|
+
def validateIsbn
|
12
20
|
check = 0
|
13
21
|
i = 0
|
14
22
|
while i < 12 do
|
15
|
-
check = check + @
|
23
|
+
check = check + @strippedIsbn[i].to_i
|
16
24
|
i = i + 2
|
17
25
|
end
|
18
26
|
|
19
27
|
i = 1
|
20
28
|
while i < 12 do
|
21
|
-
check = check + 3* @
|
29
|
+
check = check + 3* @strippedIsbn[i].to_i
|
22
30
|
i = i + 2
|
23
31
|
end
|
24
|
-
check = check + @
|
32
|
+
check = check + @strippedIsbn[12].to_i
|
25
33
|
return check % 10 == 0;
|
26
34
|
end
|
27
35
|
|
data/lib/IsbnValidatorFactory.rb
CHANGED
@@ -1,25 +1,24 @@
|
|
1
|
-
|
2
|
-
|
3
1
|
#
|
4
2
|
# The IsbnValidatorFactory is a validator factory class that returns either an object of Isbn10DigitValidator or Isbn13DigitValidator
|
5
|
-
#
|
3
|
+
# IsbnValidatorFactory.getIsbnValidator("1402894627").validate() == true
|
4
|
+
# IsbnValidatorFactory.getIsbnValidator("1402894627").validate() == true
|
5
|
+
#
|
6
|
+
|
6
7
|
require 'IsbnValidator'
|
7
8
|
require 'Isbn10DigitValidator'
|
8
9
|
require 'Isbn13DigitValidator'
|
9
10
|
class IsbnValidatorFactory
|
10
11
|
|
11
|
-
|
12
|
-
|
13
12
|
def self.getIsbnValidator(isbn)
|
14
13
|
|
15
14
|
strippedIsbn = isbn.gsub(/[^0-9a-z]/i,'')
|
16
15
|
|
17
16
|
if strippedIsbn.length == 10
|
18
17
|
return Isbn10DigitValidator.new(strippedIsbn)
|
19
|
-
elsif strippedIsbn.length ==
|
18
|
+
elsif strippedIsbn.length == 13
|
20
19
|
return Isbn13DigitValidator.new(strippedIsbn)
|
21
20
|
else
|
22
|
-
|
21
|
+
return false
|
23
22
|
end
|
24
23
|
|
25
24
|
end
|