im_onix 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 956b2f80b2309dd983629d3e3ec2e14f854c8514ae5fe0cf2030c6929c6db371
4
- data.tar.gz: 60b6cfbd37b72e1ee40db946dfff209b18479c1a360fbb7123c4e91da87734f7
3
+ metadata.gz: 1ae373f892a08b5321d26712145417619ca310f6a5d6f7bc67ad42049d65a7c9
4
+ data.tar.gz: c6b81828cef14da0196bd9e27e5a92fb6e95718e3e7261b8407f346f9e7dfe64
5
5
  SHA512:
6
- metadata.gz: 898d6346b0e55f4c1b7521e7f3a91d3c03565503833b3acd481fa964ce31b18d8c44df63cbf6eb9848bce854390464f4c2979fff55421d7f48ad9078517fbc56
7
- data.tar.gz: 55b9ace6c959c0d6397d42c1ae01d4bef0c8aba84d073258a7fe8855b1c08ef3697cc1a65a53c307de3895ca319f2ca0a6cfb840a33322f318aae646da768e32
6
+ metadata.gz: 3595015fab4b4c17898aedbb507189c81ba830cb0c76914b5b280965a574beffcf7bc31f2376c1667568ffadf4e7213b996e91cb89755f2bcee9296bad1a5cd5
7
+ data.tar.gz: 388f43af3200a38a9d3c20fb1e08e3157a2119a2b6faf91af9505c5b25b36a938c45d3faa6be2ebea90b31dc54bd84f498fa24875d258a067e043b0d0e87e525
data/Gemfile CHANGED
@@ -1,8 +1,3 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem 'nokogiri'
4
-
5
- group :development do
6
- gem "shoulda", ">= 0"
7
- gem "bundler"
8
- end
3
+ gemspec
data/README.md CHANGED
@@ -30,7 +30,7 @@ onix3_to_onix3.rb onix.xml
30
30
  Launch this command:
31
31
 
32
32
  ```
33
- ruby -Ilib:test test/test_im_onix.rb
33
+ bundle exec rake test
34
34
  ```
35
35
 
36
36
  ### Contributors
data/bin/onix_bench.rb CHANGED
@@ -8,10 +8,26 @@ filename = ARGV[0]
8
8
  if filename
9
9
  GC.start
10
10
  old_memsize = ObjectSpace.count_objects_size[:TOTAL]
11
+
12
+ if ENV['PROFILE'] == 'yes'
13
+ require 'ruby-prof'
14
+ RubyProf.start
15
+ end
16
+
11
17
  msg = ONIX::ONIXMessage.new
12
18
  bench = Benchmark.measure do
13
19
  msg.parse(filename)
14
20
  end
21
+
22
+ if ENV['PROFILE'] == 'yes'
23
+ result = RubyProf.stop
24
+ printer = RubyProf::FlatPrinter.new(result)
25
+ printer.print(STDOUT, min_percent: 1.0)
26
+
27
+ printer = RubyProf::CallTreePrinter.new(result)
28
+ printer.print
29
+ end
30
+
15
31
  GC.start
16
32
  new_memsize = ObjectSpace.count_objects_size[:TOTAL]
17
33
  puts "Parsed #{msg.products.length} products in #{bench.real.round(3)} seconds resulting in use of #{new_memsize - old_memsize} bytes of memory"
data/im_onix.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "im_onix"
7
- spec.version = "1.2.3"
7
+ spec.version = "1.3.0"
8
8
  spec.authors = ["Julien Boulnois"]
9
9
  spec.email = ["jboulnois@immateriel.fr"]
10
10
 
@@ -25,6 +25,8 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "minitest"
26
26
  spec.add_development_dependency 'yard'
27
27
  spec.add_development_dependency 'shoulda'
28
+ spec.add_development_dependency "simplecov"
29
+ spec.add_development_dependency "ruby-prof"
28
30
 
29
31
  spec.required_ruby_version = '>= 2.1'
30
32
 
@@ -2,7 +2,8 @@ require 'onix/identifier'
2
2
 
3
3
  module ONIX
4
4
  class Addressee < SubsetDSL
5
- include GlnMethods
5
+ include IdentifiersMethods::Gln
6
+
6
7
  elements "AddresseeIdentifier", :subset, :shortcut => :identifiers, :cardinality => 0..n
7
8
  element "AddresseeName", :text, :shortcut => :name, :cardinality => 0..1
8
9
  element "ContactName", :text, :cardinality => 0..1
@@ -0,0 +1,6 @@
1
+ module ONIX
2
+ class Barcode < SubsetDSL
3
+ element "BarcodeType", :subset, :cardinality => 1
4
+ element "PositionOnProduct", :subset, :cardinality => 0..1
5
+ end
6
+ end
data/lib/onix/builder.rb CHANGED
@@ -38,6 +38,8 @@ module ONIX
38
38
  yield self
39
39
  end
40
40
 
41
+ @lax = false
42
+
41
43
  @parent = @doc
42
44
  end
43
45
 
@@ -55,13 +57,27 @@ module ONIX
55
57
  end.to_xml
56
58
  end
57
59
 
60
+ # code in block will not check for validity
61
+ def lax &block
62
+ if block_given?
63
+ @lax = true
64
+ @arity ||= block.arity
65
+ if @arity <= 0
66
+ instance_eval(&block)
67
+ else
68
+ block.call(self)
69
+ end
70
+ @lax = false
71
+ end
72
+ end
73
+
58
74
  def method_missing(method, *args, &block)
59
75
  # :nodoc:
60
76
  if @context && @context.respond_to?(method)
61
77
  @context.send(method, *args, &block)
62
78
  else
63
79
  if @parent
64
- parser_el = @parent.class.ancestors_registered_elements[method.to_s]
80
+ parser_el = @parent.registered_elements[method.to_s]
65
81
  if parser_el
66
82
  if ONIX.const_defined?(parser_el.klass_name)
67
83
  node = get_class(parser_el.klass_name, args)
@@ -82,7 +98,7 @@ module ONIX
82
98
  when :ignore
83
99
  when :subset
84
100
  # FIXME: dirty
85
- if @parent.class.included_modules.include?(DateHelper) && node.is_a?(DateFormat)
101
+ if @parent.class.included_modules.include?(DateMethods) && node.is_a?(DateFormat)
86
102
  @parent.deprecated_date_format = true
87
103
  end
88
104
  node.attributes = get_attributes(args[1]) if args.length > 1
@@ -129,7 +145,9 @@ module ONIX
129
145
  if arg.is_a?(String)
130
146
  el = klass.from_code(arg)
131
147
  unless el.human
132
- raise BuilderInvalidCode, [klass.to_s, arg]
148
+ unless @lax
149
+ raise BuilderInvalidCode, [klass.to_s, arg]
150
+ end
133
151
  end
134
152
  el
135
153
  else
@@ -154,14 +172,12 @@ module ONIX
154
172
  end
155
173
 
156
174
  def get_class(nm, args)
157
- el = nil
158
175
  if ONIX.const_defined?(nm)
159
176
  klass = ONIX.const_get(nm)
160
- el = get_element(klass, args)
177
+ get_element(klass, args)
161
178
  else
162
179
  raise BuilderUndefinedElement, nm
163
180
  end
164
- el
165
181
  end
166
182
 
167
183
  def insert(node, &block)
data/lib/onix/code.rb CHANGED
@@ -1,11 +1,10 @@
1
- # -*- encoding : utf-8 -*-
2
1
  require 'yaml'
3
2
 
4
3
  module ONIX
5
4
  class InvalidCodeAlias < StandardError
6
5
  end
7
6
 
8
- module CodeHelper
7
+ module CodeMethods
9
8
  def parse(n)
10
9
  parse_attributes(n.attributes)
11
10
  @code = n.text
@@ -33,58 +32,56 @@ module ONIX
33
32
  # @return [String] humanized string (eg: "Digital watermarking" become DigitalWatermarking, "PDF" become Pdf, "BISAC Subject Heading" become BisacSubjectHeading, etc)
34
33
  attr_accessor :human
35
34
 
36
- include CodeHelper
37
-
38
- # create Code from ONIX code
39
- # @param [String] code ONIX code
40
- # @return [Code]
41
- def self.from_code(code)
42
- obj = self.new
43
- obj.code = code
44
- obj.human = self.hash[code]
45
- obj
46
- end
47
-
48
- # create Code from human readable code
49
- # @param [String] human human readable code
50
- # @return [Code]
51
- def self.from_human(human)
52
- obj = self.new
53
- obj.human = human
54
- obj.code = self.hash.key(human)
55
- unless obj.code
56
- raise InvalidCodeAlias, [self.to_s, human]
35
+ include CodeMethods
36
+
37
+ class << self
38
+ # create Code from ONIX code
39
+ # @param [String] code ONIX code
40
+ # @return [Code]
41
+ def from_code(code)
42
+ obj = self.new
43
+ obj.code = code
44
+ obj.human = self.hash[code]
45
+ obj
57
46
  end
58
- obj
59
- end
60
47
 
61
- private
48
+ # create Code from human readable code
49
+ # @param [String] human human readable code
50
+ # @return [Code]
51
+ def from_human(human)
52
+ obj = self.new
53
+ obj.human = human
54
+ obj.code = self.hash.key(human)
55
+ unless obj.code
56
+ raise InvalidCodeAlias, [self.to_s, human]
57
+ end
58
+ obj
59
+ end
62
60
 
63
- def self.hash
64
- {}
61
+ def hash
62
+ {}
63
+ end
65
64
  end
66
65
  end
67
66
 
68
67
  class CodeFromYaml < Code
69
- def self.codelist_filename
70
- File.dirname(__FILE__) + "/../../data/codelists/codelist-#{self.code_ident}.yml"
71
- end
72
-
73
- def self.hash
74
- @hash ||= YAML.load(File.open(codelist_filename))[:codelist]
75
- end
68
+ class << self
69
+ def hash
70
+ @hash ||= YAML.load(File.open(File.dirname(__FILE__) + "/../../data/codelists/codelist-#{self.code_ident}.yml"))[:codelist].freeze
71
+ end
76
72
 
77
- def self.list
78
- self.hash.to_a.map { |h| h.first }
79
- end
73
+ def list
74
+ self.hash.keys
75
+ end
80
76
 
81
- def self.code_ident
82
- nil
83
- end
77
+ def code_ident
78
+ nil
79
+ end
84
80
 
85
- def self.code_identifier code
86
- define_singleton_method :code_ident do
87
- return code
81
+ def code_identifier code
82
+ define_singleton_method :code_ident do
83
+ return code
84
+ end
88
85
  end
89
86
  end
90
87
  end
@@ -109,6 +106,7 @@ module ONIX
109
106
  end
110
107
  end
111
108
  end
109
+
112
110
  class NotificationType < CodeFromYaml
113
111
  code_identifier 1
114
112
  end
data/lib/onix/date.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module ONIX
2
- module DateHelper
2
+ module DateMethods
3
3
  # @return [DateFormat]
4
4
  attr_accessor :date_format
5
5
  # @return [Date]
@@ -31,7 +31,7 @@ module ONIX
31
31
  format = code_format
32
32
 
33
33
  if code_format != text_format
34
- puts "WARN incorrect date format #{text_format} != #{code_format}"
34
+ # puts "WARN incorrect date format #{text_format} != #{code_format}"
35
35
  format = text_format
36
36
  end
37
37
 
@@ -133,9 +133,15 @@ module ONIX
133
133
  end
134
134
 
135
135
  class BaseDate < SubsetDSL
136
- include DateHelper
137
- element "DateFormat", :subset
138
- element "Date", :text
136
+ include DateMethods
137
+
138
+ class << self
139
+ def date_elements(role)
140
+ element role, :subset, :shortcut => :role
141
+ element "DateFormat", :subset
142
+ element "Date", :text
143
+ end
144
+ end
139
145
 
140
146
  # use former date representation
141
147
  # @return [Boolean]
@@ -153,26 +159,26 @@ module ONIX
153
159
  end
154
160
 
155
161
  class MarketDate < BaseDate
156
- element "MarketDateRole", :subset, :shortcut => :role
162
+ date_elements "MarketDateRole"
157
163
 
158
164
  scope :availability, lambda { human_code_match(:market_date_role, ["PublicationDate", "EmbargoDate"]) }
159
165
  end
160
166
 
161
167
  class PriceDate < BaseDate
162
- element "PriceDateRole", :subset, :shortcut => :role
168
+ date_elements "PriceDateRole"
163
169
 
164
170
  scope :from_date, lambda { human_code_match(:price_date_role, "FromDate") }
165
171
  scope :until_date, lambda { human_code_match(:price_date_role, "UntilDate") }
166
172
  end
167
173
 
168
174
  class SupplyDate < BaseDate
169
- element "SupplyDateRole", :subset, :shortcut => :role
175
+ date_elements "SupplyDateRole"
170
176
 
171
177
  scope :availability, lambda { human_code_match(:supply_date_role, ["ExpectedAvailabilityDate", "EmbargoDate"]) }
172
178
  end
173
179
 
174
180
  class PublishingDate < BaseDate
175
- element "PublishingDateRole", :subset, :shortcut => :role
181
+ date_elements "PublishingDateRole"
176
182
 
177
183
  scope :publication, lambda { human_code_match(:publishing_date_role, ["PublicationDate", "PublicationDateOfPrintCounterpart"]) }
178
184
  scope :embargo, lambda { human_code_match(:publishing_date_role, "SalesEmbargoDate") }
@@ -181,12 +187,12 @@ module ONIX
181
187
  end
182
188
 
183
189
  class ContentDate < BaseDate
184
- element "ContentDateRole", :subset, :shortcut => :role
190
+ date_elements "ContentDateRole"
185
191
 
186
192
  scope :last_updated, lambda { human_code_match(:content_date_role, "LastUpdated") }
187
193
  end
188
194
 
189
195
  class ContributorDate < BaseDate
190
- element "ContributorDateRole", :subset, :shortcut => :role
196
+ date_elements "ContributorDateRole"
191
197
  end
192
198
  end
data/lib/onix/entity.rb CHANGED
@@ -7,7 +7,7 @@ module ONIX
7
7
  end
8
8
 
9
9
  class Entity < SubsetDSL
10
- include GlnMethods
10
+ include IdentifiersMethods::Gln
11
11
  include EntityHelper
12
12
 
13
13
  # @return [String]
@@ -60,7 +60,8 @@ module ONIX
60
60
  end
61
61
 
62
62
  class Imprint < SubsetDSL
63
- include GlnMethods
63
+ include IdentifiersMethods::Gln
64
+
64
65
  elements "ImprintIdentifier", :subset, :shortcut => :identifiers, :cardinality => 0..n
65
66
  element "ImprintName", :text, :shortcut => :name, :cardinality => 0..1
66
67
  end
@@ -1,10 +1,11 @@
1
1
  module ONIX
2
2
  class Identifier < SubsetDSL
3
- element "IDTypeName", :text, :shortcut => :name, :cardinality => 0..1
4
- element "IDValue", :text, :shortcut => :value, :cardinality => 1
5
-
6
- def type
7
- nil
3
+ class << self
4
+ def identifier_elements(id_type)
5
+ element id_type, :subset, :shortcut => :type, :cardinality => 1
6
+ element "IDTypeName", :text, :shortcut => :name, :cardinality => 0..1
7
+ element "IDValue", :text, :shortcut => :value, :cardinality => 1
8
+ end
8
9
  end
9
10
 
10
11
  def uniq_id
@@ -13,124 +14,128 @@ module ONIX
13
14
  end
14
15
 
15
16
  class SenderIdentifier < Identifier
16
- element "SenderIDType", :subset, :shortcut => :type, :cardinality => 1
17
+ identifier_elements "SenderIDType"
17
18
  end
18
19
 
19
20
  class AddresseeIdentifier < Identifier
20
- element "AddresseeIDType", :subset, :shortcut => :type, :cardinality => 1
21
+ identifier_elements "AddresseeIDType"
21
22
  end
22
23
 
23
24
  class RecordSourceIdentifier < Identifier
24
- element "RecordSourceIDType", :subset, :shortcut => :type, :cardinality => 1
25
+ identifier_elements "RecordSourceIDType"
25
26
  end
26
27
 
27
28
  class AgentIdentifier < Identifier
28
- element "AgentIDType", :subset, :shortcut => :type, :cardinality => 1
29
+ identifier_elements "AgentIDType"
29
30
  end
30
31
 
31
32
  class ImprintIdentifier < Identifier
32
- element "ImprintIDType", :subset, :shortcut => :type, :cardinality => 1
33
+ identifier_elements "ImprintIDType"
33
34
  end
34
35
 
35
36
  class PublisherIdentifier < Identifier
36
- element "PublisherIDType", :subset, :shortcut => :type, :cardinality => 1
37
+ identifier_elements "PublisherIDType"
37
38
  end
38
39
 
39
40
  class SupplierIdentifier < Identifier
40
- element "SupplierIDType", :subset, :shortcut => :type, :cardinality => 1
41
+ identifier_elements "SupplierIDType"
41
42
  end
42
43
 
43
44
  class NameIdentifier < Identifier
44
- element "NameIDType", :subset, :shortcut => :type, :cardinality => 1
45
+ identifier_elements "NameIDType"
45
46
  end
46
47
 
47
48
  class CollectionIdentifier < Identifier
48
- element "CollectionIDType", :subset, :shortcut => :type, :cardinality => 1
49
+ identifier_elements "CollectionIDType"
49
50
  end
50
51
 
51
52
  class ProductIdentifier < Identifier
52
- element "ProductIDType", :subset, :shortcut => :type, :cardinality => 1
53
+ identifier_elements "ProductIDType"
53
54
  end
54
55
 
55
56
  class ProductContactIdentifier < Identifier
56
- element "ProductContactIDType", :subset, :shortcut => :type, :cardinality => 1
57
+ identifier_elements "ProductContactIDType"
57
58
  end
58
59
 
59
60
  class SalesOutletIdentifier < Identifier
60
- element "SalesOutletIDType", :subset, :shortcut => :type, :cardinality => 1
61
+ identifier_elements "SalesOutletIDType"
61
62
  end
62
63
 
63
64
  class WorkIdentifier < Identifier
64
- element "WorkIDType", :subset, :shortcut => :type, :cardinality => 1
65
+ identifier_elements "WorkIDType"
65
66
  end
66
67
 
67
68
  class SupplyContactIdentifier < Identifier
68
- element "SupplyContactIDType", :subset, :shortcut => :type, :cardinality => 1
69
+ identifier_elements "SupplyContactIDType"
69
70
  end
70
71
 
71
72
  class TextItemIdentifier < Identifier
72
- element "TextItemIDType", :subset, :shortcut => :type, :cardinality => 1
73
+ identifier_elements "TextItemIDType"
73
74
  end
74
75
 
75
- module EanMethods
76
- # EAN string identifier from identifiers
77
- # @return [String]
78
- def ean
79
- if ean_identifier
80
- ean_identifier.value
81
- else
82
- nil
76
+ module IdentifiersMethods
77
+ module Ean
78
+ # EAN string identifier from identifiers
79
+ # @return [String]
80
+ def ean
81
+ if ean_identifier
82
+ ean_identifier.value
83
+ else
84
+ nil
85
+ end
83
86
  end
84
- end
85
87
 
86
- private
87
- def ean_identifier
88
- self.identifiers.select { |id| id.type.human=="Gtin13" }.first || self.identifiers.select { |id| id.type.human=="Isbn13" }.first
89
- end
90
- end
88
+ private
91
89
 
92
- module IsbnMethods
93
- # ISBN-13 string identifier from identifiers
94
- # @return [String]
95
- def isbn13
96
- if isbn13_identifier
97
- isbn13_identifier.value
98
- else
99
- nil
90
+ def ean_identifier
91
+ self.identifiers.select { |id| id.type.human == "Gtin13" }.first || self.identifiers.select { |id| id.type.human == "Isbn13" }.first
100
92
  end
101
93
  end
102
94
 
103
- private
104
- def isbn13_identifier
105
- self.identifiers.select{|id| id.type.human=="Isbn13"}.first
95
+ module Isbn
96
+ # ISBN-13 string identifier from identifiers
97
+ # @return [String]
98
+ def isbn13
99
+ if isbn13_identifier
100
+ isbn13_identifier.value
101
+ else
102
+ nil
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def isbn13_identifier
109
+ self.identifiers.select { |id| id.type.human == "Isbn13" }.first
110
+ end
106
111
  end
107
- end
108
112
 
109
- module GlnMethods
110
- # GLN string identifier from identifiers
111
- # @return [String]
112
- def gln
113
- if gln_identifier
114
- if gln_identifier.value =~ /\d{13}/
115
- gln_identifier.value
113
+ module Gln
114
+ # GLN string identifier from identifiers
115
+ # @return [String]
116
+ def gln
117
+ if gln_identifier
118
+ if gln_identifier.value =~ /\d{13}/
119
+ gln_identifier.value
120
+ else
121
+ # puts "WARN Invalid GLN #{gln_identifier.value}"
122
+ nil
123
+ end
116
124
  else
117
- puts "Invalid GLN #{gln_identifier.value}"
118
125
  nil
119
126
  end
120
- else
121
- nil
122
127
  end
123
- end
124
128
 
125
- # private
126
- def gln_identifier
127
- self.identifiers.select { |id| id.type.human=="Gln" }.first
129
+ # private
130
+ def gln_identifier
131
+ self.identifiers.select { |id| id.type.human == "Gln" }.first
132
+ end
128
133
  end
129
- end
130
134
 
131
- module ProprietaryIdMethods
132
- def proprietary_ids
133
- self.identifiers.select { |id| id.type.human=="Proprietary" }
135
+ module ProprietaryId
136
+ def proprietary_ids
137
+ self.identifiers.select { |id| id.type.human == "Proprietary" }
138
+ end
134
139
  end
135
140
  end
136
141
  end
@@ -1,3 +1,4 @@
1
+ require 'onix/publisher_representative'
1
2
  require 'onix/product_contact'
2
3
 
3
4
  module ONIX