petrarca 0.2.0 → 0.4.3

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: 78b04a2cf61f74ad207570c95631cf1a2187be066ee096ab0a321799e45fb986
4
- data.tar.gz: 12625949933981779b0cc28a3b5267565e877a1e27dbe4ecfd9f92ecc68bee9a
3
+ metadata.gz: e4d096eb521f4211740b769c35d15b5c4a707c62e14bc0b6299523cc9d66cd06
4
+ data.tar.gz: 51aa0420797105c2ff2323cf77c50b1cb51344e9ed859e5bc2c54c497e0d20d5
5
5
  SHA512:
6
- metadata.gz: 58460aad6cbfc5b8c251a1dd9ab897cae10366cb46b8db8374c6242818364bca9875ce6bc3413262fc71a729f87ab1df87f5cd13fb4bc442e2bc1358634dff71
7
- data.tar.gz: b25f466050d74bf9b2b2182aeb2174c5e480b71c31de54fa1aed7ce5168c40cd1c975ff1194ff8d30c0539413212e42e84d4ae550754f32e159b5bcf408887ab
6
+ metadata.gz: 1d9c167c08d74014361c760f71c82b8e825dd4e0c693b8c28f10eeab05a38edde01db908c634089f36f646c42232d6f811fccb9d55da14b795093c419e2498b4
7
+ data.tar.gz: d468f62ba32ce0fe075f5c691cc8091d58a6efe0708cfd9b901d111431c2cd551aeaad9a542faf0e9d3efd746e265ffd267ebb2374f049271fcd6df16f2766ce
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /vendor/
10
10
  /Gemfile.lock
11
11
  *.txt
12
+ *.xml
data/README.md CHANGED
@@ -9,7 +9,10 @@ This library Petrarca provides some utility functions to manipulate ISBN numbers
9
9
 
10
10
  All functions support both ISBN-13 and ISBN-10.
11
11
 
12
- Note: Only registrant ranges for Japan are currently supported.
12
+ All ranges of registration groups and registrants are supported.
13
+ Those depends on 'RangeMessage.xml' file, downloaded from [International ISBN Agency](https://www.isbn-international.org/range_file_generation).
14
+
15
+ NOTE: Updated range files to latest version on April 19, 2021.
13
16
 
14
17
  ## Installation
15
18
 
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
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
data/lib/petrarca.rb CHANGED
@@ -59,12 +59,12 @@ module Petrarca
59
59
  end
60
60
 
61
61
  def to_10(isbn13)
62
- s = isbn13.to_s.delete("-")[3..11]
62
+ s = isbn13.to_s.delete("-")[3, 9]
63
63
  ISBN10.hyphenate(s + ISBN10.calc_check_digit(s))
64
64
  end
65
65
 
66
66
  def to_13(isbn10)
67
- s = "978" + isbn10.to_s.delete("-")[0..8]
67
+ s = "978" + isbn10.to_s.delete("-")[0, 9]
68
68
  ISBN13.hyphenate(s + ISBN13.calc_check_digit(s))
69
69
  end
70
70
 
@@ -1,34 +1,28 @@
1
1
  module Petrarca
2
2
  module Helpers
3
3
 
4
+ extend self
5
+
4
6
  def split_to_parts(body, ranges)
5
- g = ""
6
- ranges.each do |range_str|
7
+ ranges.map do |range_str|
7
8
  s, e = range_str.split("-")
8
- g = body[0..(s.size - 1)]
9
- break if Range.new(s.to_i, e.to_i).cover?(g.to_i)
10
- end
11
- [g, body[(g.size)..]]
12
- end
13
-
14
- def hyphenate13(isbn)
15
- ean_prefix = isbn[0..2]
16
- body = isbn[3..11]
17
- check_digit = isbn[12..12]
18
- registration_group, body = split_to_parts(body, REGISTRATION_GROUP_RANGES[ean_prefix])
19
- prefix = "#{ean_prefix}-#{registration_group}"
20
- registrant, publication = split_to_parts(body, REGISTRANT_RANGES[prefix])
21
- [ean_prefix, registration_group, registrant, publication, check_digit].join("-")
9
+ prefix = body[0, s.size]
10
+ if Range.new(s.to_i, e.to_i).cover?(prefix.to_i)
11
+ [prefix, body[(prefix.size)..]]
12
+ else
13
+ nil
14
+ end
15
+ end.compact.first
22
16
  end
23
17
 
24
18
 
25
- def self.load_ranges(range_file)
19
+ def load_ranges(range_file)
26
20
  ranges = {}
27
21
  File.open(range_file, "r") do |f|
28
22
  f.each_line do |line|
29
23
  next if line.start_with?("#")
30
24
  g, r = line.chomp.split(":")
31
- ranges[g] = r.split(",")
25
+ ranges[g] = r.split(",") unless r.nil?
32
26
  end
33
27
  end
34
28
  ranges
@@ -4,8 +4,6 @@ require "petrarca/helpers"
4
4
  module Petrarca
5
5
  module ISBN10
6
6
 
7
- include Helpers
8
-
9
7
  extend self
10
8
 
11
9
  def valid?(isbn)
@@ -18,7 +16,7 @@ module Petrarca
18
16
  end
19
17
 
20
18
  def calc_check_digit(isbn)
21
- nums = isbn.delete("-").split("")[0..8].map{|x| x.to_i }
19
+ nums = isbn.delete("-").split("")[0, 9].map{|x| x.to_i }
22
20
  sum = nums.zip((2..10).to_a.reverse).map{|x, y| x * y }.inject(:+)
23
21
  check_digit = 11 - (sum % 11)
24
22
  case check_digit
@@ -32,7 +30,7 @@ module Petrarca
32
30
  end
33
31
 
34
32
  def hyphenate(isbn)
35
- s = hyphenate13("978" + isbn)
33
+ s = ISBN13.hyphenate("978" + isbn)
36
34
  s.sub(/^978-/, "")
37
35
  end
38
36
 
@@ -4,8 +4,6 @@ require "petrarca/helpers"
4
4
  module Petrarca
5
5
  module ISBN13
6
6
 
7
- include Helpers
8
-
9
7
  extend self
10
8
 
11
9
  def valid?(isbn)
@@ -18,14 +16,20 @@ module Petrarca
18
16
  end
19
17
 
20
18
  def calc_check_digit(isbn)
21
- nums = isbn.delete("-").split("")[0..11].map{|x| x.to_i }
19
+ nums = isbn.delete("-").split("")[0, 12].map{|x| x.to_i }
22
20
  sum = nums.zip([1, 3] * 6).map{|x, y| x * y }.inject(:+)
23
21
  check_digit = 10 - (sum % 10)
24
22
  check_digit == 10 ? "0" : check_digit.to_s
25
23
  end
26
24
 
27
25
  def hyphenate(isbn)
28
- hyphenate13(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("-")
29
33
  end
30
34
 
31
35
  end
@@ -1,3 +1,3 @@
1
1
  module Petrarca
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.3"
3
3
  end
data/petrarca.gemspec CHANGED
@@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
13
13
  spec.description = %q{Library to manipulate ISBN numbers for Ruby.}
14
14
  spec.homepage = "https://github.com/takatoh/Petrarca"
15
15
 
16
+ spec.licenses = ["MIT"]
17
+
16
18
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
19
  # to allow pushing to a single host or delete this section to allow pushing to any host.
18
20
  # if spec.respond_to?(:metadata)
@@ -38,4 +40,5 @@ Gem::Specification.new do |spec|
38
40
  spec.add_development_dependency "bundler", "~> 2.0"
39
41
  spec.add_development_dependency "rake", "~> 10.0"
40
42
  spec.add_development_dependency "rspec", ">= 3.0.0"
43
+ spec.add_development_dependency "nokogiri"
41
44
  end
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+
3
+ require "nokogiri"
4
+ require "optparse"
5
+
6
+
7
+ RANGE_MESSAGE_FILE = "RangeMessage.xml"
8
+ REGISTRATION_GROUP_RANGES_FILE = "registration_group_ranges.txt"
9
+ REGISTRANT_RANGES_FILE = "registrant_ranges.txt"
10
+
11
+
12
+ def main
13
+ options = {
14
+ dir: "."
15
+ }
16
+ opts = OptionParser.new
17
+ opts.on("-d", "--dir=DIR", String, "Set directory, output range fiels into."){|v|
18
+ options[:dir] = v
19
+ }
20
+ opts.parse!
21
+
22
+ doc = File.open(RANGE_MESSAGE_FILE, "r"){|f| Nokogiri::XML(f) }
23
+
24
+ metadata = {
25
+ source: doc.xpath("//MessageSource").text,
26
+ date: doc.xpath("//MessageDate").text
27
+ }
28
+
29
+ registration_groups = extract_ranges(doc.xpath("//EAN.UCC"))
30
+ registrants = extract_ranges(doc.xpath("//Group"))
31
+
32
+ output_range_file(
33
+ registration_groups,
34
+ File.join(options[:dir], REGISTRATION_GROUP_RANGES_FILE),
35
+ metadata
36
+ )
37
+ output_range_file(
38
+ registrants,
39
+ File.join(options[:dir], REGISTRANT_RANGES_FILE),
40
+ metadata
41
+ )
42
+ end
43
+
44
+ def extract_ranges(nodes)
45
+ nodes.map do |node|
46
+ prefix = node.xpath("Prefix").text
47
+ agency = node.xpath("Agency").text
48
+ ranges = node.xpath("Rules/Rule").map do |rule|
49
+ length = rule.xpath("Length").text.to_i
50
+ range = if length > 0
51
+ rule.xpath("Range").text.split("-").map{|s| s[0, length] }.join("-")
52
+ else
53
+ nil
54
+ end
55
+ range
56
+ end.compact
57
+ {
58
+ "prefix" => prefix,
59
+ "agency" => agency,
60
+ "ranges" => ranges
61
+ }
62
+ end
63
+ end
64
+
65
+ def output_range_file(ranges, range_file, metadata)
66
+ File.open(range_file, "w") do |f|
67
+ f.puts "# Generated from RangeMessage.xml file, downloaded from https://www.isbn-international.org/range_file_generation."
68
+ f.puts "#"
69
+ f.puts "# " + metadata[:source]
70
+ f.puts "# " + metadata[:date]
71
+ f.puts "#"
72
+ ranges.each do |range|
73
+ f.puts "# " + range["agency"]
74
+ f.puts range["prefix"] + ":" + range["ranges"].join(",")
75
+ end
76
+ end
77
+ $stderr.puts "Generated: #{range_file}"
78
+ end
79
+
80
+
81
+
82
+ main
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.2.0
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - takatoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-30 00:00:00.000000000 Z
11
+ date: 2021-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Library to manipulate ISBN numbers for Ruby.
56
70
  email:
57
71
  - takatoh.m@gmail.com
@@ -75,8 +89,10 @@ files:
75
89
  - lib/petrarca/isbn13.rb
76
90
  - lib/petrarca/version.rb
77
91
  - petrarca.gemspec
92
+ - scripts/generate_range_files.rb
78
93
  homepage: https://github.com/takatoh/Petrarca
79
- licenses: []
94
+ licenses:
95
+ - MIT
80
96
  metadata: {}
81
97
  post_install_message:
82
98
  rdoc_options: []