IsbnUtils 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/IsbnUtils.rb +77 -0
  2. metadata +45 -0
data/lib/IsbnUtils.rb ADDED
@@ -0,0 +1,77 @@
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
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: IsbnUtils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Prasanth
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! " simple\tgem"
15
+ email: prasanthmp500@yahoo.ie
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/IsbnUtils.rb
21
+ homepage: http://rubygems.org/
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Gems to check if isbn is valid also for conversion of isbns!
45
+ test_files: []