petrarca 0.4.12 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb0ccff65ffe9d1c7e156994de57a1d24252cb4dde61f114903455cc68ab5763
4
- data.tar.gz: 72d19fb813fe2eb03a702e2574d53980127b2bb463eb37af851b1b3da635f171
3
+ metadata.gz: 555636db6e3aff42afbd5f5d6dc82ae87fc93ce54cf0969824efcd55430d5a9a
4
+ data.tar.gz: 35bf3992488d8c4fbf928b7c44c89cede9fc82828cecf6ffed8e9ea79ff47b49
5
5
  SHA512:
6
- metadata.gz: 01a5ac13121daa41cd7d106e1db42e3a7a80d712fccded276229780e606faf3bf33aad846b92445bb588a842a1b2b8937efc54c8ba7fb7accebbb93d75632c8b
7
- data.tar.gz: 71c549ce192376ac2d413a94cfd77242edf58bae0627e513b129d03ff68c5552db5ef544ae6410cfb08888ede4e4d6ddcedba769f364d2884008e18dc695a268
6
+ metadata.gz: 6261436383f789b8fcfc67eccee809d6ce1c44d959dc137406354297e9dd0af53406f3c0556c0cd7784cc3aa0d24b3f01e6b75da08285d499a26a256f9482c88
7
+ data.tar.gz: d07edfb969d86a50b73ed9ce80c48f4519aa7e967e501c7cd10407ec2e0e73d1ee1da111fd3e7e761eff467fa3e81d632233bc34e4f1ef561d87e24d31e541ec
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 February 8, 2022.
15
+ NOTE: Updated range files to latest version on May 4, 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
- # Tue, 8 Feb 2022 02:45:23 GMT
4
+ # Wed, 4 May 2022 07:09:56 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
@@ -60,9 +60,9 @@
60
60
  # Iran
61
61
  978-622:00-10,200-324,5650-7999,94000-99999
62
62
  # Indonesia
63
- 978-623:00-09,200-499,5500-7999,90000-99999
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,9 @@
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
+ # Malaysia
75
+ 978-629:00-02,470-499,7500-7999,96500-99999
74
76
  # Romania
75
77
  978-630:300-349,6500-6849
76
78
  # Brazil
@@ -187,6 +189,8 @@
187
189
  978-988:00-11,12000-19999,200-739,74000-76999,77000-79999,8000-9699,97000-99999
188
190
  # Portugal
189
191
  978-989:0-1,20-34,35000-36999,37-52,53000-54999,550-799,8000-9499,95000-99999
192
+ # Montenegro
193
+ 978-9911:550-749
190
194
  # Tanzania
191
195
  978-9912:40-44,750-799,9850-9999
192
196
  # Uganda
@@ -196,7 +200,7 @@
196
200
  # Uruguay
197
201
  978-9915:40-59,650-799,9300-9999
198
202
  # Estonia
199
- 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
200
204
  # Bolivia
201
205
  978-9917:0-0,30-34,600-699,9800-9999
202
206
  # Malta
@@ -436,7 +440,7 @@
436
440
  # Eritrea
437
441
  978-99948:0-4,50-79,800-999
438
442
  # Mauritius
439
- 978-99949:0-1,20-89,900-999
443
+ 978-99949:0-1,20-79,8-8,900-999
440
444
  # Cambodia
441
445
  978-99950:0-4,50-79,800-999
442
446
  # Reserved Agency
@@ -476,7 +480,7 @@
476
480
  # Botswana
477
481
  978-99968:0-3,400-599,60-89,900-999
478
482
  # Oman
479
- 978-99969:0-4,50-79,800-999
483
+ 978-99969:0-4,50-79,800-949,95-99
480
484
  # Haiti
481
485
  978-99970:0-4,50-89,900-999
482
486
  # Myanmar
@@ -513,6 +517,8 @@
513
517
  978-99987:850-999
514
518
  # Sudan
515
519
  978-99988:0-0,50-54,800-824
520
+ # Ethiopia
521
+ 978-99990:0-0,50-54,975-999
516
522
  # France
517
523
  979-10:00-19,200-699,7000-8999,90000-97599,976000-999999
518
524
  # Korea, Republic
@@ -520,4 +526,4 @@
520
526
  # Italy
521
527
  979-12:200-299,5450-5999,80000-84999
522
528
  # United States
523
- 979-8:200-219,4000-7999,88500-89999,9850000-9869999
529
+ 979-8:200-219,4000-8499,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
- # Tue, 8 Feb 2022 02:45:23 GMT
4
+ # Wed, 4 May 2022 07:09:56 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.12"
2
+ VERSION = "0.5.0"
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.12
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takatoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-08 00:00:00.000000000 Z
11
+ date: 2022-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler