milkfarm-onix 0.8.9 → 0.8.10

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ v0.8.10 (25 May 2011)
2
+ - Add DiscountCoded to Price composite
3
+ - Add DiscountPercent to Price composite
4
+ - Add PriceEffectiveFrom, PriceEffectiveUntil to Price composite
5
+ - Create WorkIdentifiers module
6
+
1
7
  v0.8.9 (24 March 2011)
2
8
  - Add RelatedProduct composite (PR.23.7 - PR.23.33)
3
9
  - Create ProductIdentifiers module
@@ -52,14 +52,19 @@ module ONIX
52
52
  end
53
53
  end
54
54
 
55
+ # helper modules
56
+ require File.join(File.dirname(__FILE__), "onix", "list_writer")
57
+ require File.join(File.dirname(__FILE__), "onix", "inflector")
58
+ require File.join(File.dirname(__FILE__), "onix", "product_identifiers")
59
+ require File.join(File.dirname(__FILE__), "onix", "work_identifiers")
60
+ require File.join(File.dirname(__FILE__), "onix", "discount_codeds")
61
+
55
62
  # core files
56
63
  # - ordering is important, classes need to be defined before any
57
64
  # other class can use them
58
- require File.join(File.dirname(__FILE__), "onix", "list_writer")
59
65
  require File.join(File.dirname(__FILE__), "onix", "sender_identifier")
60
66
  require File.join(File.dirname(__FILE__), "onix", "addressee_identifier")
61
67
  require File.join(File.dirname(__FILE__), "onix", "header")
62
- require File.join(File.dirname(__FILE__), "onix", "product_identifiers")
63
68
  require File.join(File.dirname(__FILE__), "onix", "product_identifier")
64
69
  require File.join(File.dirname(__FILE__), "onix", "series_identifier")
65
70
  require File.join(File.dirname(__FILE__), "onix", "series")
@@ -77,6 +82,7 @@ require File.join(File.dirname(__FILE__), "onix", "other_text")
77
82
  require File.join(File.dirname(__FILE__), "onix", "media_file")
78
83
  require File.join(File.dirname(__FILE__), "onix", "sales_restriction")
79
84
  require File.join(File.dirname(__FILE__), "onix", "stock")
85
+ require File.join(File.dirname(__FILE__), "onix", "discount_coded")
80
86
  require File.join(File.dirname(__FILE__), "onix", "price")
81
87
  require File.join(File.dirname(__FILE__), "onix", "related_product")
82
88
  require File.join(File.dirname(__FILE__), "onix", "supply_detail")
@@ -0,0 +1,17 @@
1
+ # coding: utf-8
2
+
3
+ module ONIX
4
+ class DiscountCoded
5
+ include ROXML
6
+ extend ONIX::ListWriter
7
+ include ONIX::Inflector
8
+
9
+ xml_name "DiscountCoded"
10
+
11
+ xml_reader :discount_code_type, :from => "DiscountCodeType", :as => Fixnum, :to_xml => ONIX::Formatters.two_digit
12
+ xml_accessor :discount_code_type_name, :from => "DiscountCodeTypeName"
13
+ xml_accessor :discount_code, :from => "DiscountCode"
14
+ list_writer :discount_code_type, :list => 100
15
+
16
+ end
17
+ end
@@ -0,0 +1,73 @@
1
+ # coding: utf-8
2
+
3
+ module ONIX
4
+ module DiscountCodeds
5
+ CODE_TYPES = {:bic => 1, :proprietary => 2, :boeksoort => 3, :german => 4}
6
+ DEFAULT_ATTRIBUTE = :discount_code_type
7
+
8
+ def initialize_discount_codeds(options = {})
9
+ self.discount_codeds = options[:discount_codeds]
10
+ end
11
+
12
+ def discount_codeds
13
+ @discount_codeds ||= []
14
+ end
15
+
16
+ # find the DiscountCoded matching a particular type
17
+ def find_discount_coded(options = {})
18
+ raise NoMethodError, "Must call initialize_discount_codeds first" unless defined?(@discount_codeds)
19
+
20
+ # test find parameters
21
+ attribute, value = set_type_options(options).first
22
+ raise ArgumentError, "Find method passed unknown attribute '#{attribute}'" unless ONIX::DiscountCoded.new.attributes.include?(attribute.to_s)
23
+
24
+ @discount_codeds.find { |obj| obj.send(attribute) == value }
25
+ end
26
+
27
+ # set the value of a particular DiscountCoded (found by type)
28
+ # type can be either type code or key from CODE_TYPES
29
+ def set_discount_coded(type, value)
30
+ obj = find_discount_coded(type)
31
+
32
+ # create a new discount_coded object if necessary
33
+ if obj.nil?
34
+ obj = ONIX::DiscountCoded.new(set_type_options(type))
35
+ @discount_codeds << obj
36
+ end
37
+
38
+ obj.discount_code = value
39
+ end
40
+
41
+ def discount_codeds=(values)
42
+ values = [values] if values.nil? || values.is_a?(ONIX::DiscountCoded)
43
+ if values.is_a?(Array)
44
+ # Empty array
45
+ @discount_codeds = []
46
+ # Add any valid value
47
+ values.each do |value|
48
+ @discount_codeds << value if value.is_a?(ONIX::DiscountCoded)
49
+ end
50
+ else
51
+ raise ArgumentError, "Invalid DiscountCoded value: #{values.inspect}"
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def set_type_options(options = {})
58
+ if options.is_a?(Fixnum)
59
+ # assume value is DEFAULT_ATTRIBUTE
60
+ options = {DEFAULT_ATTRIBUTE => options}
61
+ elsif options.is_a?(String) || options.is_a?(Symbol)
62
+ # assume value is the key to CODE_TYPES
63
+ if key = CODE_TYPES[options.to_sym]
64
+ options = {DEFAULT_ATTRIBUTE => CODE_TYPES[options.to_sym]}
65
+ else
66
+ raise ArgumentError, "Unknown key '#{options}'"
67
+ end
68
+ end
69
+ options
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,24 @@
1
+ # utf-8
2
+
3
+ module ONIX
4
+ module Inflector
5
+
6
+ def initialize(options = {})
7
+ initialize_attributes(options)
8
+ end
9
+
10
+ def attributes
11
+ public_methods.map {|meth| meth.match(/^(?!roxml|taguri|=)(.+)=$/) ? $1 : nil }.compact
12
+ end
13
+
14
+ private
15
+
16
+ def initialize_attributes(options)
17
+ options.symbolize_keys!
18
+ attributes.each do |attribute|
19
+ self.send("#{attribute}=", options[attribute.to_sym]) if options[attribute.to_sym]
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -3,6 +3,7 @@
3
3
  module ONIX
4
4
  class Price
5
5
  include ROXML
6
+ include ONIX::DiscountCodeds
6
7
 
7
8
  xml_name "Price"
8
9
 
@@ -13,8 +14,16 @@ module ONIX
13
14
  xml_accessor :minimum_order_qty, :from => "MinimumOrderQuantity", :as => Fixnum
14
15
  xml_accessor :class_of_trade, :from => "ClassOfTrade"
15
16
  xml_accessor :bic_discount_group_code, :from => "BICDiscountGroupCode"
17
+ xml_reader :discount_codeds, :from => "DiscountCoded", :as => [ONIX::DiscountCoded]
18
+ xml_accessor :discount_percent, :from => "DiscountPercent", :as => BigDecimal, :to_xml => ONIX::Formatters.decimal
16
19
  xml_accessor :price_status, :from => "PriceStatus", :as => Fixnum, :to_xml => ONIX::Formatters.two_digit
17
20
  xml_accessor :price_amount, :from => "PriceAmount", :as => BigDecimal, :to_xml => ONIX::Formatters.decimal
18
21
  xml_accessor :currency_code, :from => "CurrencyCode"
22
+ xml_accessor :price_effective_from, :from => "PriceEffectiveFrom", :to_xml => ONIX::Formatters.yyyymmdd
23
+ xml_accessor :price_effective_until, :from => "PriceEffectiveUntil", :to_xml => ONIX::Formatters.yyyymmdd
24
+
25
+ def initialize(options = {})
26
+ initialize_discount_codeds(options) # @discount_codeds array
27
+ end
19
28
  end
20
29
  end
@@ -3,10 +3,14 @@
3
3
  module ONIX
4
4
  class ProductIdentifier
5
5
  include ROXML
6
+ extend ONIX::ListWriter
7
+ include ONIX::Inflector
6
8
 
7
9
  xml_name "ProductIdentifier"
8
10
 
9
- xml_accessor :product_id_type, :from => "ProductIDType", :as => Fixnum, :to_xml => ONIX::Formatters.two_digit
11
+ xml_reader :product_id_type, :from => "ProductIDType", :as => Fixnum, :to_xml => ONIX::Formatters.two_digit
10
12
  xml_accessor :id_value, :from => "IDValue"
13
+ list_writer :product_id_type, :list => 5
14
+
11
15
  end
12
16
  end
@@ -5,18 +5,17 @@ module ONIX
5
5
  ACCESSOR_METHODS = {:proprietary_id => 1, :ean => 3, :isbn10 => 2, :isbn13 => 15, :isbn => 15, :lccn => 13}
6
6
 
7
7
  def initialize_product_identifiers(options)
8
- @product_identifiers ||= []
9
- ACCESSOR_METHODS.keys.push(:product_identifiers).each do |name|
10
- self.send("#{name}=", options[name]) if options[name]
11
- end
8
+ self.product_identifiers = options[:product_identifiers]
9
+ # To initialize accessor methods, class in which this module is included
10
+ # must also include Inflector and call initialize_attributes
12
11
  end
13
12
 
14
13
  ACCESSOR_METHODS.each do |name, digit|
15
14
  define_method(name) do
16
- identifier(digit).andand.id_value
15
+ find(digit).andand.id_value
17
16
  end
18
17
  define_method("#{name}=") do |value|
19
- identifier_set(digit, value)
18
+ set(digit, value)
20
19
  end
21
20
  end
22
21
 
@@ -41,18 +40,26 @@ module ONIX
41
40
  end
42
41
 
43
42
  # find the ProductIdentifier matching a particular type
44
- def identifier(type)
45
- @product_identifiers.find { |obj| obj.product_id_type == type }
43
+ def find(options = {})
44
+ raise NoMethodError, "Must call initialize_product_identifiers first" unless defined?(@product_identifiers)
45
+
46
+ # use default find 'product_id_type' if implied
47
+ options = {:product_id_type => options} unless options.is_a?(Hash)
48
+
49
+ # test find parameters
50
+ attribute, value = options.first
51
+ raise ArgumentError, "Find method passed unknown attribute '#{attribute}'" unless ONIX::ProductIdentifier.new.attributes.include?(attribute.to_s)
52
+
53
+ @product_identifiers.find { |obj| obj.send(attribute) == value }
46
54
  end
47
55
 
48
56
  # set the value of a particular ProductIdentifier (found by type)
49
- def identifier_set(type, value)
50
- obj = identifier(type)
57
+ def set(type, value)
58
+ obj = find(type)
51
59
 
52
60
  # create a new product identifier object if we necessary
53
61
  if obj.nil?
54
- obj = ONIX::ProductIdentifier.new
55
- obj.product_id_type = type
62
+ obj = ONIX::ProductIdentifier.new(:product_id_type => type)
56
63
  @product_identifiers << obj
57
64
  end
58
65
 
@@ -5,6 +5,7 @@ module ONIX
5
5
  include ROXML
6
6
  include ONIX::ProductIdentifiers
7
7
  extend ONIX::ListWriter
8
+ include ONIX::Inflector
8
9
 
9
10
  xml_name "RelatedProduct"
10
11
 
@@ -13,9 +14,9 @@ module ONIX
13
14
  list_writer :relation_code, :list => 51
14
15
 
15
16
  def initialize(options = {})
16
- options.symbolize_keys!
17
- self.relation_code = options[:relation_code]
18
- self.initialize_product_identifiers(options) # Must be called to setup @product_identifiers array
17
+ # Must initialize arrays prior to attributes
18
+ initialize_product_identifiers(options) # @product_identifiers array
19
+ initialize_attributes(options)
19
20
  end
20
21
 
21
22
  end
@@ -1,3 +1,3 @@
1
1
  module ONIX
2
- VERSION = "0.8.9"
2
+ VERSION = "0.8.10"
3
3
  end
@@ -4,6 +4,7 @@ module ONIX
4
4
  class WorkIdentifier
5
5
  include ROXML
6
6
  extend ONIX::ListWriter
7
+ include ONIX::Inflector
7
8
 
8
9
  xml_name "WorkIdentifier"
9
10
 
@@ -12,12 +13,5 @@ module ONIX
12
13
  xml_accessor :id_value, :from => "IDValue"
13
14
  list_writer :work_id_type, :list => 16
14
15
 
15
- def initialize(options = {})
16
- options.symbolize_keys!
17
- self.work_id_type = options[:work_id_type]
18
- @id_type_name = options[:id_type_name]
19
- @id_value = options[:id_value]
20
- end
21
-
22
16
  end
23
17
  end
@@ -0,0 +1,74 @@
1
+ # coding: utf-8
2
+
3
+ module ONIX
4
+ module WorkIdentifiers
5
+ ID_TYPES = {:proprietary_id => 1, :isbn10 => 2, :isbn13 => 15, :isbn => 15}
6
+
7
+ def initialize_work_identifiers(options = {})
8
+ self.work_identifiers = options[:work_identifiers]
9
+ # To initialize accessor methods, class in which this module is included
10
+ # must also include Inflector and call initialize_attributes
11
+ end
12
+
13
+ def work_identifiers
14
+ @work_identifiers ||= []
15
+ end
16
+
17
+ # find the WorkIdentifier matching a particular type
18
+ def find_work_identifier(options = {})
19
+ raise NoMethodError, "Must call initialize_work_identifiers first" unless defined?(@work_identifiers)
20
+
21
+ # test find parameters
22
+ attribute, value = set_type_options(options).first
23
+ raise ArgumentError, "Find method passed unknown attribute '#{attribute}'" unless ONIX::WorkIdentifier.new.attributes.include?(attribute.to_s)
24
+
25
+ @work_identifiers.find { |obj| obj.send(attribute) == value }
26
+ end
27
+
28
+ # set the value of a particular WorkIdentifier (found by type)
29
+ # type can be either type code or key from ID_TYPES
30
+ def set_work_identifier(type, value)
31
+ obj = find_work_identifier(type)
32
+
33
+ # create a new work identifier object if necessary
34
+ if obj.nil?
35
+ obj = ONIX::WorkIdentifier.new(set_type_options(type))
36
+ @work_identifiers << obj
37
+ end
38
+
39
+ obj.id_value = value
40
+ end
41
+
42
+ def work_identifiers=(values)
43
+ values = [values] if values.nil? || values.is_a?(ONIX::WorkIdentifier)
44
+ if values.is_a?(Array)
45
+ # Empty array
46
+ @work_identifiers = []
47
+ # Add any valid value
48
+ values.each do |value|
49
+ @work_identifiers << value if value.is_a?(ONIX::WorkIdentifier)
50
+ end
51
+ else
52
+ raise ArgumentError, "Invalid WorkIdentifier value: #{values.inspect}"
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def set_type_options(options = {})
59
+ if options.is_a?(Fixnum)
60
+ # assume value is 'work_id_type'
61
+ options = {:work_id_type => options}
62
+ elsif options.is_a?(String) || options.is_a?(Symbol)
63
+ # assume value is the key to ID_TYPES
64
+ if key = ID_TYPES[options.to_sym]
65
+ options = {:work_id_type => ID_TYPES[options.to_sym]}
66
+ else
67
+ raise ArgumentError, "Unknown key '#{options}'"
68
+ end
69
+ end
70
+ options
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper.rb'
4
+
5
+ describe ONIX::DiscountCoded do
6
+
7
+ before(:each) do
8
+ data_path = File.join(File.dirname(__FILE__), "..", "data")
9
+ file1 = File.join(data_path, "discount_coded.xml")
10
+ @doc = Nokogiri::XML::Document.parse(File.read(file1))
11
+ @root = @doc.root
12
+ end
13
+
14
+ it "should correctly convert to a string" do
15
+ t = ONIX::DiscountCoded.from_xml(@root.to_s)
16
+ t.to_xml.to_s[0,15].should eql("<DiscountCoded>")
17
+ end
18
+
19
+ it "should provide read access to first level attributes" do
20
+ t = ONIX::DiscountCoded.from_xml(@root.to_s)
21
+ t.discount_code_type.should eql(2)
22
+ t.discount_code_type_name.should eql("Proprietary Scheme")
23
+ t.discount_code.should eql("ABC123")
24
+ end
25
+
26
+ it "should provide write access to first level attributes" do
27
+ t = ONIX::DiscountCoded.new
28
+
29
+ t.discount_code_type = 1
30
+ t.to_xml.to_s.include?("<DiscountCodeType>01</DiscountCodeType>").should be_true
31
+
32
+ t.discount_code_type_name = "BIC Scheme"
33
+ t.to_xml.to_s.include?("<DiscountCodeTypeName>BIC Scheme</DiscountCodeTypeName>").should be_true
34
+
35
+ t.discount_code = "ABC123"
36
+ t.to_xml.to_s.include?("<DiscountCode>ABC123</DiscountCode>").should be_true
37
+ end
38
+
39
+ it "should raise error writing discount_code_type value not in list" do
40
+ t = ONIX::DiscountCoded.new
41
+ lambda {t.discount_code_type = 999}.should raise_error
42
+ lambda {ONIX::DiscountCoded.new(:discount_code_type => 999)}.should raise_error
43
+ end
44
+
45
+ it "should properly initialize attributes when calling new" do
46
+ t = ONIX::DiscountCoded.new(:discount_code_type => 1, :discount_code => "value", :discount_code_type_name => "name")
47
+ t.discount_code_type.should eql(1)
48
+ t.discount_code.should eql("value")
49
+ t.discount_code_type_name.should eql("name")
50
+ end
51
+
52
+ end
@@ -0,0 +1,96 @@
1
+ # coding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper.rb'
4
+
5
+ describe ONIX::DiscountCodeds do
6
+
7
+ module ONIX
8
+ class FakeDiscountEntity
9
+ include ROXML
10
+ include ONIX::DiscountCodeds
11
+
12
+ xml_name "FakeDiscountEntity"
13
+ xml_reader :discount_codeds, :from => "DiscountCoded", :as => [ONIX::DiscountCoded]
14
+
15
+ def initialize(options = {})
16
+ initialize_discount_codeds(options) # @discount_codeds array
17
+ end
18
+ end
19
+ end
20
+
21
+ before :each do
22
+ @fake = ONIX::FakeDiscountEntity.new(:proprietary => 123456)
23
+ data_path = File.join(File.dirname(__FILE__),"..","data")
24
+ file = File.join(data_path, "fake_entity.xml")
25
+ @doc = Nokogiri::XML::Document.parse(File.read(file))
26
+ @root = @doc.root
27
+ end
28
+
29
+ it "should instantiate discount_codeds array" do
30
+ @fake.discount_codeds.should be_a(Array)
31
+ end
32
+
33
+ it "should provide read access to discount_codeds" do
34
+ fe = ONIX::FakeDiscountEntity.from_xml(@root.to_s)
35
+ wid = fe.find_discount_coded(2)
36
+ wid.discount_code.should eql("PROPRIETARY_CODE")
37
+ wid = fe.find_discount_coded(:proprietary)
38
+ wid.discount_code.should eql("PROPRIETARY_CODE")
39
+ wid = fe.find_discount_coded(:bic)
40
+ wid.discount_code.should eql("BIC_CODE")
41
+ end
42
+
43
+ it "should provide write access to discount_codeds by discount_code_type" do
44
+ discount_code = "123456"
45
+ {:bic => 1, :proprietary => 2, :boeksoort => 3, :german => 4}.each do |key, discount_code_type|
46
+ fe = ONIX::FakeDiscountEntity.new
47
+ fe.set_discount_coded(discount_code_type, discount_code)
48
+ fe.to_xml.to_s.include?("<DiscountCodeType>#{sprintf('%02d', discount_code_type)}</DiscountCodeType>").should be_true
49
+ fe.to_xml.to_s.include?("<DiscountCode>#{discount_code}</DiscountCode>").should be_true
50
+ end
51
+ end
52
+
53
+ it "should provide write access to discount_codeds by key" do
54
+ discount_code = "123456"
55
+ {:bic => 1, :proprietary => 2, :boeksoort => 3, :german => 4}.each do |key, discount_code_type|
56
+ fe = ONIX::FakeDiscountEntity.new
57
+ fe.set_discount_coded(key, discount_code)
58
+ fe.to_xml.to_s.include?("<DiscountCodeType>#{sprintf('%02d', discount_code_type)}</DiscountCodeType>").should be_true
59
+ fe.to_xml.to_s.include?("<DiscountCode>#{discount_code}</DiscountCode>").should be_true
60
+ end
61
+ end
62
+
63
+ it "should fail to write discount_coded with invalid discount_code_type" do
64
+ discount_code = "123456"
65
+ discount_code_type = 99
66
+ fe = ONIX::FakeDiscountEntity.new
67
+ lambda {fe.set_discount_coded(discount_code_type, discount_code)}.should raise_error(ArgumentError)
68
+ end
69
+
70
+ it "should fail to write discount_coded with invalid key" do
71
+ discount_code = "123456"
72
+ key = "bad_key"
73
+ fe = ONIX::FakeDiscountEntity.new
74
+ lambda {fe.set_discount_coded(key, discount_code)}.should raise_error(ArgumentError)
75
+ end
76
+
77
+ it "should provide write access to discount_codeds array" do
78
+ id = ONIX::DiscountCoded.new
79
+ id.discount_code_type = 1
80
+ id.discount_code = "123456"
81
+ fe = ONIX::FakeDiscountEntity.new(:discount_codeds => id)
82
+
83
+ fe.to_xml.to_s.include?("<DiscountCoded>").should be_true
84
+ fe.to_xml.to_s.include?("<DiscountCodeType>01</DiscountCodeType>").should be_true
85
+ fe.to_xml.to_s.include?("<DiscountCode>123456</DiscountCode>").should be_true
86
+
87
+ id = ONIX::DiscountCoded.new
88
+ id.discount_code_type = 2
89
+ id.discount_code = "987654"
90
+ fe.discount_codeds << id
91
+
92
+ fe.to_xml.to_s.include?("<DiscountCodeType>02</DiscountCodeType>").should be_true
93
+ fe.to_xml.to_s.include?("<DiscountCode>987654</DiscountCode>").should be_true
94
+ end
95
+
96
+ end
@@ -5,11 +5,11 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
5
5
  describe ONIX::ListWriter do
6
6
 
7
7
  module ONIX
8
- class FakeEntity
8
+ class FakeListEntity
9
9
  include ROXML
10
10
  extend ONIX::ListWriter
11
11
 
12
- xml_name "FakeEntity"
12
+ xml_name "FakeListEntity"
13
13
  xml_reader :series_id_type, :from => "SeriesIDType", :as => Fixnum, :to_xml => ONIX::Formatters.two_digit
14
14
  list_writer :series_id_type, :list => 13
15
15
 
@@ -21,26 +21,26 @@ describe ONIX::ListWriter do
21
21
 
22
22
  before :each do
23
23
  data_path = File.join(File.dirname(__FILE__),"..","data")
24
- file1 = File.join(data_path, "fake_entity_for_lists.xml")
24
+ file1 = File.join(data_path, "fake_entity.xml")
25
25
  @doc = Nokogiri::XML::Document.parse(File.read(file1))
26
26
  @root = @doc.root
27
27
  end
28
28
 
29
29
  it "should provide read access to first level attribute" do
30
- fake = ONIX::FakeEntity.from_xml(@root.to_s)
30
+ fake = ONIX::FakeListEntity.from_xml(@root.to_s)
31
31
  fake.series_id_type.should eql(1)
32
32
  end
33
33
 
34
34
  it "should provide write access to first level attribute" do
35
- fake = ONIX::FakeEntity.new(:series_id_type => 3)
35
+ fake = ONIX::FakeListEntity.new(:series_id_type => 3)
36
36
  fake.series_id_type.should eql(3)
37
37
  fake.to_xml.to_s.include?("<SeriesIDType>03</SeriesIDType>").should be_true
38
38
  end
39
39
 
40
40
  it "should raise error writing value not in list" do
41
- fake = ONIX::FakeEntity.new
41
+ fake = ONIX::FakeListEntity.new
42
42
  lambda {fake.series_id_type = 100}.should raise_error
43
- lambda {ONIX::FakeEntity.new(:series_id_type => 100)}.should raise_error
43
+ lambda {ONIX::FakeListEntity.new(:series_id_type => 100)}.should raise_error
44
44
  end
45
45
 
46
46
  end
@@ -6,9 +6,9 @@ describe ONIX::Price do
6
6
 
7
7
  before(:each) do
8
8
  data_path = File.join(File.dirname(__FILE__),"..","data")
9
- file1 = File.join(data_path, "price.xml")
10
- @doc = Nokogiri::XML::Document.parse(File.read(file1))
11
- @root = @doc.root
9
+ file1 = File.join(data_path, "price.xml")
10
+ @doc = Nokogiri::XML::Document.parse(File.read(file1))
11
+ @root = @doc.root
12
12
  end
13
13
 
14
14
  it "should correctly convert to a string" do
@@ -20,7 +20,27 @@ describe ONIX::Price do
20
20
  p = ONIX::Price.from_xml(@root.to_s)
21
21
 
22
22
  p.price_type_code.should eql(2)
23
+ p.discount_percent.should eql(BigDecimal.new("45"))
23
24
  p.price_amount.should eql(BigDecimal.new("7.5"))
25
+ p.price_effective_from.should eql("19990101")
26
+ p.price_effective_until.should eql("20001231")
27
+ end
28
+
29
+ it "should provide read access to discount_codeds" do
30
+ p = ONIX::Price.from_xml(@root.to_s)
31
+
32
+ d = p.find_discount_coded(1)
33
+ d.discount_code_type_name.should eql("britishco")
34
+ d.discount_code.should eql("999ZZZ")
35
+
36
+ d = p.find_discount_coded(:discount_code_type_name => 'bakertaylor')
37
+ d.discount_code_type.should eql(2)
38
+ d.discount_code_type_name.should eql("bakertaylor")
39
+ d.discount_code.should eql("ABC123")
40
+
41
+ d = p.find_discount_coded(:proprietary) # will retrieve first proprietary discount code
42
+ d.discount_code_type_name.should eql("ingram")
43
+ d.discount_code.should eql("XYZ987")
24
44
  end
25
45
 
26
46
  it "should provide write access to first level attributes" do
@@ -29,9 +49,36 @@ describe ONIX::Price do
29
49
  p.price_type_code = 1
30
50
  p.to_xml.to_s.include?("<PriceTypeCode>01</PriceTypeCode>").should be_true
31
51
 
52
+ p.discount_percent = BigDecimal.new("50")
53
+ p.to_xml.to_s.include?("<DiscountPercent>50.0</DiscountPercent>").should be_true
54
+
32
55
  p.price_amount = BigDecimal.new("7.5")
33
56
  p.to_xml.to_s.include?("<PriceAmount>7.5</PriceAmount>").should be_true
34
57
 
58
+ p.price_effective_from = Date.civil(1999,1,1)
59
+ p.to_xml.to_s.include?("<PriceEffectiveFrom>19990101</PriceEffectiveFrom>").should be_true
60
+
61
+ p.price_effective_until = Date.civil(2000,12,31)
62
+ p.to_xml.to_s.include?("<PriceEffectiveUntil>20001231</PriceEffectiveUntil>").should be_true
63
+ end
64
+
65
+ it "should provide write access to discount_codeds" do
66
+ d1 = ONIX::DiscountCoded.new(:discount_code_type => 1, :discount_code => "value1", :discount_code_type_name => "name1")
67
+ d2 = ONIX::DiscountCoded.new(:discount_code_type => 2, :discount_code => "value2", :discount_code_type_name => "name2")
68
+ d3 = ONIX::DiscountCoded.new(:discount_code_type => 3, :discount_code => "value3", :discount_code_type_name => "name3")
69
+ # p = ONIX::Price.new(:discount_codeds => [d1,d2])
70
+ p = ONIX::Price.new
71
+ p.discount_codeds = [d1, d2]
72
+ p.discount_codeds << d3
73
+ p.discount_codeds.size.should eql(3)
74
+ (1..p.discount_codeds.size).to_a.each do |i|
75
+ name = "name#{i}"
76
+ value = "value#{i}"
77
+ t = p.find_discount_coded(:discount_code_type_name => name)
78
+ t.discount_code_type.should eql(i)
79
+ t.discount_code.should eql(value)
80
+ t.discount_code_type_name.should eql(name)
81
+ end
35
82
  end
36
83
 
37
84
  end
@@ -24,14 +24,20 @@ describe ONIX::ProductIdentifier do
24
24
  end
25
25
 
26
26
  it "should provide write access to first level attributes" do
27
- id = ONIX::ProductIdentifier.new
27
+ id = ONIX::ProductIdentifier.new(:product_id_type => 1, :id_value => "TESTING")
28
+ id.to_xml.to_s.include?("<ProductIDType>01</ProductIDType>").should be_true
29
+ id.to_xml.to_s.include?("<IDValue>TESTING</IDValue>").should be_true
28
30
 
29
31
  id.product_id_type = 2
30
32
  id.to_xml.to_s.include?("<ProductIDType>02</ProductIDType>").should be_true
31
33
 
32
34
  id.id_value = "James"
33
35
  id.to_xml.to_s.include?("<IDValue>James</IDValue>").should be_true
34
-
36
+ end
37
+
38
+ it "should raise error when writing invalid product_id_type" do
39
+ id = ONIX::ProductIdentifier.new
40
+ lambda {id.product_id_type = 999}.should raise_error
35
41
  end
36
42
 
37
43
  end
@@ -8,12 +8,15 @@ describe ONIX::ProductIdentifiers do
8
8
  class FakeEntity
9
9
  include ROXML
10
10
  include ONIX::ProductIdentifiers
11
+ include ONIX::Inflector
11
12
 
12
13
  xml_name "FakeEntity"
13
14
  xml_reader :product_identifiers, :from => "ProductIdentifier", :as => [ONIX::ProductIdentifier]
14
15
 
15
16
  def initialize(options = {})
16
- self.initialize_product_identifiers(options)
17
+ # Must initialize arrays prior to attributes
18
+ initialize_product_identifiers(options) # @product_identifiers array
19
+ initialize_attributes(options)
17
20
  end
18
21
  end
19
22
  end
@@ -41,5 +41,12 @@ describe ONIX::WorkIdentifier do
41
41
  lambda {t.work_id_type = 999}.should raise_error
42
42
  lambda {ONIX::WorkIdentifier.new(:work_id_type => 999)}.should raise_error
43
43
  end
44
+
45
+ it "should properly initialize attributes when calling new" do
46
+ t = ONIX::WorkIdentifier.new(:work_id_type => 1, :id_value => "value", :id_type_name => "name")
47
+ t.work_id_type.should eql(1)
48
+ t.id_value.should eql("value")
49
+ t.id_type_name.should eql("name")
50
+ end
44
51
 
45
52
  end
@@ -0,0 +1,100 @@
1
+ # coding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper.rb'
4
+
5
+ describe ONIX::WorkIdentifiers do
6
+
7
+ module ONIX
8
+ class FakeWorkEntity
9
+ include ROXML
10
+ include ONIX::WorkIdentifiers
11
+
12
+ xml_name "FakeWorkEntity"
13
+ xml_reader :work_identifiers, :from => "WorkIdentifier", :as => [ONIX::WorkIdentifier]
14
+
15
+ def initialize(options = {})
16
+ initialize_work_identifiers(options) # @work_identifiers array
17
+ end
18
+ end
19
+ end
20
+
21
+ before :each do
22
+ @fake = ONIX::FakeWorkEntity.new(:isbn => 123456)
23
+ data_path = File.join(File.dirname(__FILE__),"..","data")
24
+ file = File.join(data_path, "fake_entity.xml")
25
+ @doc = Nokogiri::XML::Document.parse(File.read(file))
26
+ @root = @doc.root
27
+ end
28
+
29
+ it "should instantiate work identifiers array" do
30
+ @fake.work_identifiers.should be_a(Array)
31
+ end
32
+
33
+ it "should provide read access to work identifiers" do
34
+ fe = ONIX::FakeWorkEntity.from_xml(@root.to_s)
35
+ wid = fe.find_work_identifier(1)
36
+ wid.id_value.should eql("WD_PROPRIETARY_ID")
37
+ wid = fe.find_work_identifier(:proprietary_id)
38
+ wid.id_value.should eql("WD_PROPRIETARY_ID")
39
+ wid = fe.find_work_identifier(:isbn10)
40
+ wid.id_value.should eql("WD_ISBN10")
41
+ wid = fe.find_work_identifier(:isbn13)
42
+ wid.id_value.should eql("WD_ISBN13")
43
+ wid = fe.find_work_identifier(:isbn)
44
+ wid.id_value.should eql("WD_ISBN13")
45
+ end
46
+
47
+ it "should provide write access to work identifiers by work_id_type" do
48
+ id_value = "123456"
49
+ {:proprietary_id => 1, :isbn10 => 2, :isbn13 => 15, :isbn => 15}.each do |key, work_id_type|
50
+ fe = ONIX::FakeWorkEntity.new
51
+ fe.set_work_identifier(work_id_type, id_value)
52
+ fe.to_xml.to_s.include?("<WorkIDType>#{sprintf('%02d', work_id_type)}</WorkIDType>").should be_true
53
+ fe.to_xml.to_s.include?("<IDValue>#{id_value}</IDValue>").should be_true
54
+ end
55
+ end
56
+
57
+ it "should provide write access to work identifiers by key" do
58
+ id_value = "123456"
59
+ {:proprietary_id => 1, :isbn10 => 2, :isbn13 => 15, :isbn => 15}.each do |key, work_id_type|
60
+ fe = ONIX::FakeWorkEntity.new
61
+ fe.set_work_identifier(key, id_value)
62
+ fe.to_xml.to_s.include?("<WorkIDType>#{sprintf('%02d', work_id_type)}</WorkIDType>").should be_true
63
+ fe.to_xml.to_s.include?("<IDValue>#{id_value}</IDValue>").should be_true
64
+ end
65
+ end
66
+
67
+ it "should fail to write work identifier with invalid work_id_type" do
68
+ id_value = "123456"
69
+ work_id_type = 99
70
+ fe = ONIX::FakeWorkEntity.new
71
+ lambda {fe.set_work_identifier(work_id_type, id_value)}.should raise_error(ArgumentError)
72
+ end
73
+
74
+ it "should fail to write work identifier with invalid key" do
75
+ id_value = "123456"
76
+ key = "bad_key"
77
+ fe = ONIX::FakeWorkEntity.new
78
+ lambda {fe.set_work_identifier(key, id_value)}.should raise_error(ArgumentError)
79
+ end
80
+
81
+ it "should provide write access to work_identifiers array" do
82
+ id = ONIX::WorkIdentifier.new
83
+ id.work_id_type = 1
84
+ id.id_value = "123456"
85
+ fe = ONIX::FakeWorkEntity.new(:work_identifiers => id)
86
+
87
+ fe.to_xml.to_s.include?("<WorkIdentifier>").should be_true
88
+ fe.to_xml.to_s.include?("<WorkIDType>01</WorkIDType>").should be_true
89
+ fe.to_xml.to_s.include?("<IDValue>123456</IDValue>").should be_true
90
+
91
+ id = ONIX::WorkIdentifier.new
92
+ id.work_id_type = 2
93
+ id.id_value = "987654"
94
+ fe.work_identifiers << id
95
+
96
+ fe.to_xml.to_s.include?("<WorkIDType>02</WorkIDType>").should be_true
97
+ fe.to_xml.to_s.include?("<IDValue>987654</IDValue>").should be_true
98
+ end
99
+
100
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: milkfarm-onix
3
3
  version: !ruby/object:Gem::Version
4
- hash: 45
4
+ hash: 43
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 9
10
- version: 0.8.9
9
+ - 10
10
+ version: 0.8.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - James Healy
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-03-24 00:00:00 -07:00
19
+ date: 2011-05-25 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -123,8 +123,11 @@ files:
123
123
  - lib/onix/audience_range.rb
124
124
  - lib/onix/code_list_extractor.rb
125
125
  - lib/onix/contributor.rb
126
+ - lib/onix/discount_coded.rb
127
+ - lib/onix/discount_codeds.rb
126
128
  - lib/onix/header.rb
127
129
  - lib/onix/imprint.rb
130
+ - lib/onix/inflector.rb
128
131
  - lib/onix/language.rb
129
132
  - lib/onix/list_writer.rb
130
133
  - lib/onix/lists.rb
@@ -154,6 +157,7 @@ files:
154
157
  - lib/onix/version.rb
155
158
  - lib/onix/website.rb
156
159
  - lib/onix/work_identifier.rb
160
+ - lib/onix/work_identifiers.rb
157
161
  - lib/onix/writer.rb
158
162
  - lib/onix.rb
159
163
  - support/codes/001.tsv
@@ -335,6 +339,8 @@ files:
335
339
  - spec/apa_product_spec.rb
336
340
  - spec/audience_range_spec.rb
337
341
  - spec/contributor_spec.rb
342
+ - spec/discount_coded_spec.rb
343
+ - spec/discount_codeds_spec.rb
338
344
  - spec/header_spec.rb
339
345
  - spec/imprint_spec.rb
340
346
  - spec/language_spec.rb
@@ -365,6 +371,7 @@ files:
365
371
  - spec/title_spec.rb
366
372
  - spec/website_spec.rb
367
373
  - spec/work_identifier_spec.rb
374
+ - spec/work_identifiers_spec.rb
368
375
  - spec/writer_spec.rb
369
376
  has_rdoc: true
370
377
  homepage: http://github.com/milkfarm/onix
@@ -406,6 +413,8 @@ test_files:
406
413
  - spec/apa_product_spec.rb
407
414
  - spec/audience_range_spec.rb
408
415
  - spec/contributor_spec.rb
416
+ - spec/discount_coded_spec.rb
417
+ - spec/discount_codeds_spec.rb
409
418
  - spec/header_spec.rb
410
419
  - spec/imprint_spec.rb
411
420
  - spec/language_spec.rb
@@ -436,4 +445,5 @@ test_files:
436
445
  - spec/title_spec.rb
437
446
  - spec/website_spec.rb
438
447
  - spec/work_identifier_spec.rb
448
+ - spec/work_identifiers_spec.rb
439
449
  - spec/writer_spec.rb