Class: ONIX::Reader
Overview
This is the primary class for reading data from an ONIX file, and there’s really not much to it
Each file should contain a single header, and 1 or more products:
reader = ONIX::Reader.new("somefile.xml")
puts reader.header.inspect
reader.each do |product|
puts product.inspect
end
The header will be returned as an ONIX::Header object, and the product will be an ONIX::Product.
The ONIX::Product class can be a bit of a hassle to work with, as data can be nested in it fairly deeply. To wrap all the products returned by the reader in a shim that provides simple accessor access to common attributes, pass the shim class as a second argument
reader = ONIX::Reader.new("somefile.xml", ONIX::APAProduct)
puts reader.header.inspect
reader.each do |product|
puts product.inspect
end
APAProduct stands for Australian Publishers Association and provides simple access to the ONIX attributes that are commonly used in the Australian market.
As well as accessing the file header, there are handful of other read only attributes that might be useful
reader = ONIX::Reader.new("somefile.xml", ONIX::APAProduct)
puts reader.version
puts reader.xml_lang
puts reader.xml_version
puts reader.encoding
The version attribute is particuarly useful. There are multiple revisions of the ONIX spec, and you may need to handle the file differently based on what version it is.
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#release ⇒ Object
readonly
Returns the value of attribute release.
Instance Method Summary collapse
- #close ⇒ Object
-
#each(&block) ⇒ Object
Iterate over all the products in an ONIX file.
-
#initialize(input, product_klass = ::ONIX::Product) ⇒ Reader
constructor
A new instance of Reader.
Constructor Details
#initialize(input, product_klass = ::ONIX::Product) ⇒ Reader
Returns a new instance of Reader.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/onix/reader.rb', line 58 def initialize(input, product_klass = ::ONIX::Product) if input.kind_of?(String) @file = File.open(input, "r") @reader = Nokogiri::XML::Reader(@file) { |cfg| cfg.dtdload.noent } elsif input.kind_of?(IO) @reader = Nokogiri::XML::Reader(input) { |cfg| cfg.dtdload.noent } else raise ArgumentError, "Unable to read from file or IO stream" end @product_klass = product_klass @release = find_release @header = find_header @xml_lang ||= @reader.lang @xml_version ||= @reader.xml_version.to_f end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
56 57 58 |
# File 'lib/onix/reader.rb', line 56 def header @header end |
#release ⇒ Object (readonly)
Returns the value of attribute release.
56 57 58 |
# File 'lib/onix/reader.rb', line 56 def release @release end |
Instance Method Details
#close ⇒ Object
92 93 94 |
# File 'lib/onix/reader.rb', line 92 def close @reader.close if @reader end |
#each(&block) ⇒ Object
Iterate over all the products in an ONIX file
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/onix/reader.rb', line 79 def each(&block) @reader.each do |node| if @reader.node_type == 1 && @reader.name == "Product" str = @reader.outer_xml if str.nil? yield @product_klass.new else yield @product_klass.from_xml(str) end end end end |