isbn10 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +36 -0
- data/lib/isbn10.rb +56 -0
- data/spec/isbn10_spec.rb +37 -0
- metadata +69 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Peter Yandell
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
A small class for generating and validating 10's, the 10 digit codes that are found
|
2
|
+
on many book-ish products sold before 2005.
|
3
|
+
|
4
|
+
Yes there are man other ISBN gems out there. I wanted one that has a similar API to
|
5
|
+
my other identifier related gems (ean13, abn, san, upc, etc) to reduce the
|
6
|
+
number of APIs I have to remember.
|
7
|
+
|
8
|
+
= Installation
|
9
|
+
|
10
|
+
gem install isbn10
|
11
|
+
|
12
|
+
= Usage
|
13
|
+
|
14
|
+
ISBN10.new("0140449043").valid?
|
15
|
+
=> true
|
16
|
+
|
17
|
+
ISBN10.valid?("0140449043")
|
18
|
+
=> true
|
19
|
+
|
20
|
+
ISBN10.valid?("0140449042")
|
21
|
+
=> false
|
22
|
+
|
23
|
+
ISBN10.complete("014044904")
|
24
|
+
=> "0140449043"
|
25
|
+
|
26
|
+
ISBN10.new("0140449043").to_ean
|
27
|
+
=> "9780140449044"
|
28
|
+
|
29
|
+
= Further Reader
|
30
|
+
|
31
|
+
- http://en.wikipedia.org/wiki/International_Standard_Book_Number
|
32
|
+
|
33
|
+
= Contributing
|
34
|
+
|
35
|
+
Source code is publicly available @ http://github.com/yob/isbn10/tree/master.
|
36
|
+
Patches welcome, preferably via a git repo I can pull from.
|
data/lib/isbn10.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'ean13'
|
4
|
+
|
5
|
+
class ISBN10
|
6
|
+
|
7
|
+
class Version #:nodoc:
|
8
|
+
Major = 1
|
9
|
+
Minor = 0
|
10
|
+
Tiny = 0
|
11
|
+
|
12
|
+
String = [Major, Minor, Tiny].join('.')
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(str)
|
16
|
+
@number = str.to_s.gsub("-","")
|
17
|
+
end
|
18
|
+
|
19
|
+
def valid?
|
20
|
+
ISBN10.valid? @number
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.valid?(isbn)
|
24
|
+
isbn = isbn.to_s.gsub("-","")
|
25
|
+
isbn.length == 10 && isbn == ISBN10.complete(isbn[0,9])
|
26
|
+
end
|
27
|
+
|
28
|
+
# Purely for generating new ean numbers
|
29
|
+
def self.complete(nine)
|
30
|
+
nine = nine.to_s
|
31
|
+
return nil unless nine.length == 9 && nine.match(/\d{9}/)
|
32
|
+
|
33
|
+
check = nine[0,1].to_i
|
34
|
+
check = check + nine[1,1].to_i * 2
|
35
|
+
check = check + nine[2,1].to_i * 3
|
36
|
+
check = check + nine[3,1].to_i * 4
|
37
|
+
check = check + nine[4,1].to_i * 5
|
38
|
+
check = check + nine[5,1].to_i * 6
|
39
|
+
check = check + nine[6,1].to_i * 7
|
40
|
+
check = check + nine[7,1].to_i * 8
|
41
|
+
check = check + nine[8,1].to_i * 9
|
42
|
+
check = check % 11
|
43
|
+
|
44
|
+
if check == 10
|
45
|
+
"#{nine}X"
|
46
|
+
else
|
47
|
+
"#{nine}#{check}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_ean
|
52
|
+
return nil unless self.valid?
|
53
|
+
EAN13.complete("978#{@number[0,9]}")
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/spec/isbn10_spec.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
$LOAD_PATH << File.dirname(__FILE__) + "/../lib"
|
2
|
+
|
3
|
+
require 'spec'
|
4
|
+
require 'isbn10'
|
5
|
+
|
6
|
+
describe ISBN10 do
|
7
|
+
it "should identify a valid ISBN10" do
|
8
|
+
ISBN10.new("0140449043").valid?.should be_true
|
9
|
+
ISBN10.new("0-140-44904-3").valid?.should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should identify a valid ISBN10" do
|
13
|
+
ISBN10.valid?("0140449043").should be_true
|
14
|
+
ISBN10.valid?("0-140-44904-3").should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should identify an invalid ISBN10" do
|
18
|
+
ISBN10.valid?(nil).should be_false
|
19
|
+
ISBN10.valid?("0-1404-4904-2").should be_false
|
20
|
+
ISBN10.valid?("0140449042").should be_false
|
21
|
+
ISBN10.valid?("978184354916").should be_false
|
22
|
+
ISBN10.valid?(Array).should be_false
|
23
|
+
ISBN10.valid?(Array.new).should be_false
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should calculate a ISBN10 check digit correctly" do
|
27
|
+
ISBN10.complete("014044904").should eql("0140449043")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should convert to an EAN correctly" do
|
31
|
+
ISBN10.new("0140449042").to_ean.should be_nil
|
32
|
+
isbn = ISBN10.new("0140449043")
|
33
|
+
isbn.valid?.should be_true
|
34
|
+
isbn.to_ean.should eql("9780140449044")
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: isbn10
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Healy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-18 00:00:00 +11:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ean13
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: a (very) small library for working with ISBN10 codes
|
26
|
+
email: jimmy@deefa.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/isbn10.rb
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.rdoc
|
37
|
+
- CHANGELOG
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/yob/isbn10/tree/master
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --title
|
45
|
+
- ISBN10
|
46
|
+
- --line-numbers
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: a (very) small library for working with ISBN10 codes
|
68
|
+
test_files:
|
69
|
+
- spec/isbn10_spec.rb
|