petrarca 0.4.13 → 0.5.1

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: 402b877f049e3754cb83cb6dcf981a143fe12104cf9a1951a19c65fb66f24d60
4
- data.tar.gz: 0b6d8b20fdc89207cb4c6a337c0c33cda2fe8438953f3152aa92402e2892a781
3
+ metadata.gz: 0ab4ba09871791640cb9f9bffbd19d2f5a6a12f582b5e7d68757bdead9789128
4
+ data.tar.gz: 39db2ae7f09275b01f5b3400b33428389086fea36eaf4743074dfd9ca0af65a9
5
5
  SHA512:
6
- metadata.gz: 35458364a5ca87a2df20fdcad6406b4fb8d7238957b6c5fd9b06a9cdf07016ca84fc297d9774356dbec1ca7273c66003a2c180fc448b44e4b7cf7bdeeb83bcd9
7
- data.tar.gz: e06f853c439b6c77d7e1c67d818d4c979b98db953416ad1889e133b917bb2b3c1552fccfeec6fde5a73e105bd859acadacaf547be3befafb1af664e9411903d4
6
+ metadata.gz: aba9bb0617f5e85d03e60c8e7137c59fb315303e3275adcb98109662a21bc3cc45816f5e315fe7641194a3f3794e8982636040c91f644e885217194919890f1a
7
+ data.tar.gz: 671dfaece75762853fb12109bd8204b2927688dde606273b839d645430790436a751bf6639edb0e523cef0d2c6dabeaaa43b744e389d82c9e5d9d90eb00ff19d
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 March 6, 2022.
15
+ NOTE: Updated range files to latest version on June 11, 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
- # Sun, 6 Mar 2022 08:32:54 GMT
4
+ # Sat, 11 Jun 2022 03:56:52 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,11 +58,11 @@
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
65
- 978-624:00-04,200-249,5000-6249,95000-99999
65
+ 978-624:00-04,200-249,5000-6349,94500-99999
66
66
  # Turkey
67
67
  978-625:00-00,400-442,44300-44499,445-449,7000-7793,77940-77949,7795-8499
68
68
  # Taiwan
@@ -70,7 +70,7 @@
70
70
  # Pakistan
71
71
  978-627:30-31,500-524,7500-7999
72
72
  # Colombia
73
- 978-628:00-09,500-549,7500-8499
73
+ 978-628:00-09,500-549,7500-8499,95000-99999
74
74
  # Malaysia
75
75
  978-629:00-02,470-499,7500-7999,96500-99999
76
76
  # Romania
@@ -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,9850-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
@@ -438,7 +440,7 @@
438
440
  # Eritrea
439
441
  978-99948:0-4,50-79,800-999
440
442
  # Mauritius
441
- 978-99949:0-1,20-89,900-999
443
+ 978-99949:0-1,20-79,8-8,900-999
442
444
  # Cambodia
443
445
  978-99950:0-4,50-79,800-999
444
446
  # Reserved Agency
@@ -478,7 +480,7 @@
478
480
  # Botswana
479
481
  978-99968:0-3,400-599,60-89,900-999
480
482
  # Oman
481
- 978-99969:0-4,50-79,800-999
483
+ 978-99969:0-4,50-79,800-949,95-99
482
484
  # Haiti
483
485
  978-99970:0-4,50-89,900-999
484
486
  # Myanmar
@@ -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,10 @@
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
522
+ # Ethiopia
523
+ 978-99990:0-0,50-54,975-999
518
524
  # France
519
525
  979-10:00-19,200-699,7000-8999,90000-97599,976000-999999
520
526
  # Korea, Republic
@@ -522,4 +528,4 @@
522
528
  # Italy
523
529
  979-12:200-299,5450-5999,80000-84999
524
530
  # United States
525
- 979-8:200-219,4000-7999,88500-89999,9850000-9869999
531
+ 979-8:200-219,3500-3999,4000-8499,8500-8849,88500-89999,9850000-9869999
@@ -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
- # Sun, 6 Mar 2022 08:32:54 GMT
4
+ # Sat, 11 Jun 2022 03:56:52 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.13"
2
+ VERSION = "0.5.1"
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.13
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - takatoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-06 00:00:00.000000000 Z
11
+ date: 2022-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler