petrarca 0.4.14 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f30d4ed1e6dbb387565a884feda53df61cddd0cf13a85709ac4f0181c280b0b8
4
- data.tar.gz: 3688e3d078594adec49661bf53143accbdb0ab6d8752967a35ca8f713e110dfd
3
+ metadata.gz: 555636db6e3aff42afbd5f5d6dc82ae87fc93ce54cf0969824efcd55430d5a9a
4
+ data.tar.gz: 35bf3992488d8c4fbf928b7c44c89cede9fc82828cecf6ffed8e9ea79ff47b49
5
5
  SHA512:
6
- metadata.gz: b41868b2851ac750ea202ec618997463dc3266344826044394d593db593be0be66ec1a2772bc59c3321c4f01c355ac4913ea53b69b92ac020f731b990a464e9a
7
- data.tar.gz: 6278fa8927717b25ceec8a42cb3fa301dee6cfff74bd665f24d55c6e570ea2d13fc689f1a7d2b16059babc60729bd91325820cac4f4a88291ee2ed05aedd984c
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 April 2, 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
- # Sat, 2 Apr 2022 22:26:31 BST
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
@@ -189,6 +189,8 @@
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:550-749
192
194
  # Tanzania
193
195
  978-9912:40-44,750-799,9850-9999
194
196
  # Uganda
@@ -198,7 +200,7 @@
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
@@ -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
+ # 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.14"
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.14
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-04-02 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