Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancements for Onix 3 support #108

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
7 changes: 6 additions & 1 deletion lib/onix/contributor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid API break, it would be nice to add a singular role method like for place https://github.com/immateriel/im_onix/blob/master/lib/onix/contributor.rb#L49

elements "FromLanguage", :subset, :klass => "LanguageCode", :cardinality => 0..n
elements "ToLanguage", :subset, :klass => "LanguageCode", :cardinality => 0..n

Expand Down Expand Up @@ -50,6 +50,11 @@ def place
self.places.first
end

# @return [ContributorRole]
def role
self.roles.first
end

# !@endgroup

# @!group High level
Expand Down
21 changes: 18 additions & 3 deletions lib/onix/onix_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add

return unless @reader

before the iteration to avoid an exception if reader has not been called ?

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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/onix/subset.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'forwardable'
require 'cgi'
require 'delegate'

module ONIX
class ShortToRef
Expand Down Expand Up @@ -492,4 +493,4 @@ def parse(n)
end
end
end
end
end