From 993c6e290cf703b4ce3c4babd7db530c4dcf00d3 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 15 Nov 2022 15:14:51 -0500 Subject: [PATCH 1/5] support for ruby 2.7 --- lib/onix/subset.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/onix/subset.rb b/lib/onix/subset.rb index 2bc9493..3f04c3b 100644 --- a/lib/onix/subset.rb +++ b/lib/onix/subset.rb @@ -1,5 +1,6 @@ require 'forwardable' require 'cgi' +require 'delegate' module ONIX class ShortToRef @@ -492,4 +493,4 @@ def parse(n) end end end -end \ No newline at end of file +end From 951b1f7746150055881e3bf5d0821891448b5e18 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 23 Nov 2022 11:01:02 -0500 Subject: [PATCH 2/5] support many contributor roles --- lib/onix/contributor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/onix/contributor.rb b/lib/onix/contributor.rb index 5c57d64..a13f7a2 100644 --- a/lib/onix/contributor.rb +++ b/lib/onix/contributor.rb @@ -6,7 +6,7 @@ module ONIX class Contributor < SubsetDSL element "SequenceNumber", :integer, :cardinality => 0..1 - element "ContributorRole", :subset, :shortcut => :role, :cardinality => 1..n + elements "ContributorRole", :subset, :shortcut => :roles, :cardinality => 1..n elements "FromLanguage", :subset, :klass => "LanguageCode", :cardinality => 0..n elements "ToLanguage", :subset, :klass => "LanguageCode", :cardinality => 0..n From daa36d47094ba7b580babaa94d734751c28c805f Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 23 Nov 2022 13:13:24 -0500 Subject: [PATCH 3/5] reader for less memory --- lib/onix/onix_message.rb | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/onix/onix_message.rb b/lib/onix/onix_message.rb index 29b252c..9dde0c4 100644 --- a/lib/onix/onix_message.rb +++ b/lib/onix/onix_message.rb @@ -104,6 +104,19 @@ def open(arg, force_encoding = nil) xml end + def reader(file_path) + @reader = Nokogiri::XML::Reader(File.open(file_path, 'r')) + end + + def each(&block) + @reader.each do |node| + if @reader.node_type == 1 && @reader.name == "Product" + element = Nokogiri::XML(@reader.outer_xml).at('Product') + yield self.product_klass.parse(element) + end + end + end + # release as an integer eg: 210, 300, 301 # @return [Number] def version @@ -125,13 +138,15 @@ def detect_release(element) end def set_release_from_xml(node, force_release) + if force_release + @release = force_release.to_s + return + end + @release = node["release"] unless @release @release = detect_release(node) end - if force_release - @release = force_release.to_s - end end def product_klass From c950b9d9820f4ced00f850734c752c245e618f14 Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 28 Nov 2022 10:24:30 -0500 Subject: [PATCH 4/5] add role shortcut --- lib/onix/contributor.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/onix/contributor.rb b/lib/onix/contributor.rb index a13f7a2..7741928 100644 --- a/lib/onix/contributor.rb +++ b/lib/onix/contributor.rb @@ -50,6 +50,11 @@ def place self.places.first end + # @return [ContributorRole] + def role + self.roles.first + end + # !@endgroup # @!group High level From 8b36748ffbee3fd58657fc9978848f9cb75ea93f Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 28 Nov 2022 10:35:31 -0500 Subject: [PATCH 5/5] README note --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 9fe8528..6103be4 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,19 @@ pp message.products.first.identifiers.first Ruby elements variables are underscored, lowercase of ONIX tags (Product -> product, DescriptiveDetail -> descriptive_detail) and pluralized in case of array (ProductSupply -> product_supplies). +#### Using reader + +Able to optionally stream each product rather than read the full ONIX file at once. This can be helpful for large ONIX files. + +nb. The version will need to be explicitly, and each `product` is yielded exactly once. For more info on this pattern see [Nokogiri::XML::Reader](https://nokogiri.org/rdoc/Nokogiri/XML/Reader.html) + +```ruby +onix_message = ONIX::ONIXMessage.new +onix_message.set_release_from_xml({}, '3.0') +onix_message.reader("onix_file.xml") +onix_message.each { |product| ...handle product... } +``` + ### High level API High level methods give abstracted and simplified access to the most important data. See https://www.rubydoc.info/gems/im_onix/ONIX/Product for high level API rdoc and onix_pp.rb, onix3_to_onix2.rb and onix3_to_onix3.rb sample in bin/