entangledstate-isbn 1.0.0
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/VERSION.yml +4 -0
- data/lib/isbn.rb +56 -0
- data/test/isbn_test.rb +7 -0
- data/test/test_helper.rb +7 -0
- metadata +56 -0
data/VERSION.yml
ADDED
data/lib/isbn.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module ISBN
|
2
|
+
extend self
|
3
|
+
|
4
|
+
def calculate(isbn)
|
5
|
+
isbn = isbn.delete("-")
|
6
|
+
isbn = isbn[0...-1] unless (isbn.size == 9 || isbn.size == 12)
|
7
|
+
case isbn.size
|
8
|
+
when 9
|
9
|
+
weight = (2..10).to_a.reverse
|
10
|
+
mod = 11
|
11
|
+
check = 'X'
|
12
|
+
when 12
|
13
|
+
weight = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
|
14
|
+
mod = 10
|
15
|
+
check = '0'
|
16
|
+
else
|
17
|
+
raise InvalidISBNError
|
18
|
+
end
|
19
|
+
case check_digit = (mod - (isbn.chars.zip(weight).inject(0) {|s,i| s += i[0].to_i * i[1]} % mod))
|
20
|
+
when 10 then isbn << check
|
21
|
+
when 11 then isbn << '0'
|
22
|
+
else isbn << check_digit.to_s
|
23
|
+
end
|
24
|
+
isbn
|
25
|
+
end
|
26
|
+
|
27
|
+
def from_13_to_10(isbn)
|
28
|
+
isbn = isbn.delete("-")
|
29
|
+
raise "NOT 13 Digit ISBN" if isbn.size != 13
|
30
|
+
calculate(isbn[/\d{3}(\d{9})(?:\d|X)/i, 1])
|
31
|
+
end
|
32
|
+
|
33
|
+
def from_10_to_13(isbn, used=false)
|
34
|
+
isbn = isbn.delete("-")
|
35
|
+
raise "NOT 10 Digit ISBN" if isbn.size != 10
|
36
|
+
calculate("#{used ? '290' : '978'}#{isbn}")
|
37
|
+
end
|
38
|
+
|
39
|
+
def between_new_and_used(isbn)
|
40
|
+
case isbn[0..2]
|
41
|
+
when '978' then calculate(isbn.sub(/^978/, "290"))
|
42
|
+
when '290' then calculate(isbn.sub(/^290/, "978"))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid?(isbn)
|
47
|
+
begin
|
48
|
+
isbn[-1,1] == calculate(isbn)[-1,1]
|
49
|
+
rescue InvalidISBNError => isbn_error
|
50
|
+
false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class InvalidISBNError < RuntimeError
|
55
|
+
end
|
56
|
+
end
|
data/test/isbn_test.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: entangledstate-isbn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Kersey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-13 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: library to transform ISBN's from new to used, between 10 and 13, etc...
|
17
|
+
email: entangledstate@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- lib/isbn.rb
|
27
|
+
- test/isbn_test.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
has_rdoc: false
|
30
|
+
homepage: http://github.com/entangledstate/isbn
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.2.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: a simple library of functions on ISBN's
|
55
|
+
test_files: []
|
56
|
+
|