IsbnUtils 0.0.0 → 0.0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ require './IsbnValidator'
2
+ class Isbn10DigitValidator < IsbnValidator
3
+
4
+ def initialize(isbn10)
5
+ @isbn = isbn10
6
+ end
7
+
8
+ def checkDigit
9
+ @strippedIsbn = @isbn.gsub(/[^0-9a-z]/i,'')
10
+ if @strippedIsbn.length != 10
11
+ raise "error not an isbn"
12
+ end
13
+
14
+ end
15
+
16
+
17
+ def validateIsbn
18
+ a = 0
19
+ for i in 0..9
20
+ if (@strippedIsbn[i] == "X" || @strippedIsbn[i] == "x")
21
+ a += 10 *(10-i)
22
+ elsif @strippedIsbn[i].match(/^(\d)+$/)
23
+ a += @strippedIsbn[i].to_i * (10-i)
24
+ else
25
+ return false
26
+ end
27
+ end
28
+ return ( a%11 == 0 )
29
+ end
30
+
31
+ end
@@ -0,0 +1,56 @@
1
+ require './Isbn13To10DigitConverter'
2
+
3
+ class Isbn10To13DigitConverter
4
+
5
+ def initialize(isbn)
6
+ @isbn = isbn
7
+ end
8
+
9
+ def set13To10DigitConverter(isbn13To10Converter)
10
+ @isbn13To10Converter = isbn13To10Converter
11
+ end
12
+
13
+
14
+ def process
15
+
16
+ @strippedIsbn = @isbn.gsub(/[^0-9a-z]/i,'')
17
+
18
+ if( @strippedIsbn.length() == 10 )
19
+ return convert(@strippedIsbn)
20
+ else (@strippedIsbn.length() == 13 )
21
+ @isbn13To10Converter.process
22
+ end
23
+
24
+ end
25
+
26
+
27
+ def convert (isbn10)
28
+ isbn10ToIsbn13(isbn10)
29
+ end
30
+
31
+ # The method to convert an 10 digit ISBN to 13 digit
32
+ # The method takes a 10 digit isbn and append "978" to the first 9 characters of the isbn and generate the 13 digit
33
+
34
+ def isbn10ToIsbn13(isbn10)
35
+
36
+ if isbn10.length() !=10
37
+ raise "The argument length should be 10"
38
+ end
39
+
40
+ isbn13 = "978" + isbn10.chomp("-");
41
+ return isbn13.slice(0, isbn13.length()-1) + getIsbn13CheckSum(isbn13).to_s;
42
+ end
43
+
44
+
45
+ def getIsbn13CheckSum(isbn13)
46
+ sum = 0
47
+ isbnFactorPattern = [1,3,1,3,1,3,1,3,1,3,1,3]
48
+ for i in 0..11
49
+ sum = sum + isbnFactorPattern[i] * isbn13[i].to_i
50
+ end
51
+ value = sum % 10
52
+ lastDigitIsbn = 10 - value
53
+ return lastDigitIsbn
54
+ end
55
+
56
+ end
@@ -0,0 +1,28 @@
1
+ require './IsbnValidator'
2
+
3
+ class Isbn13DigitValidator < IsbnValidator
4
+
5
+ def initialize(isbn13)
6
+ @isbn = isbn13
7
+ end
8
+
9
+
10
+ def isIsbn13Valid
11
+ check = 0
12
+ i = 0
13
+ while i < 12 do
14
+ check = check + @isbn[i].to_i
15
+ i = i + 2
16
+ end
17
+
18
+ i = 1
19
+ while i < 12 do
20
+ check = check + 3* @isbn[i].to_i
21
+ i = i + 2
22
+ end
23
+ check = check + @isbn[12].to_i
24
+ return check % 10 == 0;
25
+ end
26
+
27
+
28
+ end
@@ -0,0 +1,44 @@
1
+ require './Isbn10To13DigitConverter'
2
+
3
+ class Isbn13To10DigitConverter
4
+
5
+ def initialize(isbn)
6
+ @isbn = isbn
7
+ end
8
+
9
+ def set10To13DigitConverter(isbn10To13Converter)
10
+ @isbn10To13Converter = isbn10To13Converter
11
+ end
12
+
13
+
14
+ def process
15
+ @strippedIsbn = @isbn.gsub(/[^0-9a-z]/i,'')
16
+ if( @strippedIsbn.length() == 13 )
17
+ return convert(@strippedIsbn)
18
+ else (@strippedIsbn.length() == 10 )
19
+ @isbn10To13Converter.process
20
+ end
21
+ end
22
+
23
+
24
+ def convert(isbn13)
25
+ isbn10 = isbn13[3,9]
26
+ checksum = 0
27
+ weight = 10
28
+ isbn10.each_char do |c|
29
+ checksum += (c.to_i * weight)
30
+ weight -= 1
31
+ end
32
+ checksum = 11-(checksum % 11)
33
+ if (checksum == 10)
34
+ isbn10 += "X"
35
+ elsif (checksum == 11)
36
+ isbn10 += "0"
37
+ else
38
+ isbn10 += checksum.to_s
39
+ end
40
+ isbn10
41
+ end
42
+
43
+
44
+ end
@@ -0,0 +1,22 @@
1
+ require './Isbn13To10DigitConverter'
2
+ require './Isbn10To13DigitConverter'
3
+
4
+ #
5
+ # The IsbnConverter class has a toggle method that returns 13 digit isbn if 10 digit is input
6
+ # and vice versa
7
+ # IsbnConverter.toggle("1402894627") returns "9781402894626"
8
+ # IsbnConverter.toggle("9781402894626") returns "1402894627"
9
+ # This Class uses the concept of Chain of Responsibility.
10
+ #
11
+ class IsbnConverter
12
+
13
+ def self.toggle(isbn)
14
+ isbn10To13Converter = Isbn10To13DigitConverter.new(isbn)
15
+ isbn13To10Converter = Isbn13To10DigitConverter.new(isbn)
16
+ isbn10To13Converter.set13To10DigitConverter(isbn13To10Converter)
17
+ isbn13To10Converter.set10To13DigitConverter(isbn10To13Converter)
18
+ return isbn10To13Converter.process()
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,19 @@
1
+ #
2
+ # The IsbnValidator class defines a template method called validate , which represent
3
+ # a workflow to follow. The validate method calls checkDigit and validateIsbn methods
4
+ # which have its custom implementation in the subclasses.
5
+ #
6
+
7
+ class IsbnValidator
8
+
9
+ METHOD_MISSING = "IsbnConverter SYSTEM ERROR: method missing"
10
+ def checkDigit; raise METHOD_MISSING; end
11
+ def validateIsbn; raise METHOD_MISSING; end
12
+
13
+ def validate
14
+ checkDigit()
15
+ result = validateIsbn()
16
+ return result
17
+ end
18
+
19
+ end
@@ -0,0 +1,27 @@
1
+ require './IsbnValidator'
2
+ require './Isbn10DigitValidator'
3
+ require './Isbn13DigitValidator'
4
+
5
+ #
6
+ # The IsbnValidatorFactory is a validator factory class that returns either an object of Isbn10DigitValidator or Isbn13DigitValidator
7
+ #
8
+
9
+ class IsbnValidatorFactory
10
+
11
+ def self.getIsbnValidator(isbn)
12
+
13
+ strippedIsbn = isbn.gsub(/[^0-9a-z]/i,'')
14
+
15
+ if strippedIsbn.length == 10
16
+ return Isbn10DigitValidator.new(strippedIsbn)
17
+ elsif strippedIsbn.length == 14
18
+ return Isbn13DigitValidator.new(strippedIsbn)
19
+ else
20
+ raise "error Invalid Isbn"
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+
metadata CHANGED
@@ -1,25 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: IsbnUtils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Prasanth
8
+ - PrasanthMP
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-27 00:00:00.000000000 Z
12
+ date: 2013-12-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: ! " simple\tgem"
15
- email: prasanthmp500@yahoo.ie
14
+ description: Gem Utilities for ISBN validation and Isbn Conversion
15
+ email: prasanthmp500@gmail.com
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - lib/IsbnUtils.rb
21
- homepage: http://rubygems.org/
22
- licenses: []
20
+ - lib/Isbn10DigitValidator.rb
21
+ - lib/Isbn10To13DigitConverter.rb
22
+ - lib/Isbn13DigitValidator.rb
23
+ - lib/Isbn13To10DigitConverter.rb
24
+ - lib/IsbnConverter.rb
25
+ - lib/IsbnValidator.rb
26
+ - lib/IsbnValidatorFactory.rb
27
+ homepage: http://rubygems.org/gems
28
+ licenses:
29
+ - NCIRL
23
30
  post_install_message:
24
31
  rdoc_options: []
25
32
  require_paths:
@@ -41,5 +48,5 @@ rubyforge_project:
41
48
  rubygems_version: 1.8.24
42
49
  signing_key:
43
50
  specification_version: 3
44
- summary: Gems to check if isbn is valid also for conversion of isbns!
51
+ summary: The Gemfile containing Utilities for ISBN
45
52
  test_files: []
data/lib/IsbnUtils.rb DELETED
@@ -1,77 +0,0 @@
1
- class IsbnUtils
2
-
3
-
4
- def self.isIsbnValid(isbn)
5
-
6
- if isbn.length() == 10
7
- return isIsbn10Valid(isbn)
8
- elsif isbn.length() == 13
9
- return isIsbn13Valid(isbn)
10
- end
11
-
12
- return false
13
- end
14
-
15
- def self.isIsbn10Valid(isbn10)
16
- a = 0
17
- for i in 0..9
18
- if (isbn10[i] == "X" || isbn10[i] == "x")
19
- a += 10 *(10-i)
20
- elsif isbn10[i].match(/^(\d)+$/)
21
- a += isbn10[i].to_i * (10-i)
22
- else
23
- return false
24
- end
25
- end
26
-
27
- return ( a%11 == 0 )
28
- end
29
-
30
- def self.isIsbn13Valid(isbn13)
31
-
32
- check = 0
33
- i = 0
34
- while i < 12 do
35
- check = check + isbn13[i].to_i
36
- i = i + 2
37
- end
38
-
39
- i = 1
40
- while i < 12 do
41
- check = check + 3*isbn13[i].to_i
42
- i = i + 2
43
- end
44
- check = check + isbn13[12].to_i
45
- return check % 10 == 0;
46
- end
47
-
48
- # The method to convert an 10 digit ISBN to 13 digit
49
- # The method takes a 10 digit isbn and append "978" to the first 9 characters of the isbn and generate the 13 digit
50
-
51
- def self.isbn10ToIsbn13(isbn10)
52
-
53
- if isbn10.length() !=10
54
- raise "The argument length should be 10"
55
- end
56
-
57
- isbn13 = "978" + isbn10.chomp("-");
58
- return isbn13.slice(0, isbn13.length()-1) + getIsbn13CheckSum(isbn13).to_s;
59
- end
60
-
61
-
62
- def self.getIsbn13CheckSum(isbn13)
63
- sum = 0
64
- isbnFactorPattern = [1,3,1,3,1,3,1,3,1,3,1,3]
65
- for i in 0..11
66
- sum = sum + isbnFactorPattern[i] * isbn13[i].to_i
67
- end
68
- value = sum % 10
69
- lastDigitIsbn = 10 - value
70
- return lastDigitIsbn
71
- end
72
-
73
- end
74
-
75
-
76
- # puts IsbnUtils.isIsbnValid("9783161484190")
77
-