petrarca 0.2.0 → 0.3.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 +4 -4
- data/lib/petrarca.rb +2 -2
- data/lib/petrarca/helpers.rb +11 -17
- data/lib/petrarca/isbn10.rb +2 -4
- data/lib/petrarca/isbn13.rb +8 -4
- data/lib/petrarca/version.rb +1 -1
- data/petrarca.gemspec +2 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e588fc804e558ead549eeb33f9eeecf157c2d0652927a8ec8057f0057400f932
|
4
|
+
data.tar.gz: e25faf3cf900c1ced7f34884e347c21b3094db97e3ad49640ed8a721c07dc376
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c0add5276c9ab9f1c1babae1d7838771eaf693eda6acf6840fb5c94e78fc85ac70b2e659db2c37e1afa8757f4cba205120f7687385e3406c8b44127dfbd8ce7
|
7
|
+
data.tar.gz: 0efc1a6cdda198cec9799242a976fc5403d9374ec54237027133d181507e79c933606fcba37d11214d712c7fc79748fd664d54bc385f5a44a0868cbc2fa7d136
|
data/lib/petrarca.rb
CHANGED
@@ -59,12 +59,12 @@ module Petrarca
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def to_10(isbn13)
|
62
|
-
s = isbn13.to_s.delete("-")[3
|
62
|
+
s = isbn13.to_s.delete("-")[3, 9]
|
63
63
|
ISBN10.hyphenate(s + ISBN10.calc_check_digit(s))
|
64
64
|
end
|
65
65
|
|
66
66
|
def to_13(isbn10)
|
67
|
-
s = "978" + isbn10.to_s.delete("-")[0
|
67
|
+
s = "978" + isbn10.to_s.delete("-")[0, 9]
|
68
68
|
ISBN13.hyphenate(s + ISBN13.calc_check_digit(s))
|
69
69
|
end
|
70
70
|
|
data/lib/petrarca/helpers.rb
CHANGED
@@ -1,28 +1,22 @@
|
|
1
1
|
module Petrarca
|
2
2
|
module Helpers
|
3
3
|
|
4
|
+
extend self
|
5
|
+
|
4
6
|
def split_to_parts(body, ranges)
|
5
|
-
|
6
|
-
ranges.each do |range_str|
|
7
|
+
ranges.map do |range_str|
|
7
8
|
s, e = range_str.split("-")
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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("-")
|
9
|
+
prefix = body[0, s.size]
|
10
|
+
if Range.new(s.to_i, e.to_i).cover?(prefix.to_i)
|
11
|
+
[prefix, body[(prefix.size)..]]
|
12
|
+
else
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
end.compact.first
|
22
16
|
end
|
23
17
|
|
24
18
|
|
25
|
-
def
|
19
|
+
def load_ranges(range_file)
|
26
20
|
ranges = {}
|
27
21
|
File.open(range_file, "r") do |f|
|
28
22
|
f.each_line do |line|
|
data/lib/petrarca/isbn10.rb
CHANGED
@@ -4,8 +4,6 @@ require "petrarca/helpers"
|
|
4
4
|
module Petrarca
|
5
5
|
module ISBN10
|
6
6
|
|
7
|
-
include Helpers
|
8
|
-
|
9
7
|
extend self
|
10
8
|
|
11
9
|
def valid?(isbn)
|
@@ -18,7 +16,7 @@ module Petrarca
|
|
18
16
|
end
|
19
17
|
|
20
18
|
def calc_check_digit(isbn)
|
21
|
-
nums = isbn.delete("-").split("")[0
|
19
|
+
nums = isbn.delete("-").split("")[0, 9].map{|x| x.to_i }
|
22
20
|
sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+)
|
23
21
|
check_digit = 11 - (sum % 11)
|
24
22
|
case check_digit
|
@@ -32,7 +30,7 @@ module Petrarca
|
|
32
30
|
end
|
33
31
|
|
34
32
|
def hyphenate(isbn)
|
35
|
-
s =
|
33
|
+
s = ISBN13.hyphenate("978" + isbn)
|
36
34
|
s.sub(/^978-/, "")
|
37
35
|
end
|
38
36
|
|
data/lib/petrarca/isbn13.rb
CHANGED
@@ -4,8 +4,6 @@ require "petrarca/helpers"
|
|
4
4
|
module Petrarca
|
5
5
|
module ISBN13
|
6
6
|
|
7
|
-
include Helpers
|
8
|
-
|
9
7
|
extend self
|
10
8
|
|
11
9
|
def valid?(isbn)
|
@@ -18,14 +16,20 @@ module Petrarca
|
|
18
16
|
end
|
19
17
|
|
20
18
|
def calc_check_digit(isbn)
|
21
|
-
nums = isbn.delete("-").split("")[0
|
19
|
+
nums = isbn.delete("-").split("")[0, 12].map{|x| x.to_i }
|
22
20
|
sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
|
23
21
|
check_digit = 10 - (sum % 10)
|
24
22
|
check_digit == 10 ? "0" : check_digit.to_s
|
25
23
|
end
|
26
24
|
|
27
25
|
def hyphenate(isbn)
|
28
|
-
|
26
|
+
ean_prefix = isbn[0, 3]
|
27
|
+
body = isbn[3, 9]
|
28
|
+
check_digit = isbn[12, 1]
|
29
|
+
registration_group, body = Helpers.split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix])
|
30
|
+
prefix = "#{ean_prefix}-#{registration_group}"
|
31
|
+
registrant, publication = Helpers.split_to_parts(body, REGISTRANT_RANGES[prefix])
|
32
|
+
[ean_prefix, registration_group, registrant, publication, check_digit].join("-")
|
29
33
|
end
|
30
34
|
|
31
35
|
end
|
data/lib/petrarca/version.rb
CHANGED
data/petrarca.gemspec
CHANGED
@@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.description = %q{Library to manipulate ISBN numbers for Ruby.}
|
14
14
|
spec.homepage = "https://github.com/takatoh/Petrarca"
|
15
15
|
|
16
|
+
spec.licenses = ["MIT"]
|
17
|
+
|
16
18
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
19
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
20
|
# if spec.respond_to?(:metadata)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: petrarca
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- takatoh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,7 +76,8 @@ files:
|
|
76
76
|
- lib/petrarca/version.rb
|
77
77
|
- petrarca.gemspec
|
78
78
|
homepage: https://github.com/takatoh/Petrarca
|
79
|
-
licenses:
|
79
|
+
licenses:
|
80
|
+
- MIT
|
80
81
|
metadata: {}
|
81
82
|
post_install_message:
|
82
83
|
rdoc_options: []
|