bookworm 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bookworm.gemspec +1 -1
- data/lib/bookworm.rb +17 -10
- data/test/bookworm_test.rb +8 -8
- metadata +1 -1
data/bookworm.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "bookworm"
|
6
|
-
s.version = "0.1.
|
6
|
+
s.version = "0.1.2"
|
7
7
|
s.authors = ["Tyler Johnston"]
|
8
8
|
s.email = ["tylerjohnst@gmail.com"]
|
9
9
|
s.homepage = "http://github.com/tylerjohnst/bookworm"
|
data/lib/bookworm.rb
CHANGED
@@ -45,8 +45,7 @@ class Bookworm
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def to_thirteen(identifier)
|
48
|
-
identifier
|
49
|
-
identifier + thirteen_check_digit(identifier)
|
48
|
+
thirteen_strip(identifier) + thirteen_check_digit(identifier)
|
50
49
|
end
|
51
50
|
|
52
51
|
def scrub(string)
|
@@ -54,21 +53,29 @@ class Bookworm
|
|
54
53
|
result ? result[1] : nil
|
55
54
|
end
|
56
55
|
|
57
|
-
def thirteen_check_digit(identifier)
|
58
|
-
sum = 0
|
56
|
+
def thirteen_check_digit(identifier=isbn)
|
57
|
+
identifier, sum = identifier.rjust(13,"978")[/(.+)\w/,1], 0
|
58
|
+
|
59
59
|
12.times do |index|
|
60
60
|
digit = identifier[index].to_i
|
61
61
|
sum += index.even? ? digit : digit * 3
|
62
62
|
end
|
63
|
+
|
63
64
|
checksum = (10 - sum % 10)
|
64
65
|
checksum == 10 ? '0' : checksum.to_s
|
65
66
|
end
|
66
67
|
|
67
|
-
def
|
68
|
+
def thirteen_strip(identifier)
|
69
|
+
identifier.rjust(13,"978")[0..-2]
|
70
|
+
end
|
71
|
+
|
72
|
+
def ten_check_digit(identifier=isbn)
|
68
73
|
sum = 0
|
74
|
+
|
69
75
|
9.times do |index|
|
70
76
|
sum += (10 - index) * identifier[index].to_i
|
71
77
|
end
|
78
|
+
|
72
79
|
checksum = 11 - sum % 11
|
73
80
|
|
74
81
|
case checksum
|
@@ -79,10 +86,10 @@ class Bookworm
|
|
79
86
|
end
|
80
87
|
|
81
88
|
def is_valid?
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
89
|
+
case original
|
90
|
+
when /^\d{13}/ then original[12] == thirteen_check_digit(original)
|
91
|
+
when /^\d{10}/ then original[9] == ten_check_digit(original)
|
92
|
+
else false
|
93
|
+
end
|
87
94
|
end
|
88
95
|
end
|
data/test/bookworm_test.rb
CHANGED
@@ -10,21 +10,21 @@ class BookwormTest < Test::Unit::TestCase
|
|
10
10
|
@bookworm.scrub('9780976805427999990')
|
11
11
|
end
|
12
12
|
|
13
|
-
def test_should_create_from_used_and_store_as_new
|
14
|
-
assert_equal '9780976805427', Bookworm.new("2900976805426").isbn
|
15
|
-
end
|
16
|
-
|
17
13
|
def test_should_calculate_thirteen_check_digit
|
18
|
-
assert_equal "6", @bookworm.thirteen_check_digit('
|
14
|
+
assert_equal "6", @bookworm.thirteen_check_digit('2900976805426')
|
19
15
|
end
|
20
16
|
|
21
17
|
def test_should_calculate_ten_check_digit
|
22
|
-
assert_equal "1", @bookworm.ten_check_digit("
|
18
|
+
assert_equal "1", @bookworm.ten_check_digit("0976805421")
|
23
19
|
end
|
24
20
|
|
25
|
-
def
|
21
|
+
def test_should_create_from_used
|
22
|
+
assert_equal '9780976805427', Bookworm.new("2900976805426").isbn
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_convert_from_ten
|
26
26
|
assert_equal '9780976805427', Bookworm.new("0976805421").as_new
|
27
|
-
assert_equal '', Bookworm.new("0073324922").as_new
|
27
|
+
assert_equal '9780073324920', Bookworm.new("0073324922").as_new
|
28
28
|
end
|
29
29
|
|
30
30
|
def test_should_convert_to_new
|