isbn_kit 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.
- checksums.yaml +7 -0
- data/.rubocop.yml +11 -0
- data/CHANGELOG.md +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +15 -0
- data/lib/isbn_kit/isbn.rb +110 -0
- data/lib/isbn_kit/version.rb +5 -0
- data/lib/isbn_kit.rb +10 -0
- data/sig/isbn_kit/isbn.rbs +59 -0
- data/sig/isbn_kit.rbs +4 -0
- metadata +60 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e1889ada2bd48f95e93d39a7edd70111f4af0865844a82c01ca17c6ec01b3af8
|
|
4
|
+
data.tar.gz: 297acd9a52f46e8bcb1afcd545e4cb20cf61deae24794b72342317717ffd5eae
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 66d6e07f8c1cca73cd5c7c8ad41932f3dd1388c43cde6cf95676ed7305532554e3e4c60b72d60bf875a738f6938dbe84b2f8311de6b1ce08eef230808fcfda20
|
|
7
|
+
data.tar.gz: e01017da2e247f076e58ea153368af1dac550431a053b93867187d6983e71342cfd15a942ab239507d712c656791c7ba6dc71e5d8a0c07912f7884c30070a2f2
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2026-11-18
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- Initial release
|
|
7
|
+
- ISBN sanitization and normalization.
|
|
8
|
+
- ISBN-10 and ISBN-13 validation.
|
|
9
|
+
- ISBN-10 to ISBN-13 conversion and vice versa.
|
|
10
|
+
|
|
11
|
+
### Future Release
|
|
12
|
+
- ISBN segmentation and formatting using official allocation ranges
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Addiel Moldes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# IsbnKit
|
|
2
|
+
|
|
3
|
+
## [UNRELEASED]
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
8
|
+
|
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
TODO: Write usage instructions here
|
|
24
|
+
|
|
25
|
+
## Development
|
|
26
|
+
|
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
28
|
+
|
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
30
|
+
|
|
31
|
+
## Contributing
|
|
32
|
+
|
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/addielmoldes/isbn_kit.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rubocop/rake_task"
|
|
5
|
+
require "rake/testtask"
|
|
6
|
+
|
|
7
|
+
Rake::TestTask.new do |t|
|
|
8
|
+
t.libs << "test"
|
|
9
|
+
t.test_files = ["test/**/*_test.rb"]
|
|
10
|
+
t.verbose = true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
RuboCop::RakeTask.new
|
|
14
|
+
|
|
15
|
+
task default: %i[test rubocop]
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Provides an object-oriented interface for working with ISBNs.
|
|
4
|
+
module IsbnKit
|
|
5
|
+
# Represents an ISBN and provides normalization, validation
|
|
6
|
+
# and conversion between ISBN-10 and ISBN-13.
|
|
7
|
+
class Isbn
|
|
8
|
+
def initialize(isbn)
|
|
9
|
+
@isbn = normalize(isbn).freeze
|
|
10
|
+
freeze
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def valid?
|
|
14
|
+
isbn10? || isbn13?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def isbn10?
|
|
18
|
+
@isbn.match?(/\A\d{9}[\dX]\z/) && valid_isbn10_checksum?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def isbn13?
|
|
22
|
+
@isbn.match?(/\A(978|979)\d{10}\z/) && valid_isbn13_checksum?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def parts
|
|
26
|
+
raise NotImplementedError
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def to_isbn10
|
|
30
|
+
return unless isbn13? && @isbn.start_with?("978")
|
|
31
|
+
|
|
32
|
+
IsbnKit::Isbn.new convert_to_isbn10
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def to_isbn10!
|
|
36
|
+
raise IsbnKit::ConversionError, "Cannot be converted to ISBN-10" unless isbn13? && @isbn.start_with?("978")
|
|
37
|
+
|
|
38
|
+
IsbnKit::Isbn.new convert_to_isbn10
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def to_isbn13
|
|
42
|
+
return unless isbn10?
|
|
43
|
+
|
|
44
|
+
IsbnKit::Isbn.new convert_to_isbn13
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def to_isbn13!
|
|
48
|
+
raise IsbnKit::ConversionError, "Cannot be converted to ISBN-13" unless isbn10?
|
|
49
|
+
|
|
50
|
+
IsbnKit::Isbn.new convert_to_isbn13
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def equivalent?(other_isbn)
|
|
54
|
+
raise NotImplementedError
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def to_s
|
|
58
|
+
normalize(@isbn)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def ==(other)
|
|
62
|
+
@isbn == other
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def normalize(isbn)
|
|
68
|
+
isbn.to_s.gsub(/[-_\s]/, "").upcase
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def valid_isbn10_checksum?(isbn = @isbn)
|
|
72
|
+
digits(isbn)[-1] == calculate_isbn10_check_digit(isbn)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def valid_isbn13_checksum?(isbn = @isbn)
|
|
76
|
+
digits(isbn)[-1] == calculate_isbn13_check_digit(isbn)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def calculate_isbn10_check_digit(isbn)
|
|
80
|
+
weights = 10.downto(1)
|
|
81
|
+
sum = digits(isbn)[...-1].zip(weights).sum { |digit, weight| digit * weight }
|
|
82
|
+
|
|
83
|
+
(11 - (sum % 11)) % 11
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def calculate_isbn13_check_digit(isbn)
|
|
87
|
+
sum = digits(isbn)[...-1].each_with_index.sum { |digit, index| digit * (index.even? ? 1 : 3) }
|
|
88
|
+
|
|
89
|
+
(10 - (sum % 10)) % 10
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def digits(isbn)
|
|
93
|
+
isbn.chars.map { |char| char == "X" ? 10 : char.to_i }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def convert_to_isbn10
|
|
97
|
+
@isbn.delete_prefix("978").then do
|
|
98
|
+
it[-1] = calculate_isbn10_check_digit(it).then { |digit| digit == 10 ? "X" : digit.to_s }
|
|
99
|
+
it
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def convert_to_isbn13
|
|
104
|
+
@isbn.dup.prepend("978").then do
|
|
105
|
+
it[-1] = calculate_isbn13_check_digit(it).to_s
|
|
106
|
+
it
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
data/lib/isbn_kit.rb
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module IsbnKit
|
|
2
|
+
class Isbn
|
|
3
|
+
type isbn10_parts = {
|
|
4
|
+
group: String,
|
|
5
|
+
registrant: String,
|
|
6
|
+
publication: String,
|
|
7
|
+
check_digit: String
|
|
8
|
+
}
|
|
9
|
+
type isbn13_parts = {
|
|
10
|
+
gs1: String,
|
|
11
|
+
group: String,
|
|
12
|
+
registrant: String,
|
|
13
|
+
publication: String,
|
|
14
|
+
check_digit: String
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@isbn: String
|
|
18
|
+
|
|
19
|
+
def initialize: (String) -> void
|
|
20
|
+
|
|
21
|
+
def valid?: -> bool
|
|
22
|
+
|
|
23
|
+
def isbn10?: -> bool
|
|
24
|
+
|
|
25
|
+
def isbn13?: -> bool
|
|
26
|
+
|
|
27
|
+
def checksum: -> Integer
|
|
28
|
+
|
|
29
|
+
def parts: -> (isbn10_parts | isbn13_parts)
|
|
30
|
+
|
|
31
|
+
def to_isbn10: -> (Isbn | nil)
|
|
32
|
+
|
|
33
|
+
def to_isbn10!: -> Isbn
|
|
34
|
+
|
|
35
|
+
def to_isbn13: -> (Isbn | nil)
|
|
36
|
+
|
|
37
|
+
def to_isbn13!: -> Isbn
|
|
38
|
+
|
|
39
|
+
def equivalent?: (String) -> bool
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def normalize: (String) -> String
|
|
44
|
+
|
|
45
|
+
def valid_isbn10_checksum?: (?String) -> bool
|
|
46
|
+
|
|
47
|
+
def valid_isbn13_checksum?: (?String) -> bool
|
|
48
|
+
|
|
49
|
+
def calculate_isbn10_check_digit: (String) -> Integer
|
|
50
|
+
|
|
51
|
+
def calculate_isbn13_check_digit: (String) -> Integer
|
|
52
|
+
|
|
53
|
+
def digits: (String) -> Array[Integer]
|
|
54
|
+
|
|
55
|
+
def convert_to_isbn10: -> String
|
|
56
|
+
|
|
57
|
+
def convert_to_isbn13: -> String
|
|
58
|
+
end
|
|
59
|
+
end
|
data/sig/isbn_kit.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: isbn_kit
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Addiel Moldes
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: |
|
|
13
|
+
isbn_kit is a Ruby library for working with International Standard Book
|
|
14
|
+
Numbers (ISBNs).
|
|
15
|
+
|
|
16
|
+
It provides an Isbn class that handles ISBN sanitization and normalization,
|
|
17
|
+
validates ISBN-10 and ISBN-13 numbers, converts between both formats, and
|
|
18
|
+
extracts ISBN components such as GS1 prefix, registration group, registrant
|
|
19
|
+
(publisher), publication element (title), and check digit.
|
|
20
|
+
email:
|
|
21
|
+
- addielpunk99@gmail.com
|
|
22
|
+
executables: []
|
|
23
|
+
extensions: []
|
|
24
|
+
extra_rdoc_files: []
|
|
25
|
+
files:
|
|
26
|
+
- ".rubocop.yml"
|
|
27
|
+
- CHANGELOG.md
|
|
28
|
+
- LICENSE.txt
|
|
29
|
+
- README.md
|
|
30
|
+
- Rakefile
|
|
31
|
+
- lib/isbn_kit.rb
|
|
32
|
+
- lib/isbn_kit/isbn.rb
|
|
33
|
+
- lib/isbn_kit/version.rb
|
|
34
|
+
- sig/isbn_kit.rbs
|
|
35
|
+
- sig/isbn_kit/isbn.rbs
|
|
36
|
+
homepage: https://github.com/addielmoldes/isbn_kit
|
|
37
|
+
licenses:
|
|
38
|
+
- MIT
|
|
39
|
+
metadata:
|
|
40
|
+
homepage_uri: https://github.com/addielmoldes/isbn_kit
|
|
41
|
+
source_code_uri: https://github.com/addielmoldes/isbn_kit
|
|
42
|
+
changelog_uri: https://github.com/addielmoldes/isbn_kit/blob/main/CHANGELOG.md
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: 3.1.0
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
requirements: []
|
|
57
|
+
rubygems_version: 3.6.9
|
|
58
|
+
specification_version: 4
|
|
59
|
+
summary: A Ruby toolkit for validating, normalizing, and converting ISBNs.
|
|
60
|
+
test_files: []
|