bookworm 0.1.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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.md +21 -0
- data/Rakefile +10 -0
- data/bookworm.gemspec +21 -0
- data/lib/bookworm.rb +82 -0
- data/test/bookworm_test.rb +76 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2011 Tyler Johnston
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Bookworm
|
2
|
+
|
3
|
+
Your wiggling friend in ISBN conversions.
|
4
|
+
|
5
|
+
```
|
6
|
+
@book = Bookworm.new('9780980011111')
|
7
|
+
@book.as_new
|
8
|
+
> "9780980011111"
|
9
|
+
@book.as_used
|
10
|
+
> "2900980011110"
|
11
|
+
@book.as_ten
|
12
|
+
> "0980011116"
|
13
|
+
@book.type
|
14
|
+
> 'new'
|
15
|
+
@book.valid?
|
16
|
+
> true
|
17
|
+
```
|
18
|
+
|
19
|
+
# Copyright
|
20
|
+
|
21
|
+
Copyright (c) 2011 Tyler Johnston. See LICENSE for details.
|
data/Rakefile
ADDED
data/bookworm.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "bookworm"
|
6
|
+
s.version = "0.1.0"
|
7
|
+
s.authors = ["Tyler Johnston"]
|
8
|
+
s.email = ["tylerjohnst@gmail.com"]
|
9
|
+
s.homepage = "http://github.com/tylerjohnst/bookworm"
|
10
|
+
s.summary = %q{ISBN manipulation, conversion, and inspection for Ruby.}
|
11
|
+
s.description = %q{Convert from 13 digit to 10 digit, from new to used, and back again. Detect used and new addendums in ISBN strings..}
|
12
|
+
|
13
|
+
s.rubyforge_project = "bookworm"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "rake"
|
21
|
+
end
|
data/lib/bookworm.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
class Bookworm
|
2
|
+
attr_reader :isbn, :original
|
3
|
+
|
4
|
+
def initialize(identifier)
|
5
|
+
@original = identifier.to_s
|
6
|
+
@isbn = as_new(identifier.to_s)
|
7
|
+
@type = type
|
8
|
+
end
|
9
|
+
|
10
|
+
def as_new(identifier=isbn)
|
11
|
+
return nil unless identifier
|
12
|
+
identifier = scrub(identifier)
|
13
|
+
case identifier
|
14
|
+
when /^290/ then to_thirteen("978#{identifier[3..-1]}")
|
15
|
+
when /^291/ then to_thirteen("979#{identifier[3..-1]}")
|
16
|
+
when /^979|978|\d{10}/ then to_thirteen(identifier)
|
17
|
+
else nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def as_used
|
22
|
+
return nil unless is_valid?
|
23
|
+
prefix = /^979/.match(isbn) ? "291" : "290"
|
24
|
+
to_thirteen(prefix + isbn[3..-1])
|
25
|
+
end
|
26
|
+
|
27
|
+
def as_ten
|
28
|
+
return nil if not is_valid? or isbn =~ /979/
|
29
|
+
identifier = isbn[3..-2]
|
30
|
+
identifier + ten_check_digit(identifier)
|
31
|
+
end
|
32
|
+
|
33
|
+
def type
|
34
|
+
addendum = case original
|
35
|
+
when /^(?:978|979)\d{10}/
|
36
|
+
addendum = /^(?:978|979)\d{10}(\d{5})$/.match(original).to_a.last
|
37
|
+
addendum == '90000' ? 'used' : 'new'
|
38
|
+
when /^(?:290|291)\d{10}/
|
39
|
+
addendum = /^(?:290|291)\d{10}(\d{5})$/.match(original).to_a.last
|
40
|
+
addendum == '99990' ? 'new' : 'used'
|
41
|
+
when /^\d{10}$/
|
42
|
+
'new'
|
43
|
+
else nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_thirteen(identifier)
|
48
|
+
identifier = strip_last(identifier)
|
49
|
+
identifier + thirteen_check_digit(identifier)
|
50
|
+
end
|
51
|
+
|
52
|
+
def scrub(string)
|
53
|
+
result = /^((?:978|979|291|290)\d{10}|\d{10}).*/.match(string.delete('-'))
|
54
|
+
result ? result[1] : nil
|
55
|
+
end
|
56
|
+
|
57
|
+
def thirteen_check_digit(identifier)
|
58
|
+
sum = 0
|
59
|
+
12.times do |index|
|
60
|
+
digit = identifier[index].to_i
|
61
|
+
sum += index.even? ? digit : digit * 3
|
62
|
+
end
|
63
|
+
(10 - sum % 10).to_s
|
64
|
+
end
|
65
|
+
|
66
|
+
def ten_check_digit(identifier)
|
67
|
+
sum = 0
|
68
|
+
9.times do |index|
|
69
|
+
sum += (10 - index) * identifier[index].to_i
|
70
|
+
end
|
71
|
+
checksum = 11 - sum % 11
|
72
|
+
checksum == 10 ? 'X' : checksum.to_s
|
73
|
+
end
|
74
|
+
|
75
|
+
def is_valid?
|
76
|
+
!!(isbn && isbn[-1] == original[-1])
|
77
|
+
end
|
78
|
+
|
79
|
+
def strip_last(string)
|
80
|
+
string.rjust(13,"978")[/(.+)\w/,1]
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'bookworm'
|
3
|
+
|
4
|
+
class BookwormTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@bookworm = Bookworm.new('9780976805427')
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_should_scrub_inputs
|
10
|
+
@bookworm.scrub('9780976805427999990')
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_create_from_used_and_store_as_new
|
14
|
+
assert_equal '9780976805427', Bookworm.new("2900976805426").isbn
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_calculate_thirteen_check_digit
|
18
|
+
assert_equal "6", @bookworm.thirteen_check_digit('290097680542')
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_calculate_ten_check_digit
|
22
|
+
assert_equal "1", @bookworm.ten_check_digit("097680542")
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_convert_from_ten_to_thirteen
|
26
|
+
assert_equal '9780976805427', Bookworm.new("0976805421").as_new
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_convert_to_new
|
30
|
+
assert_equal "9780976805427", @bookworm.as_new
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_should_convert_to_used
|
34
|
+
assert_equal "2900976805426", @bookworm.as_used
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_should_convert_to_ten
|
38
|
+
assert_equal "0976805421", @bookworm.as_ten
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_should_convert_to_new_from_979
|
42
|
+
assert_equal "9790883590569", Bookworm.new('9790883590569').as_new
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_convert_to_used_from_979
|
46
|
+
assert_equal "2910883590568", Bookworm.new('9790883590569').as_used
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_not_convert_from_979_to_ten
|
50
|
+
assert_nil Bookworm.new('9790883590569').as_ten
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_mark_invalid_bad_isbns
|
54
|
+
assert_equal false, Bookworm.new('9790883590562').is_valid?
|
55
|
+
assert_equal false, Bookworm.new('herpderp').is_valid?
|
56
|
+
assert_equal false, Bookworm.new('978herpderp').is_valid?
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_should_not_mark_valid_isbns_as_invalid
|
60
|
+
assert_equal true, @bookworm.is_valid?
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_should_return_nil_if_invalid_string_used
|
64
|
+
assert_nil Bookworm.new('herpderp').as_new
|
65
|
+
assert_nil Bookworm.new('herpderp').as_used
|
66
|
+
assert_nil Bookworm.new('herpderp').as_ten
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_should_have_original_type
|
70
|
+
assert_equal 'used', Bookworm.new("2900976805426").type
|
71
|
+
assert_equal 'used', Bookworm.new('979088359056290000').type
|
72
|
+
assert_equal 'used', Bookworm.new("290097680542690000").type
|
73
|
+
assert_equal 'new', Bookworm.new("290097680542699990").type
|
74
|
+
assert_equal 'new', Bookworm.new("0976805421").type
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bookworm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tyler Johnston
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-12-15 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rake
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
description: Convert from 13 digit to 10 digit, from new to used, and back again. Detect used and new addendums in ISBN strings..
|
27
|
+
email:
|
28
|
+
- tylerjohnst@gmail.com
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files: []
|
34
|
+
|
35
|
+
files:
|
36
|
+
- .gitignore
|
37
|
+
- Gemfile
|
38
|
+
- LICENSE
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- bookworm.gemspec
|
42
|
+
- lib/bookworm.rb
|
43
|
+
- test/bookworm_test.rb
|
44
|
+
homepage: http://github.com/tylerjohnst/bookworm
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: bookworm
|
67
|
+
rubygems_version: 1.8.11
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: ISBN manipulation, conversion, and inspection for Ruby.
|
71
|
+
test_files:
|
72
|
+
- test/bookworm_test.rb
|