isbnranges 0.3.0 → 2024.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,9 @@
1
- # Generated from RangeMessage.xml file, downloaded from https://www.isbn-international.org/range_file_generation.
2
- #
3
- # International ISBN Agency
4
- # Tue, 13 Aug 2024 06:15:38 BST
5
- #
6
- # International ISBN Agency
7
- 978:0-5,600-649,65-65,7-7,80-94,950-989,9900-9989,99900-99999:International ISBN Agency
8
- # International ISBN Agency
9
- 979:10-15,8-8:International ISBN Agency
1
+ # Generated from RangeMessage.xml file, downloaded from https://www.isbn-international.org/range_file_generation.
2
+ #
3
+ # International ISBN Agency
4
+ # Sat, 5 Oct 2024 05:57:37 BST
5
+ #
6
+ # International ISBN Agency
7
+ 978:0-5,600-649,65-65,7-7,80-94,950-989,9900-9989,99900-99999:International ISBN Agency
8
+ # International ISBN Agency
9
+ 979:10-15,8-8:International ISBN Agency
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ISBNRanges
4
- VERSION = "0.3.0"
4
+ VERSION = "2024.10"
5
5
  end
data/lib/isbnranges.rb CHANGED
@@ -1,34 +1,34 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "isbnranges/version"
4
- require_relative "isbnranges/datafiles"
5
- require "date"
6
-
7
- module ISBNRanges
8
- # class Error < StandardError; end
9
- # Your code goes here...
10
-
11
- extend self
12
-
13
- def load_ranges(range_file)
14
- ranges = {}
15
- agencies = {}
16
- File.open(range_file, "r") do |f|
17
- f.each_line do |line|
18
- next if line.start_with?("#")
19
- g, r, a = line.chomp.split(":")
20
- ranges[g] = r.split(",").map{|r| r.split("-")} unless r.nil?
21
- agencies[g] = a unless r.nil?
22
- end
23
- end
24
- [ranges, agencies]
25
- end
26
-
27
- data_dir = __dir__ + "/../data"
28
- registration_group_ranges_file = File.join(data_dir, ISBNRanges::REGISTRATION_GROUP_RANGES_FILE)
29
- registrant_ranges_file = File.join(data_dir, ISBNRanges::REGISTRANT_RANGES_FILE)
30
- range_date_file = File.join(data_dir, ISBNRanges::RANGE_DATE_FILE)
31
- REGISTRATION_GROUP_RANGES, _ = load_ranges(registration_group_ranges_file)
32
- REGISTRANT_RANGES, REGISTRATION_GROUP_AGENCY = load_ranges(registrant_ranges_file)
33
- RANGE_DATE = Date.parse(File.read(range_date_file))
34
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "isbnranges/version"
4
+ require_relative "isbnranges/datafiles"
5
+ require "date"
6
+
7
+ module ISBNRanges
8
+ # class Error < StandardError; end
9
+ # Your code goes here...
10
+
11
+ extend self
12
+
13
+ def load_ranges(range_file)
14
+ ranges = {}
15
+ agencies = {}
16
+ File.open(range_file, "r") do |f|
17
+ f.each_line do |line|
18
+ next if line.start_with?("#")
19
+ g, r, a = line.chomp.split(":")
20
+ ranges[g] = r.split(",").map{|r| r.split("-")} unless r.nil?
21
+ agencies[g] = a unless r.nil?
22
+ end
23
+ end
24
+ [ranges, agencies]
25
+ end
26
+
27
+ data_dir = __dir__ + "/../data"
28
+ registration_group_ranges_file = File.join(data_dir, ISBNRanges::REGISTRATION_GROUP_RANGES_FILE)
29
+ registrant_ranges_file = File.join(data_dir, ISBNRanges::REGISTRANT_RANGES_FILE)
30
+ range_date_file = File.join(data_dir, ISBNRanges::RANGE_DATE_FILE)
31
+ REGISTRATION_GROUP_RANGES, _ = load_ranges(registration_group_ranges_file)
32
+ REGISTRANT_RANGES, REGISTRATION_GROUP_AGENCY = load_ranges(registrant_ranges_file)
33
+ RANGE_DATE = Date.parse(File.read(range_date_file))
34
+ end
@@ -1,83 +1,83 @@
1
- # encoding: utf-8
2
-
3
- require_relative "../lib/isbnranges/datafiles"
4
- require "nokogiri"
5
- require "optparse"
6
-
7
-
8
- def main
9
- options = {
10
- dir: "."
11
- }
12
- opts = OptionParser.new
13
- opts.on("-d", "--dir=DIR", String, "Set directory, output range fiels into."){|v|
14
- options[:dir] = v
15
- }
16
- opts.parse!
17
-
18
- doc = File.open(ISBNRanges::RANGE_MESSAGE_FILE, "r"){|f| Nokogiri::XML(f) }
19
-
20
- metadata = {
21
- source: doc.xpath("//MessageSource").text,
22
- date: doc.xpath("//MessageDate").text
23
- }
24
-
25
- registration_groups = extract_ranges(doc.xpath("//EAN.UCC"))
26
- registrants = extract_ranges(doc.xpath("//Group"))
27
-
28
- output_range_file(
29
- registration_groups,
30
- File.join(options[:dir], ISBNRanges::REGISTRATION_GROUP_RANGES_FILE),
31
- metadata
32
- )
33
- output_range_file(
34
- registrants,
35
- File.join(options[:dir], ISBNRanges::REGISTRANT_RANGES_FILE),
36
- metadata
37
- )
38
- range_date_file = File.join(options[:dir], ISBNRanges::RANGE_DATE_FILE)
39
- File.open(range_date_file, "w") do |f|
40
- f.puts metadata[:date]
41
- end
42
- $stderr.puts "Generated: #{range_date_file}"
43
- end
44
-
45
- def extract_ranges(nodes)
46
- nodes.map do |node|
47
- prefix = node.xpath("Prefix").text
48
- agency = node.xpath("Agency").text
49
- ranges = node.xpath("Rules/Rule").map do |rule|
50
- length = rule.xpath("Length").text.to_i
51
- range = if length > 0
52
- rule.xpath("Range").text.split("-").map{|s| s[0, length] }.join("-")
53
- else
54
- nil
55
- end
56
- range
57
- end.compact
58
- {
59
- "prefix" => prefix,
60
- "agency" => agency,
61
- "ranges" => ranges
62
- }
63
- end
64
- end
65
-
66
- def output_range_file(ranges, range_file, metadata)
67
- File.open(range_file, "w") do |f|
68
- f.puts "# Generated from RangeMessage.xml file, downloaded from https://www.isbn-international.org/range_file_generation."
69
- f.puts "#"
70
- f.puts "# " + metadata[:source]
71
- f.puts "# " + metadata[:date]
72
- f.puts "#"
73
- ranges.each do |range|
74
- f.puts "# " + range["agency"]
75
- f.puts [range["prefix"], range["ranges"].join(","), range["agency"]].join(":")
76
- end
77
- end
78
- $stderr.puts "Generated: #{range_file}"
79
- end
80
-
81
-
82
-
83
- main
1
+ # encoding: utf-8
2
+
3
+ require_relative "../lib/isbnranges/datafiles"
4
+ require "nokogiri"
5
+ require "optparse"
6
+
7
+
8
+ def main
9
+ options = {
10
+ dir: "."
11
+ }
12
+ opts = OptionParser.new
13
+ opts.on("-d", "--dir=DIR", String, "Set directory, output range fiels into."){|v|
14
+ options[:dir] = v
15
+ }
16
+ opts.parse!
17
+
18
+ doc = File.open(ISBNRanges::RANGE_MESSAGE_FILE, "r"){|f| Nokogiri::XML(f) }
19
+
20
+ metadata = {
21
+ source: doc.xpath("//MessageSource").text,
22
+ date: doc.xpath("//MessageDate").text
23
+ }
24
+
25
+ registration_groups = extract_ranges(doc.xpath("//EAN.UCC"))
26
+ registrants = extract_ranges(doc.xpath("//Group"))
27
+
28
+ output_range_file(
29
+ registration_groups,
30
+ File.join(options[:dir], ISBNRanges::REGISTRATION_GROUP_RANGES_FILE),
31
+ metadata
32
+ )
33
+ output_range_file(
34
+ registrants,
35
+ File.join(options[:dir], ISBNRanges::REGISTRANT_RANGES_FILE),
36
+ metadata
37
+ )
38
+ range_date_file = File.join(options[:dir], ISBNRanges::RANGE_DATE_FILE)
39
+ File.open(range_date_file, "w") do |f|
40
+ f.puts metadata[:date]
41
+ end
42
+ $stderr.puts "Generated: #{range_date_file}"
43
+ end
44
+
45
+ def extract_ranges(nodes)
46
+ nodes.map do |node|
47
+ prefix = node.xpath("Prefix").text
48
+ agency = node.xpath("Agency").text
49
+ ranges = node.xpath("Rules/Rule").map do |rule|
50
+ length = rule.xpath("Length").text.to_i
51
+ range = if length > 0
52
+ rule.xpath("Range").text.split("-").map{|s| s[0, length] }.join("-")
53
+ else
54
+ nil
55
+ end
56
+ range
57
+ end.compact
58
+ {
59
+ "prefix" => prefix,
60
+ "agency" => agency,
61
+ "ranges" => ranges
62
+ }
63
+ end
64
+ end
65
+
66
+ def output_range_file(ranges, range_file, metadata)
67
+ File.open(range_file, "w") do |f|
68
+ f.puts "# Generated from RangeMessage.xml file, downloaded from https://www.isbn-international.org/range_file_generation."
69
+ f.puts "#"
70
+ f.puts "# " + metadata[:source]
71
+ f.puts "# " + metadata[:date]
72
+ f.puts "#"
73
+ ranges.each do |range|
74
+ f.puts "# " + range["agency"]
75
+ f.puts [range["prefix"], range["ranges"].join(","), range["agency"]].join(":")
76
+ end
77
+ end
78
+ $stderr.puts "Generated: #{range_file}"
79
+ end
80
+
81
+
82
+
83
+ main
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isbnranges
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: '2024.10'
5
5
  platform: ruby
6
6
  authors:
7
7
  - takatoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-02 00:00:00.000000000 Z
11
+ date: 2024-10-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  requirements: []
51
- rubygems_version: 3.5.9
51
+ rubygems_version: 3.4.19
52
52
  signing_key:
53
53
  specification_version: 4
54
54
  summary: Range definitions of ISBN registration groups and registrants.