petrarca 0.4.14 → 0.5.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f30d4ed1e6dbb387565a884feda53df61cddd0cf13a85709ac4f0181c280b0b8
4
- data.tar.gz: 3688e3d078594adec49661bf53143accbdb0ab6d8752967a35ca8f713e110dfd
3
+ metadata.gz: 1c7dd922e92b20bfdb753eabea5ed5ebc5a17047a28a2e9b626196922f5dc1be
4
+ data.tar.gz: 2459dd81b3d7c31acd1a5e5763e82d870e2e399f9580e445dec0c37b364ac0ab
5
5
  SHA512:
6
- metadata.gz: b41868b2851ac750ea202ec618997463dc3266344826044394d593db593be0be66ec1a2772bc59c3321c4f01c355ac4913ea53b69b92ac020f731b990a464e9a
7
- data.tar.gz: 6278fa8927717b25ceec8a42cb3fa301dee6cfff74bd665f24d55c6e570ea2d13fc689f1a7d2b16059babc60729bd91325820cac4f4a88291ee2ed05aedd984c
6
+ metadata.gz: 5ad250d61d24ab92931600c04e2027370d61c9da1358459fb5ee1aebfdcbb9515b6dd4fe1dda024c46a261e8469d01c33bd7a6d6f0a2d045bf8c10d4e589715b
7
+ data.tar.gz: 955ad99e4add12f5e4d437f6abae2d1f8fe3be64729b8213e2dd29cb9e20f43b43bb43b9573e77e9a7462b0b818a7217bcc4e07a658a7b7aca501f1e365df4af
data/README.md CHANGED
@@ -12,7 +12,7 @@ All functions support both ISBN-13 and ISBN-10.
12
12
  All ranges of registration groups and registrants are supported.
13
13
  Those depends on 'RangeMessage.xml' file, downloaded from [International ISBN Agency](https://www.isbn-international.org/range_file_generation).
14
14
 
15
- NOTE: Updated range files to latest version on April 2, 2022.
15
+ NOTE: Updated range files to latest version on August 12, 2022.
16
16
 
17
17
  ## Installation
18
18
 
@@ -45,6 +45,28 @@ Or install it yourself as:
45
45
  irb(main):006:0> Petrarca.hyphenate("9784815606442")
46
46
  => "978-4-8156-0644-2"
47
47
 
48
+ SINCE v0.5.0
49
+
50
+ Accept Integer as ISBN, only if no `-` and `X` including.
51
+
52
+ irb(main):001:0> require 'petrarca'
53
+ => true
54
+ irb(main):002:0> Petrarca.calc_check_digit(978481560644)
55
+ => "2"
56
+ irb(main):003:0> Petrarca.to_10(9784815606442)
57
+ => "4-8156-0644-7"
58
+ irb(main):004:0> Petrarca.to_13(4815606447)
59
+ => "978-4-8156-0644-2"
60
+ irb(main):005:0> Petrarca.hyphenate(9784815606442)
61
+ => "978-4-8156-0644-2"
62
+
63
+ If the ISBN is hyphenated, consider registration group and registrant in `Petrarca.valid?`. The second example below has the invalid registrant.
64
+
65
+ irb(main):006:0> Petrarca.valid?("978-4-8156-0644-2")
66
+ => true
67
+ irb(main):007:0> Petrarca.valid?("978-4-815-60644-2")
68
+ => false
69
+
48
70
  ## License
49
71
 
50
72
  MIT License
@@ -1,7 +1,7 @@
1
1
  # Generated from RangeMessage.xml file, downloaded from https://www.isbn-international.org/range_file_generation.
2
2
  #
3
3
  # International ISBN Agency
4
- # Sat, 2 Apr 2022 22:26:31 BST
4
+ # Fri, 12 Aug 2022 04:58:16 BST
5
5
  #
6
6
  # English language
7
7
  978-0:00-19,200-227,2280-2289,229-368,3690-3699,370-638,6390-6397,6398000-6399999,640-644,6450000-6459999,646-647,6480000-6489999,649-654,6550-6559,656-699,7000-8499,85000-89999,900000-949999,9500000-9999999
@@ -58,7 +58,7 @@
58
58
  # Philippines
59
59
  978-621:00-29,400-599,8000-8999,95000-99999
60
60
  # Iran
61
- 978-622:00-10,200-324,5650-7999,94000-99999
61
+ 978-622:00-10,200-374,5200-7999,92500-99999
62
62
  # Indonesia
63
63
  978-623:00-09,200-499,5250-7999,88000-99999
64
64
  # Sri Lanka
@@ -166,7 +166,7 @@
166
166
  # Egypt
167
167
  978-977:00-19,200-499,5000-6999,700-849,85000-89999,90-98,990-999
168
168
  # Nigeria
169
- 978-978:000-199,2000-2999,30000-79999,8000-8999,900-999
169
+ 978-978:000-199,2000-2999,30000-78999,790-799,8000-8999,900-999
170
170
  # Indonesia
171
171
  978-979:000-099,1000-1499,15000-19999,20-29,3000-3999,400-799,8000-9499,95000-99999
172
172
  # Venezuela
@@ -189,16 +189,18 @@
189
189
  978-988:00-11,12000-19999,200-739,74000-76999,77000-79999,8000-9699,97000-99999
190
190
  # Portugal
191
191
  978-989:0-1,20-34,35000-36999,37-52,53000-54999,550-799,8000-9499,95000-99999
192
+ # Montenegro
193
+ 978-9911:20-24,550-749
192
194
  # Tanzania
193
195
  978-9912:40-44,750-799,9850-9999
194
196
  # Uganda
195
197
  978-9913:00-04,600-649,9800-9999
196
198
  # Kenya
197
- 978-9914:40-44,700-749,9850-9999
199
+ 978-9914:40-49,700-749,9600-9999
198
200
  # Uruguay
199
201
  978-9915:40-59,650-799,9300-9999
200
202
  # Estonia
201
- 978-9916:0-0,10-39,4-4,600-749,9500-9999
203
+ 978-9916:0-0,10-39,4-5,600-749,9500-9999
202
204
  # Bolivia
203
205
  978-9917:0-0,30-34,600-699,9800-9999
204
206
  # Malta
@@ -206,7 +208,7 @@
206
208
  # Mongolia
207
209
  978-9919:20-27,500-599,9500-9999
208
210
  # Morocco
209
- 978-9920:30-39,550-799,9000-9999
211
+ 978-9920:30-41,500-799,8750-9999
210
212
  # Kuwait
211
213
  978-9921:0-0,30-39,700-899,9700-9999
212
214
  # Iraq
@@ -228,7 +230,7 @@
228
230
  # Costa Rica
229
231
  978-9930:00-49,500-939,9400-9999
230
232
  # Algeria
231
- 978-9931:00-25,260-899,9000-9999
233
+ 978-9931:00-23,240-249,2500-2599,260-899,9000-9999
232
234
  # Lao People's Democratic Republic
233
235
  978-9932:00-39,400-849,8500-9999
234
236
  # Syria
@@ -492,7 +494,7 @@
492
494
  # Tajikistan
493
495
  978-99975:0-2,300-399,40-79,800-999
494
496
  # Srpska, Republic of
495
- 978-99976:0-1,20-59,600-799,85-89,900-999
497
+ 978-99976:0-0,10-15,160-199,20-59,600-799,85-89,900-999
496
498
  # Rwanda
497
499
  978-99977:0-1,40-69,700-799,995-999
498
500
  # Mongolia
@@ -515,6 +517,8 @@
515
517
  978-99987:850-999
516
518
  # Sudan
517
519
  978-99988:0-0,50-54,800-824
520
+ # Paraguay
521
+ 978-99989:0-0,50-64,900-999
518
522
  # Ethiopia
519
523
  978-99990:0-0,50-54,975-999
520
524
  # France
@@ -524,4 +528,4 @@
524
528
  # Italy
525
529
  979-12:200-299,5450-5999,80000-84999
526
530
  # United States
527
- 979-8:200-219,4000-8499,88500-89999,9850000-9869999
531
+ 979-8:200-229,3500-3999,4000-8499,8500-8849,88500-89999,9850000-9899999
@@ -1,7 +1,7 @@
1
1
  # Generated from RangeMessage.xml file, downloaded from https://www.isbn-international.org/range_file_generation.
2
2
  #
3
3
  # International ISBN Agency
4
- # Sat, 2 Apr 2022 22:26:31 BST
4
+ # Fri, 12 Aug 2022 04:58:16 BST
5
5
  #
6
6
  # International ISBN Agency
7
7
  978:0-5,600-649,65-65,7-7,80-94,950-989,9900-9989,99900-99999
@@ -1,10 +1,37 @@
1
1
  module Petrarca
2
+
3
+ class InvalidEANPrefixError < StandardError; end
4
+ class InvalidRangeError < StandardError; end
5
+
2
6
  module Helpers
3
7
 
4
8
  extend self
5
9
 
10
+ def split(isbn)
11
+ isbn = isbn.to_s.delete("-")
12
+ ean_prefix = isbn[0, 3]
13
+ unless ean_prefix == "978" || ean_prefix == "979"
14
+ raise InvalidEANPrefixError.new("Invalid EAN prefix: #{ean_prefix}")
15
+ end
16
+ body = isbn[3, 9]
17
+ check_digit = isbn[12, 1]
18
+ begin
19
+ registration_group, body = Helpers.split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix])
20
+ rescue InvalidRangeError
21
+ raise InvalidRangeError.new("Registration group is not defined: #{body} (under #{ean_prefix})")
22
+ end
23
+ prefix = "#{ean_prefix}-#{registration_group}"
24
+ begin
25
+ registrant, publication = Helpers.split_to_parts(body, REGISTRANT_RANGES[prefix])
26
+ rescue InvalidRangeError
27
+ raise InvalidRangeError.new("Registrant is not defined: #{body} (under #{prefix})")
28
+ end
29
+ [ean_prefix, registration_group, registrant, publication, check_digit]
30
+ end
31
+
32
+
6
33
  def split_to_parts(body, ranges)
7
- ranges.map do |range_str|
34
+ parts = ranges.map do |range_str|
8
35
  s, e = range_str.split("-")
9
36
  prefix = body[0, s.size]
10
37
  if Range.new(s.to_i, e.to_i).cover?(prefix.to_i)
@@ -12,7 +39,12 @@ module Petrarca
12
39
  else
13
40
  nil
14
41
  end
15
- end.compact.first
42
+ end.compact
43
+ unless parts.empty?
44
+ parts.first
45
+ else
46
+ raise InvalidRangeError.new(body)
47
+ end
16
48
  end
17
49
 
18
50
 
@@ -7,16 +7,25 @@ module Petrarca
7
7
  extend self
8
8
 
9
9
  def valid?(isbn)
10
- correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
10
+ isbn = isbn.to_s
11
+ if correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
12
+ if isbn.include?("-")
13
+ Helpers.split("978" + isbn).drop(1) == isbn.split("-")
14
+ else
15
+ true
16
+ end
17
+ else
18
+ false
19
+ end
11
20
  end
12
21
 
13
22
  def correct_format?(isbn)
14
- isbn = isbn.delete("-")
23
+ isbn = isbn.to_s.delete("-")
15
24
  !!(/\A\d{9}[0-9X]\z/ =~ isbn)
16
25
  end
17
26
 
18
27
  def calc_check_digit(isbn)
19
- nums = isbn.delete("-").split("")[0, 9].map{|x| x.to_i }
28
+ nums = isbn.to_s.delete("-").split("")[0, 9].map{|x| x.to_i }
20
29
  sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+)
21
30
  check_digit = 11 - (sum % 11)
22
31
  case check_digit
@@ -30,8 +39,7 @@ module Petrarca
30
39
  end
31
40
 
32
41
  def hyphenate(isbn)
33
- s = ISBN13.hyphenate("978" + isbn)
34
- s.sub(/^978-/, "")
42
+ Helpers.split("978" + isbn.to_s).drop(1).join("-")
35
43
  end
36
44
 
37
45
  end
@@ -7,29 +7,32 @@ module Petrarca
7
7
  extend self
8
8
 
9
9
  def valid?(isbn)
10
- correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
10
+ isbn = isbn.to_s
11
+ if correct_format?(isbn) && isbn[-1] == calc_check_digit(isbn)
12
+ if isbn.include?("-")
13
+ Helpers.split(isbn) == isbn.split("-")
14
+ else
15
+ true
16
+ end
17
+ else
18
+ false
19
+ end
11
20
  end
12
21
 
13
22
  def correct_format?(isbn)
14
- isbn = isbn.delete("-")
23
+ isbn = isbn.to_s.delete("-")
15
24
  !!(/\A97[89]\d{9}\d\z/ =~ isbn)
16
25
  end
17
26
 
18
27
  def calc_check_digit(isbn)
19
- nums = isbn.delete("-").split("")[0, 12].map{|x| x.to_i }
28
+ nums = isbn.to_s.delete("-").split("")[0, 12].map{|x| x.to_i }
20
29
  sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
21
30
  check_digit = 10 - (sum % 10)
22
31
  check_digit == 10 ? "0" : check_digit.to_s
23
32
  end
24
33
 
25
34
  def hyphenate(isbn)
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("-")
35
+ Helpers.split(isbn).join("-")
33
36
  end
34
37
 
35
38
  end
@@ -1,3 +1,3 @@
1
1
  module Petrarca
2
- VERSION = "0.4.14"
2
+ VERSION = "0.5.2"
3
3
  end
data/lib/petrarca.rb CHANGED
@@ -15,7 +15,7 @@ module Petrarca
15
15
  extend self
16
16
 
17
17
  def valid?(isbn)
18
- case isbn.delete("-").size
18
+ case isbn.to_s.delete("-").size
19
19
  when 13
20
20
  ISBN13.valid?(isbn)
21
21
  when 10
@@ -26,7 +26,7 @@ module Petrarca
26
26
  end
27
27
 
28
28
  def correct_format?(isbn)
29
- case isbn.delete("-").size
29
+ case isbn.to_s.delete("-").size
30
30
  when 13
31
31
  ISBN13.correct_format?(isbn)
32
32
  when 10
@@ -37,7 +37,7 @@ module Petrarca
37
37
  end
38
38
 
39
39
  def calc_check_digit(isbn)
40
- isbn = isbn.delete("-")
40
+ isbn = isbn.to_s.delete("-")
41
41
  case isbn.size
42
42
  when 12, 13
43
43
  ISBN13.calc_check_digit(isbn)
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.14
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - takatoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-02 00:00:00.000000000 Z
11
+ date: 2022-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler