Skip to content

Commit

Permalink
1.2.1: misc fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
julbouln committed Jan 9, 2021
1 parent 14d2819 commit bf480fa
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/onix/contributor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'onix/subset'
require 'onix/website'
require 'onix/professional_affiliation'
require 'onix/contributor_place'

module ONIX
Expand All @@ -26,7 +27,7 @@ class Contributor < SubsetDSL
# element "UnnamedPersons", :subset, :cardinality => 0..1
# elements "AlternativeName", :subset, :cardinality => 0..n
elements "ContributorDate", :subset, :shortcut => :dates, :cardinality => 0..n
# elements "ProfessionalAffiliation", :subset, :cardinality => 0..n
elements "ProfessionalAffiliation", :subset, :cardinality => 0..n
elements "Prize", :subset, :cardinality => 0..n
elements "BiographicalNote", :text, :shortcut => :biographies, :cardinality => 0..n
elements "Website", :subset, :cardinality => 0..n
Expand Down
5 changes: 5 additions & 0 deletions lib/onix/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def strpdate!(date_txt, date_format)
format = code_format

if code_format != text_format
puts "WARN incorrect date format #{text_format} != #{code_format}"
format = text_format
end

Expand Down Expand Up @@ -54,8 +55,12 @@ def format_from_code(code)
"%Y%m%d"
when "01"
"%Y%m"
when "02"
"%Y%%W"
when "05"
"%Y"
when "13"
"%Y%m%dT%H%M%S"
when "14"
"%Y%m%dT%H%M%S%z"
else
Expand Down
4 changes: 4 additions & 0 deletions lib/onix/onix_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class ONIXMessage < SubsetDSL
def_delegator :header, :default_currency_code
def_delegator :header, :sent_date_time

def header
@header || Header.new
end

def initialize
@products = []
@vault = {}
Expand Down
4 changes: 4 additions & 0 deletions lib/onix/product_part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def file_formats
@product_form_details.select { |fd| fd.code =~ /^E1.*/ }
end

def product_form_description
product_form_descriptions.first
end

# full Product if referenced in ONIXMessage
attr_accessor :product

Expand Down
6 changes: 6 additions & 0 deletions lib/onix/professional_affiliation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ONIX
class ProfessionalAffiliation < SubsetDSL
elements "ProfessionalPosition", :text, :cardinality => 0..n
element "Affiliation", :text, :cardinality => 0..1
end
end
2 changes: 1 addition & 1 deletion lib/onix/sales_restriction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module ONIX
class SalesRestriction < SubsetDSL
element "SalesRestrictionType", :subset, :shortcut => :type
elements "SalesOutlet", :subset
elements "SalesOutlet", :subset, :cardinality => 0..n
elements "SalesRestrictionNote", :text, :shortcut => :notes
element "StartDate", :date, :cardinality => 0..1
element "EndDate", :date, :cardinality => 0..1
Expand Down
22 changes: 17 additions & 5 deletions lib/onix/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ def self.serialize(xml, subset, tag, level = 0)
class Primitive
def self.serialize(xml, val, tag, level = 0)
if val.is_a?(ONIX::TextWithAttributes)
xml.send(tag, val.serialized_attributes) do
xml.__send__ :insert, val.to_s
if val.serialized_attributes["textformat"] == "05" # content is XHTML
xml.send(tag, val.serialized_attributes) do
xml.__send__ :insert, val.to_s
end
else
xml.send(tag, val.serialized_attributes, val)
end
else
xml.send(tag, val)
Expand Down Expand Up @@ -121,7 +125,7 @@ def self.serialize(xml, date, parent_tag = nil, level = 0)
if deprecated_date_format
xml.Date(date.date.strftime(code_format))
else
attrs = date.date_format ? {:dateformat => date_format.code} : {}
attrs = date.date_format ? { :dateformat => date_format.code } : {}
xml.Date(date.date.strftime(code_format), attrs)
end
else
Expand Down Expand Up @@ -158,15 +162,23 @@ def self.serialize(io, subset, tag, level = 0)
class Subset
def self.serialize(io, subset, tag, level = 0)
io.write " " * level
io.write "#{tag}:\n"
if subset.attributes.length > 0
io.write "#{tag}[#{subset.attributes.to_a.map { |k, v| "#{k}: #{v.code}(#{v.human})" }.join(", ")}]\n"
else
io.write "#{tag}:\n"
end
ONIX::Serializer::Traverser.recursive_serialize(Dump, io, subset, tag, level + 1)
end
end

class Primitive
def self.serialize(io, val, tag, level = 0)
io.write " " * level
io.write "#{tag}: #{val}\n"
if val.is_a?(ONIX::TextWithAttributes)
io.write "#{tag}[#{val.attributes.to_a.map { |k, v| "#{k}: #{v.code}(#{v.human})" }.join(", ")}]: #{val.to_s}\n"
else
io.write "#{tag}: #{val}\n"
end
end
end

Expand Down
15 changes: 6 additions & 9 deletions lib/onix/subset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def parse_attributes(attrs)
attrs.each do |k, v|
attr_klass = Attributes.attribute_class(k.to_s)
@attributes[k.to_s] = attr_klass ? attr_klass.from_code(v.to_s) : nil
#puts "PARSE ATTR #{k}(#{attr_klass}) = #{v}(#{@attributes[k.to_s]})"
end
end
end
Expand All @@ -78,8 +77,8 @@ def self.parse(n)
def parse(n) end

def unsupported(tag)
# raise SubsetUnsupported,tag.name
# puts "SubsetUnsupported: #{self.class}##{tag.name} (#{ShortToRef.names[tag.name]})"
# raise SubsetUnsupported, [self.class, tag.name]
# puts "WARN subset tag unsupported #{self.class}##{tag.name} (#{self.class.short_to_ref(tag.name)})"
end

def tag_match(v)
Expand Down Expand Up @@ -418,7 +417,10 @@ def parse(n)

if val
if primitive && t.attributes.length > 0
val = TextWithAttributes.new(t.attributes["textformat"] ? t.children.map { |x| x.to_s }.join.strip : val)
if t.attributes["textformat"] && t.attributes["textformat"].to_s == "05" # content is XHTML
val = t.children.map { |x| x.to_s }.join.strip
end
val = TextWithAttributes.new(val)
val.parse(t.attributes)
end

Expand All @@ -434,10 +436,5 @@ def parse(n)
end
end
end

def unsupported(tag)
# raise SubsetUnsupported, [self.class, tag.name]
#puts "SubsetUnsupported: #{self.class}##{tag.name} (#{self.class.short_to_ref(tag.name)})"
end
end
end
4 changes: 4 additions & 0 deletions lib/onix/supply_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class SupplyDetail < SubsetDSL
element "ProductAvailability", :subset, :shortcut => :availability, :cardinality => 1
elements "SupplyDate", :subset, :cardinality => 0..n
element "OrderTime", :integer, :cardinality => 0..1
# element "NewSupplier", :subset, :cardinality => 0..1
# elements "Stock", :subset, :cardinality => 0..n
element "PackQuantity", :integer, :cardinality => 0..1
element "PalletQuantity", :integer, :cardinality => 0..1
element "OrderQuantityMinimum", :integer, :cardinality => 0..1
# element "OrderQuantityMinimum", :integer, :cardinality => 0..1
element "OrderQuantityMultiple", :integer, :cardinality => 0..1
element "UnpricedItemType", :subset, :cardinality => 0..1
elements "Price", :subset, :cardinality => 0..n
# element "Reissue", :subset, :cardinality => 0..1

# @!group Shortcuts
def distributors
Expand Down
2 changes: 1 addition & 1 deletion lib/onix/supporting_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class SupportingResource < SubsetDSL
element "ContentAudience", :subset, :shortcut => :target_audience, :cardinality => 1..n
element "Territory", :subset, :cardinality => 0..1
element "ResourceMode", :subset, :shortcut => :mode, :cardinality => 1
elements "ResourceVersion", :subset, :shortcut => :versions, :cardinality => 1..n
elements "ResourceFeature", :subset, :shortcut => :features, :cardinality => 0..n
elements "ResourceVersion", :subset, :shortcut => :versions, :cardinality => 1..n

scope :front_cover, lambda { human_code_match(:resource_content_type, "FrontCover") }
scope :sample_content, lambda { human_code_match(:resource_content_type, "SampleContent") }
Expand Down
18 changes: 17 additions & 1 deletion test/test_serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,24 @@ class TestSerialize < Minitest::Test
builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
ONIX::Serializer::Default.serialize(xml, @message)
end
assert_equal Nokogiri::XML.parse(builder.to_xml,&:noblanks).to_xml, Nokogiri::XML.parse(File.read(@filename),&:noblanks).to_xml
end
end

context "full ONIX file 2" do
setup do
@filename = "../immateriel/test/fixtures/onix/hachette.xml"
@message = ONIX::ONIXMessage.new
@message.parse(@filename)
@product = @message.products.first
end
should "be the same serialized" do
assert_equal 1, @message.products.length
builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
ONIX::Serializer::Default.serialize(xml, @product, "Product")
end
#assert_equal builder.to_xml, File.read(@filename)
assert_equal builder.to_xml.gsub(/\>\s+\</, "><"), File.read(@filename).gsub(/\>\s+\</, "><")
#assert_equal builder.to_xml.gsub(/\>\s+\</, "><"), File.read(@filename).gsub(/\>\s+\</, "><")
end
end

Expand Down

0 comments on commit bf480fa

Please sign in to comment.