petrarca 0.6.6 → 1.0.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: d8bc4e6d2b94cc50fd61a5152335ff676dabbc3e45ff185c425cf47a5ef7117a
4
- data.tar.gz: 339e02cec60630737b3473dc2a92864d1e0955d3e6da13da6f8a343a9fa11998
3
+ metadata.gz: 4132d184b7107b98f372097e6e2273360abd3ced931d5d6bcc0690eceae20e9b
4
+ data.tar.gz: 519bc872dd45f3bfec7fda6cbd85d3b33128c2e593c6467627a4481894e91800
5
5
  SHA512:
6
- metadata.gz: 4ebe8344544e72a5f8089a0c5a9774020c06ed5f3291c2cdde7974ee48dec6d5b4c2f45adface835e7d6b97654fe9f7455a0d351520866aa969a87cbd7790a6e
7
- data.tar.gz: b702f7590ef378a74a82a9acb8dd3e57999176bcdeede5467e480f4bbdd2c838711583ae7f119d69b7fadcf7543e22c899f1c51bd236a2ec51d83da8c6dca20f
6
+ metadata.gz: 5c0c7387558e3655a79370bece28b685df72ce5e2334bdcb4b059190886ad66fe44d9b3ac9142237915b9fa123a0582cabcb65e9e61fb3e53212d6ec37215710
7
+ data.tar.gz: de4ee6524415ccdc74e31a02f236d68077299b3f83cc2fd9e366616b7fdb98eb0a5790d8cc32ff7dfda3558bbc46b24a33abaac2e50855eee7aab2e17333048b
data/.gitignore CHANGED
@@ -1,13 +1,13 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
- /vendor/
10
- /Gemfile.lock
11
- *.txt
12
- *.xml
13
- .ruby-version
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+ /Gemfile.lock
11
+ *.txt
12
+ *.xml
13
+ .ruby-version
data/.rspec CHANGED
@@ -1 +1 @@
1
- --require spec_helper
1
+ --require spec_helper
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in isbn_utils.gemspec
4
- gemspec
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in isbn_utils.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 13.0"
7
+ gem "rspec", "~> 3.0"
data/LICENSE.txt CHANGED
@@ -1,22 +1,22 @@
1
- Copyright (c) 2020 takatoh
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright (c) 2020 takatoh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,82 +1,84 @@
1
- # Petrarca
2
-
3
- This library Petrarca provides some utility functions to manipulate ISBN numbers:
4
-
5
- - Validation
6
- - Calculate check digit
7
- - Mutual conversion of ISBN-13 and ISBN-10
8
- - Hyphenation / Dehyphenation
9
- - Split to a array that includes ISBN segments
10
-
11
- All functions support both ISBN-13 and ISBN-10.
12
-
13
- All ranges of registration groups and registrants are supported.
14
- Those depends on 'RangeMessage.xml' file, downloaded from [International ISBN Agency](https://www.isbn-international.org/range_file_generation).
15
-
16
- NOTE: Updated range files to latest version on July 6, 2024.
17
-
18
- ## Installation
19
-
20
- Add this line to your application's Gemfile:
21
-
22
- ```ruby
23
- gem 'petrarca'
24
- ```
25
-
26
- And then execute:
27
-
28
- $ bundle
29
-
30
- Or install it yourself as:
31
-
32
- $ gem install petrarca
33
-
34
- ## Usage
35
-
36
- irb(main):001:0> require 'petrarca'
37
- => true
38
- irb(main):002:0> Petrarca.valid?("978-4-8156-0644-2")
39
- => true
40
- irb(main):003:0> Petrarca.calc_check_digit("978-4-8156-0644-2")
41
- => "2"
42
- irb(main):004:0> Petrarca.to_10("978-4-8156-0644-2")
43
- => "4-8156-0644-7"
44
- irb(main):005:0> Petrarca.to_13("4-8156-0644-7")
45
- => "978-4-8156-0644-2"
46
- irb(main):006:0> Petrarca.hyphenate("9784815606442")
47
- => "978-4-8156-0644-2"
48
- irb(main):007:0> Petrarca.dehyphenate("978-4-8156-0644-2")
49
- => "9784815605442"
50
- irb(main):008:0> Petrarca.split("9784815606442")
51
- => ["978", "4", "8156", "0644", "2"]
52
-
53
- `Petrarca.dehyphenate` and `Petrarca.split` are new in v0.6.0.
54
-
55
- Accept Integer as ISBN, only if no `-` and `X` including. (New in v0.5.0)
56
-
57
- irb(main):001:0> require 'petrarca'
58
- => true
59
- irb(main):002:0> Petrarca.calc_check_digit(978481560644)
60
- => "2"
61
- irb(main):003:0> Petrarca.to_10(9784815606442)
62
- => "4-8156-0644-7"
63
- irb(main):004:0> Petrarca.to_13(4815606447)
64
- => "978-4-8156-0644-2"
65
- irb(main):005:0> Petrarca.hyphenate(9784815606442)
66
- => "978-4-8156-0644-2"
67
-
68
- If the ISBN is hyphenated, registration group and registrant are considered in `Petrarca.valid?`. The second example below has the invalid registrant.
69
-
70
- irb(main):006:0> Petrarca.valid?("978-4-8156-0644-2")
71
- => true
72
- irb(main):007:0> Petrarca.valid?("978-4-815-60644-2")
73
- => false
74
-
75
- ## License
76
-
77
- MIT License
78
-
79
- ## Contributing
80
-
81
- Bug reports and pull requests are welcome on GitHub at
82
- https://github.com/takatoh/Petrarca.
1
+ # Petrarca
2
+
3
+ This library Petrarca provides some utility functions to manipulate ISBN numbers:
4
+
5
+ - Validation
6
+ - Calculate check digit
7
+ - Mutual conversion of ISBN-13 and ISBN-10
8
+ - Hyphenation / Dehyphenation
9
+ - Split to an array that includes ISBN segments
10
+
11
+ All functions support both ISBN-13 and ISBN-10.
12
+
13
+ All ranges of registration groups and registrants are supported.
14
+ Those depend indirectly on data published by the [International ISBN agency](https://www.isbn-international.org/) through 'isbnranges' gem.
15
+
16
+ **In version 1.0.0, the range files have been removed. Instead, the data provided by 'isbnranges' gem is used.** See https://github.com/takatoh/ISBNRanges for more information.
17
+
18
+ **No longer update just for range files.**
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem "petrarca"
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install petrarca
35
+
36
+ ## Usage
37
+
38
+ irb(main):001:0> require "petrarca"
39
+ => true
40
+ irb(main):002:0> Petrarca.valid?("978-4-8156-0644-2")
41
+ => true
42
+ irb(main):003:0> Petrarca.calc_check_digit("978-4-8156-0644-2")
43
+ => "2"
44
+ irb(main):004:0> Petrarca.to_10("978-4-8156-0644-2")
45
+ => "4-8156-0644-7"
46
+ irb(main):005:0> Petrarca.to_13("4-8156-0644-7")
47
+ => "978-4-8156-0644-2"
48
+ irb(main):006:0> Petrarca.hyphenate("9784815606442")
49
+ => "978-4-8156-0644-2"
50
+ irb(main):007:0> Petrarca.dehyphenate("978-4-8156-0644-2")
51
+ => "9784815605442"
52
+ irb(main):008:0> Petrarca.split("9784815606442")
53
+ => ["978", "4", "8156", "0644", "2"]
54
+
55
+ `Petrarca.dehyphenate` and `Petrarca.split` are new in v0.6.0.
56
+
57
+ Accept Integer as ISBN, only if no `-` and `X` including. (New in v0.5.0)
58
+
59
+ irb(main):001:0> require 'petrarca'
60
+ => true
61
+ irb(main):002:0> Petrarca.calc_check_digit(978481560644)
62
+ => "2"
63
+ irb(main):003:0> Petrarca.to_10(9784815606442)
64
+ => "4-8156-0644-7"
65
+ irb(main):004:0> Petrarca.to_13(4815606447)
66
+ => "978-4-8156-0644-2"
67
+ irb(main):005:0> Petrarca.hyphenate(9784815606442)
68
+ => "978-4-8156-0644-2"
69
+
70
+ If the ISBN is hyphenated, registration group and registrant are considered in `Petrarca.valid?`. The second example below has the invalid registrant.
71
+
72
+ irb(main):006:0> Petrarca.valid?("978-4-8156-0644-2")
73
+ => true
74
+ irb(main):007:0> Petrarca.valid?("978-4-815-60644-2")
75
+ => false
76
+
77
+ ## License
78
+
79
+ MIT License
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at
84
+ https://github.com/takatoh/Petrarca.
data/Rakefile CHANGED
@@ -1,9 +1,2 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
3
-
4
- namespace :range do
5
- desc "Generate registrant range files"
6
- task :generate do
7
- ruby "scripts/generate_range_files.rb --dir data"
8
- end
9
- end
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "petrarca"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "petrarca"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,64 +1,51 @@
1
- module Petrarca
2
-
3
- class InvalidEANPrefixError < StandardError; end
4
- class InvalidRangeError < StandardError; end
5
-
6
- module Helpers
7
-
8
- extend self
9
-
10
- def split(isbn)
11
- isbn = Petrarca.dehyphenate(isbn)
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
-
33
- def split_to_parts(body, ranges)
34
- parts = ranges.map do |range_str|
35
- s, e = range_str.split("-")
36
- prefix = body[0, s.size]
37
- if Range.new(s.to_i, e.to_i).cover?(prefix.to_i)
38
- [prefix, body[(prefix.size)..]]
39
- else
40
- nil
41
- end
42
- end.compact
43
- unless parts.empty?
44
- parts.first
45
- else
46
- raise InvalidRangeError.new(body)
47
- end
48
- end
49
-
50
-
51
- def load_ranges(range_file)
52
- ranges = {}
53
- File.open(range_file, "r") do |f|
54
- f.each_line do |line|
55
- next if line.start_with?("#")
56
- g, r = line.chomp.split(":")
57
- ranges[g] = r.split(",") unless r.nil?
58
- end
59
- end
60
- ranges
61
- end
62
-
63
- end
64
- end
1
+ module Petrarca
2
+
3
+ class InvalidEANPrefixError < StandardError; end
4
+ class InvalidRangeError < StandardError; end
5
+
6
+ module Helpers
7
+
8
+ extend self
9
+
10
+ def split(isbn)
11
+ isbn = Petrarca.dehyphenate(isbn)
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
+
33
+ def split_to_parts(body, ranges)
34
+ parts = ranges.map do |range|
35
+ s, e = range
36
+ prefix = body[0, s.size]
37
+ if Range.new(s, e).cover?(prefix)
38
+ [prefix, body[(prefix.size)..]]
39
+ else
40
+ nil
41
+ end
42
+ end.compact
43
+ unless parts.empty?
44
+ parts.first
45
+ else
46
+ raise InvalidRangeError.new(body)
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -1,54 +1,54 @@
1
- require "petrarca/helpers"
2
-
3
-
4
- module Petrarca
5
- module ISBN10
6
-
7
- extend self
8
-
9
- def valid?(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
20
- end
21
-
22
- def correct_format?(isbn)
23
- isbn = dehyphenate(isbn)
24
- !!(/\A\d{9}[0-9X]\z/ =~ isbn)
25
- end
26
-
27
- def calc_check_digit(isbn)
28
- nums = dehyphenate(isbn).split("")[0, 9].map{|x| x.to_i }
29
- sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+)
30
- check_digit = 11 - (sum % 11)
31
- case check_digit
32
- when 10
33
- "X"
34
- when 11
35
- "0"
36
- else
37
- check_digit.to_s
38
- end
39
- end
40
-
41
- def dehyphenate(isbn)
42
- Petrarca.dehyphenate(isbn)
43
- end
44
-
45
- def hyphenate(isbn)
46
- split(isbn).join("-")
47
- end
48
-
49
- def split(isbn)
50
- Helpers.split("978" + isbn.to_s).drop(1)
51
- end
52
-
53
- end
54
- end
1
+ require "petrarca/helpers"
2
+
3
+
4
+ module Petrarca
5
+ module ISBN10
6
+
7
+ extend self
8
+
9
+ def valid?(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
20
+ end
21
+
22
+ def correct_format?(isbn)
23
+ isbn = dehyphenate(isbn)
24
+ !!(/\A\d{9}[0-9X]\z/ =~ isbn)
25
+ end
26
+
27
+ def calc_check_digit(isbn)
28
+ nums = dehyphenate(isbn).split("")[0, 9].map{|x| x.to_i }
29
+ sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+)
30
+ check_digit = 11 - (sum % 11)
31
+ case check_digit
32
+ when 10
33
+ "X"
34
+ when 11
35
+ "0"
36
+ else
37
+ check_digit.to_s
38
+ end
39
+ end
40
+
41
+ def dehyphenate(isbn)
42
+ Petrarca.dehyphenate(isbn)
43
+ end
44
+
45
+ def hyphenate(isbn)
46
+ split(isbn).join("-")
47
+ end
48
+
49
+ def split(isbn)
50
+ Helpers.split("978" + isbn.to_s).drop(1)
51
+ end
52
+
53
+ end
54
+ end
@@ -1,47 +1,47 @@
1
- require "petrarca/helpers"
2
-
3
-
4
- module Petrarca
5
- module ISBN13
6
-
7
- extend self
8
-
9
- def valid?(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
20
- end
21
-
22
- def correct_format?(isbn)
23
- isbn = dehyphenate(isbn)
24
- !!(/\A97[89]\d{9}\d\z/ =~ isbn)
25
- end
26
-
27
- def calc_check_digit(isbn)
28
- nums = dehyphenate(isbn).split("")[0, 12].map{|x| x.to_i }
29
- sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
30
- check_digit = 10 - (sum % 10)
31
- check_digit == 10 ? "0" : check_digit.to_s
32
- end
33
-
34
- def dehyphenate(isbn)
35
- Petrarca.dehyphenate(isbn)
36
- end
37
-
38
- def hyphenate(isbn)
39
- split(isbn).join("-")
40
- end
41
-
42
- def split(isbn)
43
- Helpers.split(isbn)
44
- end
45
-
46
- end
47
- end
1
+ require "petrarca/helpers"
2
+
3
+
4
+ module Petrarca
5
+ module ISBN13
6
+
7
+ extend self
8
+
9
+ def valid?(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
20
+ end
21
+
22
+ def correct_format?(isbn)
23
+ isbn = dehyphenate(isbn)
24
+ !!(/\A97[89]\d{9}\d\z/ =~ isbn)
25
+ end
26
+
27
+ def calc_check_digit(isbn)
28
+ nums = dehyphenate(isbn).split("")[0, 12].map{|x| x.to_i }
29
+ sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
30
+ check_digit = 10 - (sum % 10)
31
+ check_digit == 10 ? "0" : check_digit.to_s
32
+ end
33
+
34
+ def dehyphenate(isbn)
35
+ Petrarca.dehyphenate(isbn)
36
+ end
37
+
38
+ def hyphenate(isbn)
39
+ split(isbn).join("-")
40
+ end
41
+
42
+ def split(isbn)
43
+ Helpers.split(isbn)
44
+ end
45
+
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
- module Petrarca
2
- VERSION = "0.6.6"
3
- end
1
+ module Petrarca
2
+ VERSION = "1.0.0"
3
+ end