onix 0.7.8 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/CHANGELOG +14 -0
  2. data/lib/onix.rb +2 -16
  3. data/lib/onix/addressee_identifier.rb +0 -1
  4. data/lib/onix/apa_product.rb +5 -1
  5. data/lib/onix/audience_range.rb +0 -1
  6. data/lib/onix/contributor.rb +0 -1
  7. data/lib/onix/header.rb +0 -1
  8. data/lib/onix/imprint.rb +0 -1
  9. data/lib/onix/language.rb +0 -1
  10. data/lib/onix/market_representation.rb +0 -1
  11. data/lib/onix/measure.rb +0 -1
  12. data/lib/onix/media_file.rb +0 -1
  13. data/lib/onix/other_text.rb +0 -1
  14. data/lib/onix/price.rb +0 -1
  15. data/lib/onix/product.rb +0 -1
  16. data/lib/onix/product_identifier.rb +0 -1
  17. data/lib/onix/publisher.rb +0 -1
  18. data/lib/onix/reader.rb +24 -99
  19. data/lib/onix/sales_restriction.rb +0 -1
  20. data/lib/onix/sender_identifier.rb +0 -1
  21. data/lib/onix/series.rb +0 -1
  22. data/lib/onix/series_identifier.rb +0 -1
  23. data/lib/onix/stock.rb +0 -1
  24. data/lib/onix/subject.rb +0 -1
  25. data/lib/onix/supply_detail.rb +0 -1
  26. data/lib/onix/title.rb +0 -1
  27. data/lib/onix/website.rb +0 -1
  28. data/spec/apa_product_spec.rb +34 -4
  29. data/spec/audience_range_spec.rb +2 -4
  30. data/spec/contributor_spec.rb +2 -4
  31. data/spec/header_spec.rb +2 -4
  32. data/spec/imprint_spec.rb +4 -6
  33. data/spec/language_spec.rb +2 -4
  34. data/spec/market_representation_spec.rb +2 -4
  35. data/spec/measure_spec.rb +2 -4
  36. data/spec/media_file_spec.rb +2 -4
  37. data/spec/normaliser_spec.rb +1 -3
  38. data/spec/other_text_spec.rb +2 -4
  39. data/spec/price_spec.rb +2 -4
  40. data/spec/product_identifier_spec.rb +2 -4
  41. data/spec/product_spec.rb +2 -5
  42. data/spec/publisher_spec.rb +2 -4
  43. data/spec/reader_spec.rb +3 -5
  44. data/spec/sales_restriction_spec.rb +2 -4
  45. data/spec/sender_identifier.rb +2 -4
  46. data/spec/series_identifier_spec.rb +2 -4
  47. data/spec/series_spec.rb +2 -4
  48. data/spec/spec_helper.rb +7 -0
  49. data/spec/stock_spec.rb +2 -4
  50. data/spec/subject_spec.rb +2 -4
  51. data/spec/supply_detail_spec.rb +2 -4
  52. data/spec/title_spec.rb +2 -4
  53. data/spec/website_spec.rb +2 -4
  54. data/spec/writer_spec.rb +1 -4
  55. metadata +93 -92
  56. data/lib/onix/common.rb +0 -26
data/CHANGELOG CHANGED
@@ -1,3 +1,17 @@
1
+ v0.8.0 (31st October 2009)
2
+ - Replace LibXML dependency with Nokogiri. Nokogiri is under active development, has
3
+ a responsive maintainer and is significantly more stable
4
+ - Switch to ROXML 3.x
5
+ - roxml also switched from libxml to nokogiri
6
+ - roxml removed deprecated parts of it's API
7
+ - should now avoid various conflicts with mongrel
8
+ - Ensure APAProduct#price returns the first product price and ignores
9
+ the price type
10
+
11
+ v0.7.8 (19th October 2009)
12
+ - add support for additional elements (mostly series and audience related)
13
+ - thanks tim
14
+
1
15
  v0.7.7 (1st October 2009)
2
16
  - optimise sed usage in ONIX::Normaliser. *huge* speed improvement on
3
17
  large files.
data/lib/onix.rb CHANGED
@@ -1,22 +1,15 @@
1
1
  # coding: utf-8
2
2
 
3
- require 'rubygems'
4
3
  require 'bigdecimal'
5
4
  require 'cgi'
6
-
7
- # ensure we load the correct gem versions
8
- gem 'roxml', '2.5.3'
9
- gem 'andand'
10
-
11
- # and now load the actual gems
12
5
  require 'roxml'
13
6
  require 'andand'
14
7
 
15
8
  module ONIX
16
9
  module Version #:nodoc:
17
10
  Major = 0
18
- Minor = 7
19
- Tiny = 8
11
+ Minor = 8
12
+ Tiny = 0
20
13
 
21
14
  String = [Major, Minor, Tiny].join('.')
22
15
  end
@@ -60,13 +53,6 @@ module ONIX
60
53
  end
61
54
  end
62
55
 
63
- # silence some warnings from ROXML
64
- unless ROXML.const_defined?("SILENCE_XML_NAME_WARNING")
65
- ROXML::SILENCE_XML_NAME_WARNING = true
66
- end
67
-
68
- require File.join(File.dirname(__FILE__), "onix", "common")
69
-
70
56
  # core files
71
57
  # - ordering is important, classes need to be defined before any
72
58
  # other class can use them
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class AddresseeIdentifier
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_accessor :addressee_id_type, :from => "AddresseeIDType", :as => Fixnum # should be a 2 digit num
9
8
  xml_accessor :id_type_name, :from => "IDTypeName"
@@ -651,7 +651,11 @@ module ONIX
651
651
  # retrieve the value of a particular price
652
652
  def price_get(type)
653
653
  supply = find_or_create_supply_detail
654
- supply.prices.find { |p| p.price_type_code == type }
654
+ if type.nil?
655
+ supply.prices.first
656
+ else
657
+ supply.prices.find { |p| p.price_type_code == type }
658
+ end
655
659
  end
656
660
 
657
661
  # set the value of a particular price
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class AudienceRange
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "AudienceRange"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Contributor
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Contributor"
9
8
 
data/lib/onix/header.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Header
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Header"
9
8
 
data/lib/onix/imprint.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Imprint
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Imprint"
9
8
 
data/lib/onix/language.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Language
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Language"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class MarketRepresentation
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "MarketRepresentation"
9
8
 
data/lib/onix/measure.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Measure
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Measure"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class MediaFile
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "MediaFile"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class OtherText
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "OtherText"
9
8
 
data/lib/onix/price.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Price
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Price"
9
8
 
data/lib/onix/product.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Product
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Product"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class ProductIdentifier
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "ProductIdentifier"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Publisher
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Publisher"
9
8
 
data/lib/onix/reader.rb CHANGED
@@ -53,31 +53,38 @@ module ONIX
53
53
  class Reader
54
54
  include Enumerable
55
55
 
56
- attr_reader :header , :version, :xml_lang, :xml_version, :encoding
56
+ attr_reader :header , :version, :xml_lang, :xml_version
57
57
 
58
58
  def initialize(input, product_klass = ::ONIX::Product)
59
59
  if input.kind_of?(String)
60
- @reader = LibXML::XML::Reader.file(input)
60
+ @file = File.open(input, "r")
61
+ @reader = Nokogiri::XML::Reader.from_io(@file)
61
62
  elsif input.kind_of?(IO)
62
- @reader = LibXML::XML::Reader.io(input)
63
+ @reader = Nokogiri::XML::Reader.from_io(input)
63
64
  else
64
65
  raise ArgumentError, "Unable to read from file or IO stream"
65
66
  end
66
67
 
67
68
  @product_klass = product_klass
68
69
 
69
- @header = read_next
70
+ @header = find_header
70
71
 
71
- @xml_lang ||= @reader.xml_lang
72
+ @xml_lang ||= @reader.lang
72
73
  @xml_version ||= @reader.xml_version.to_f
73
- @encoding ||= encoding_const_to_name(@reader.encoding)
74
74
  end
75
75
 
76
76
  # Iterate over all the products in an ONIX file
77
77
  #
78
78
  def each(&block)
79
- while obj = read_next
80
- yield obj
79
+ @reader.each do |node|
80
+ if @reader.node_type == 1 && @reader.name == "Product"
81
+ str = @reader.outer_xml
82
+ if str.nil?
83
+ yield @product_klass.new
84
+ else
85
+ yield @product_klass.from_xml(str)
86
+ end
87
+ end
81
88
  end
82
89
  end
83
90
 
@@ -87,101 +94,19 @@ module ONIX
87
94
 
88
95
  private
89
96
 
90
- # Walk the ONIX file, and grab the next header or product fragment.
91
- #
92
- def read_next
93
- while @reader.read
94
-
95
- if @reader.node_type == LibXML::XML::Reader::TYPE_DOCUMENT_TYPE
96
- # TODO restore ONIX version extraction. The following expand()
97
- # call is triggering unpredictable behaviour in libxml-ruby
98
- # 1.1.3 with libxml2 2.7.3. Sometimes segfaults, othertimes
99
- # cryptic errors about the input file being truncated or
100
- # incomplete
101
- #uri = @reader.expand.to_s.dup
102
- #m, major, minor, rev = *uri.match(/.+(\d)\.(\d)\/(\d*).*/)
103
- #@version = [major.to_i, minor.to_i, rev.to_i]
104
- elsif @reader.node_type == LibXML::XML::Reader::TYPE_ELEMENT
105
- if @reader.name == "Header"
106
- str = normalise_string_encoding(@reader.read_outer_xml.to_s.dup)
107
- if str.size == 0
108
- return ONIX::Header.new
109
- else
110
- return ONIX::Header.from_xml(str)
111
- end
112
- elsif @reader.name == "Product"
113
- str = normalise_string_encoding(@reader.read_outer_xml.to_s.dup)
114
- if str.size == 0
115
- return @product_klass.new
116
- else
117
- return @product_klass.from_xml(str)
118
- end
97
+ def find_header
98
+ 100.times do
99
+ @reader.read
100
+ if @reader.node_type == 1 && @reader.name == "Header"
101
+ str = @reader.outer_xml
102
+ if str.nil?
103
+ return ONIX::Header.new
104
+ else
105
+ return ONIX::Header.from_xml(str)
119
106
  end
120
107
  end
121
108
  end
122
-
123
- return nil
124
- rescue LibXML::XML::Error => e
125
109
  return nil
126
110
  end
127
-
128
- # XML::Reader seems to transparently convert all input data to utf-8, howver
129
- # on Ruby 1.9 it fails to correctly set the encoding on the strings.
130
- #
131
- def normalise_string_encoding(str)
132
- if RUBY_VERSION >= "1.9"
133
- return str.dup.force_encoding("utf-8")
134
- else
135
- str
136
- end
137
- end
138
-
139
- # simple mapping of encoding constants to a string
140
- #
141
- def encoding_const_to_name(const)
142
- return nil if const.nil?
143
- case const
144
- when LibXML::XML::Encoding::UTF_8
145
- "utf-8"
146
- when LibXML::XML::Encoding::UTF_16LE
147
- "utf-16le"
148
- when LibXML::XML::Encoding::UTF_16BE
149
- "utf-16be"
150
- when LibXML::XML::Encoding::UCS_4LE
151
- "ucs-4le"
152
- when LibXML::XML::Encoding::UCS_4BE
153
- "ucs-4be"
154
- when LibXML::XML::Encoding::UCS_2
155
- "ucs-2"
156
- when LibXML::XML::Encoding::ISO_8859_1
157
- "iso-8859-1"
158
- when LibXML::XML::Encoding::ISO_8859_2
159
- "iso-8859-2"
160
- when LibXML::XML::Encoding::ISO_8859_3
161
- "iso-8859-3"
162
- when LibXML::XML::Encoding::ISO_8859_4
163
- "iso-8859-4"
164
- when LibXML::XML::Encoding::ISO_8859_5
165
- "iso-8859-5"
166
- when LibXML::XML::Encoding::ISO_8859_6
167
- "iso-8859-6"
168
- when LibXML::XML::Encoding::ISO_8859_7
169
- "iso-8859-7"
170
- when LibXML::XML::Encoding::ISO_8859_8
171
- "iso-8859-8"
172
- when LibXML::XML::Encoding::ISO_8859_9
173
- "iso-8859-9"
174
- when LibXML::XML::Encoding::ISO_2022_JP
175
- "iso-2022-jp"
176
- when LibXML::XML::Encoding::SHIFT_JIS
177
- "shift-jis"
178
- when LibXML::XML::Encoding::EUC_JP
179
- "euc-jp"
180
- when LibXML::XML::Encoding::ASCII
181
- "ascii"
182
- else
183
- nil
184
- end
185
- end
186
111
  end
187
112
  end
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class SalesRestriction
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "SalesRestriction"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class SenderIdentifier
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "SenderIdentifier"
9
8
 
data/lib/onix/series.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Series
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Series"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class SeriesIdentifier
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "SeriesIdentifier"
9
8
 
data/lib/onix/stock.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Stock
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Stock"
9
8
 
data/lib/onix/subject.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Subject
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Subject"
9
8
 
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class SupplyDetail
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "SupplyDetail"
9
8
 
data/lib/onix/title.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Title
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Title"
9
8
 
data/lib/onix/website.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module ONIX
4
4
  class Website
5
5
  include ROXML
6
- include ONIX::Common
7
6
 
8
7
  xml_name "Website"
9
8
 
@@ -1,8 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
4
-
5
- require 'onix'
3
+ require File.dirname(__FILE__) + '/spec_helper.rb'
6
4
  require 'date'
7
5
 
8
6
  context "ONIX::APAProduct" do
@@ -10,7 +8,7 @@ context "ONIX::APAProduct" do
10
8
  before(:each) do
11
9
  @data_path = File.join(File.dirname(__FILE__),"..","data")
12
10
  file1 = File.join(@data_path, "product.xml")
13
- @doc = LibXML::XML::Document.file(file1)
11
+ @doc = Nokogiri::XML::Document.parse(File.read(file1))
14
12
  @product_node = @doc.root
15
13
  end
16
14
 
@@ -57,3 +55,35 @@ context "ONIX::APAProduct" do
57
55
  end
58
56
 
59
57
  end
58
+
59
+ context ONIX::APAProduct, "price method" do
60
+ before(:each) do
61
+ @data_path = File.join(File.dirname(__FILE__),"..","data")
62
+ file1 = File.join(@data_path, "usd.xml")
63
+ @doc = Nokogiri::XML::Document.parse(File.read(file1))
64
+ @product_node = @doc.root
65
+ end
66
+
67
+ specify "should return the first price in the file, regardless of type" do
68
+ @product = ONIX::Product.from_xml(@product_node.to_s)
69
+ @apa = ONIX::APAProduct.new(@product)
70
+
71
+ @apa.price.should eql(BigDecimal.new("99.95"))
72
+ end
73
+ end
74
+
75
+ context ONIX::APAProduct, "rrp_exc_sales_tax method" do
76
+ before(:each) do
77
+ @data_path = File.join(File.dirname(__FILE__),"..","data")
78
+ file1 = File.join(@data_path, "usd.xml")
79
+ @doc = Nokogiri::XML::Document.parse(File.read(file1))
80
+ @product_node = @doc.root
81
+ end
82
+
83
+ specify "should return the first price in the file of type 1" do
84
+ @product = ONIX::Product.from_xml(@product_node.to_s)
85
+ @apa = ONIX::APAProduct.new(@product)
86
+
87
+ @apa.rrp_exc_sales_tax.should eql(BigDecimal.new("99.95"))
88
+ end
89
+ end