petrarca 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/README.md +52 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/petrarca.rb +71 -0
- data/lib/petrarca/helpers.rb +38 -0
- data/lib/petrarca/isbn10.rb +40 -0
- data/lib/petrarca/isbn13.rb +32 -0
- data/lib/petrarca/version.rb +3 -0
- data/petrarca.gemspec +41 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 78b04a2cf61f74ad207570c95631cf1a2187be066ee096ab0a321799e45fb986
|
4
|
+
data.tar.gz: 12625949933981779b0cc28a3b5267565e877a1e27dbe4ecfd9f92ecc68bee9a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 58460aad6cbfc5b8c251a1dd9ab897cae10366cb46b8db8374c6242818364bca9875ce6bc3413262fc71a729f87ab1df87f5cd13fb4bc442e2bc1358634dff71
|
7
|
+
data.tar.gz: b25f466050d74bf9b2b2182aeb2174c5e480b71c31de54fa1aed7ce5168c40cd1c975ff1194ff8d30c0539413212e42e84d4ae550754f32e159b5bcf408887ab
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Petrarca
|
2
|
+
|
3
|
+
This library Petrarca provides some utility functions to manipulate ISBN numbers:
|
4
|
+
|
5
|
+
- Validation
|
6
|
+
- Calculate check digit
|
7
|
+
- Mutual conversion of ISBN-13 and ISBN-10
|
8
|
+
- Hyphenation
|
9
|
+
|
10
|
+
All functions support both ISBN-13 and ISBN-10.
|
11
|
+
|
12
|
+
Note: Only registrant ranges for Japan are currently supported.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'petrarca'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install petrarca
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
irb(main):001:0> require 'petrarca'
|
33
|
+
=> true
|
34
|
+
irb(main):002:0> Petrarca.valid?("978-4-8156-0644-2")
|
35
|
+
=> true
|
36
|
+
irb(main):003:0> Petrarca.calc_check_digit("978-4-8156-0644-2")
|
37
|
+
=> "2"
|
38
|
+
irb(main):004:0> Petrarca.to_10("978-4-8156-0644-2")
|
39
|
+
=> "4-8156-0644-7"
|
40
|
+
irb(main):005:0> Petrarca.to_13("4-8156-0644-7")
|
41
|
+
=> "978-4-8156-0644-2"
|
42
|
+
irb(main):006:0> Petrarca.hyphenate("9784815606442")
|
43
|
+
=> "978-4-8156-0644-2"
|
44
|
+
|
45
|
+
## License
|
46
|
+
|
47
|
+
MIT License
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at
|
52
|
+
https://github.com/takatoh/Petrarca.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "petrarca"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/petrarca.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "petrarca/version"
|
2
|
+
require "petrarca/isbn13"
|
3
|
+
require "petrarca/isbn10"
|
4
|
+
require "petrarca/helpers"
|
5
|
+
|
6
|
+
|
7
|
+
module Petrarca
|
8
|
+
class Error < StandardError; end
|
9
|
+
class IncorrectFormatError < StandardError; end
|
10
|
+
|
11
|
+
data_dir = __dir__ + "/../data"
|
12
|
+
REGISTRATION_GROUP_RANGES = Helpers.load_ranges("#{data_dir}/registration_group_ranges.txt")
|
13
|
+
REGISTRANT_RANGES = Helpers.load_ranges("#{data_dir}/registrant_ranges.txt")
|
14
|
+
|
15
|
+
extend self
|
16
|
+
|
17
|
+
def valid?(isbn)
|
18
|
+
case isbn.delete("-").size
|
19
|
+
when 13
|
20
|
+
ISBN13.valid?(isbn)
|
21
|
+
when 10
|
22
|
+
ISBN10.valid?(isbn)
|
23
|
+
else
|
24
|
+
false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def correct_format?(isbn)
|
29
|
+
case isbn.delete("-").size
|
30
|
+
when 13
|
31
|
+
ISBN13.correct_format?(isbn)
|
32
|
+
when 10
|
33
|
+
ISBN10.correct_format?(isbn)
|
34
|
+
else
|
35
|
+
false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def calc_check_digit(isbn)
|
40
|
+
isbn = isbn.delete("-")
|
41
|
+
case isbn.size
|
42
|
+
when 12, 13
|
43
|
+
ISBN13.calc_check_digit(isbn)
|
44
|
+
when 9, 10
|
45
|
+
ISBN10.calc_check_digit(isbn)
|
46
|
+
else
|
47
|
+
raise IncorrectFormatError
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def hyphenate(isbn)
|
52
|
+
isbn = isbn.to_s
|
53
|
+
case isbn.size
|
54
|
+
when 13
|
55
|
+
ISBN13.hyphenate(isbn)
|
56
|
+
when 10
|
57
|
+
ISBN10.hyphenate(isbn)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_10(isbn13)
|
62
|
+
s = isbn13.to_s.delete("-")[3..11]
|
63
|
+
ISBN10.hyphenate(s + ISBN10.calc_check_digit(s))
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_13(isbn10)
|
67
|
+
s = "978" + isbn10.to_s.delete("-")[0..8]
|
68
|
+
ISBN13.hyphenate(s + ISBN13.calc_check_digit(s))
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Petrarca
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
def split_to_parts(body, ranges)
|
5
|
+
g = ""
|
6
|
+
ranges.each do |range_str|
|
7
|
+
s, e = range_str.split("-")
|
8
|
+
g = body[0..(s.size - 1)]
|
9
|
+
break if Range.new(s.to_i, e.to_i).cover?(g.to_i)
|
10
|
+
end
|
11
|
+
[g, body[(g.size)..]]
|
12
|
+
end
|
13
|
+
|
14
|
+
def hyphenate13(isbn)
|
15
|
+
ean_prefix = isbn[0..2]
|
16
|
+
body = isbn[3..11]
|
17
|
+
check_digit = isbn[12..12]
|
18
|
+
registration_group, body = split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix])
|
19
|
+
prefix = "#{ean_prefix}-#{registration_group}"
|
20
|
+
registrant, publication = split_to_parts(body, REGISTRANT_RANGES[prefix])
|
21
|
+
[ean_prefix, registration_group, registrant, publication, check_digit].join("-")
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def self.load_ranges(range_file)
|
26
|
+
ranges = {}
|
27
|
+
File.open(range_file, "r") do |f|
|
28
|
+
f.each_line do |line|
|
29
|
+
next if line.start_with?("#")
|
30
|
+
g, r = line.chomp.split(":")
|
31
|
+
ranges[g] = r.split(",")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
ranges
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "petrarca/helpers"
|
2
|
+
|
3
|
+
|
4
|
+
module Petrarca
|
5
|
+
module ISBN10
|
6
|
+
|
7
|
+
include Helpers
|
8
|
+
|
9
|
+
extend self
|
10
|
+
|
11
|
+
def valid?(isbn)
|
12
|
+
correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
|
13
|
+
end
|
14
|
+
|
15
|
+
def correct_format?(isbn)
|
16
|
+
isbn = isbn.delete("-")
|
17
|
+
!!(/\A\d{9}[0-9X]\z/ =~ isbn)
|
18
|
+
end
|
19
|
+
|
20
|
+
def calc_check_digit(isbn)
|
21
|
+
nums = isbn.delete("-").split("")[0..8].map{|x| x.to_i }
|
22
|
+
sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+)
|
23
|
+
check_digit = 11 - (sum % 11)
|
24
|
+
case check_digit
|
25
|
+
when 10
|
26
|
+
"X"
|
27
|
+
when 11
|
28
|
+
"0"
|
29
|
+
else
|
30
|
+
check_digit.to_s
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def hyphenate(isbn)
|
35
|
+
s = hyphenate13("978" + isbn)
|
36
|
+
s.sub(/^978-/, "")
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "petrarca/helpers"
|
2
|
+
|
3
|
+
|
4
|
+
module Petrarca
|
5
|
+
module ISBN13
|
6
|
+
|
7
|
+
include Helpers
|
8
|
+
|
9
|
+
extend self
|
10
|
+
|
11
|
+
def valid?(isbn)
|
12
|
+
correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
|
13
|
+
end
|
14
|
+
|
15
|
+
def correct_format?(isbn)
|
16
|
+
isbn = isbn.delete("-")
|
17
|
+
!!(/\A97[89]\d{9}\d\z/ =~ isbn)
|
18
|
+
end
|
19
|
+
|
20
|
+
def calc_check_digit(isbn)
|
21
|
+
nums = isbn.delete("-").split("")[0..11].map{|x| x.to_i }
|
22
|
+
sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
|
23
|
+
check_digit = 10 - (sum % 10)
|
24
|
+
check_digit == 10 ? "0" : check_digit.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def hyphenate(isbn)
|
28
|
+
hyphenate13(isbn)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/petrarca.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "petrarca/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "petrarca"
|
8
|
+
spec.version = Petrarca::VERSION
|
9
|
+
spec.authors = ["takatoh"]
|
10
|
+
spec.email = ["takatoh.m@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Manipulate ISBN numbers.}
|
13
|
+
spec.description = %q{Library to manipulate ISBN numbers for Ruby.}
|
14
|
+
spec.homepage = "https://github.com/takatoh/Petrarca"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
#
|
21
|
+
# spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
23
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
24
|
+
# else
|
25
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
# "public gem pushes."
|
27
|
+
# end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = "exe"
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ["lib"]
|
37
|
+
|
38
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
39
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
40
|
+
spec.add_development_dependency "rspec", ">= 3.0.0"
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: petrarca
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- takatoh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-08-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
55
|
+
description: Library to manipulate ISBN numbers for Ruby.
|
56
|
+
email:
|
57
|
+
- takatoh.m@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- data/registrant_ranges.txt
|
71
|
+
- data/registration_group_ranges.txt
|
72
|
+
- lib/petrarca.rb
|
73
|
+
- lib/petrarca/helpers.rb
|
74
|
+
- lib/petrarca/isbn10.rb
|
75
|
+
- lib/petrarca/isbn13.rb
|
76
|
+
- lib/petrarca/version.rb
|
77
|
+
- petrarca.gemspec
|
78
|
+
homepage: https://github.com/takatoh/Petrarca
|
79
|
+
licenses: []
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubygems_version: 3.1.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Manipulate ISBN numbers.
|
100
|
+
test_files: []
|