Class: ONIX::ONIXMessage

Inherits:
Subset
  • Object
show all
Defined in:
lib/onix/onix_message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Subset

parse, #tag_match, tag_match, #unsupported

Constructor Details

#initializeONIXMessage

Returns a new instance of ONIXMessage.



58
59
60
61
# File 'lib/onix/onix_message.rb', line 58

def initialize
  @products=[]
  @vault={}
end

Instance Attribute Details

#adresseeObject

Returns the value of attribute adressee.



53
54
55
# File 'lib/onix/onix_message.rb', line 53

def adressee
  @adressee
end

#default_currency_codeObject

Returns the value of attribute default_currency_code.



53
54
55
# File 'lib/onix/onix_message.rb', line 53

def default_currency_code
  @default_currency_code
end

#default_language_of_textObject

Returns the value of attribute default_language_of_text.



53
54
55
# File 'lib/onix/onix_message.rb', line 53

def default_language_of_text
  @default_language_of_text
end

#productsObject

Returns the value of attribute products.



53
54
55
# File 'lib/onix/onix_message.rb', line 53

def products
  @products
end

#releaseObject

Returns the value of attribute release.



53
54
55
# File 'lib/onix/onix_message.rb', line 53

def release
  @release
end

#senderObject

Returns the value of attribute sender.



53
54
55
# File 'lib/onix/onix_message.rb', line 53

def sender
  @sender
end

#sent_date_timeObject

Returns the value of attribute sent_date_time.



53
54
55
# File 'lib/onix/onix_message.rb', line 53

def sent_date_time
  @sent_date_time
end

Instance Method Details

#init_vaultObject

initialize hash between ID and product object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/onix/onix_message.rb', line 88

def init_vault
  @vault={}
  @products.each do |product|
    product.identifiers.each do |ident|
      @vault[ident.uniq_id]=product
    end
  end

  @products.each do |product|
    product.related.each do |rel|
      rel.identifiers.each do |ident|
        if @vault[ident.uniq_id]
          rel.product=@vault[ident.uniq_id]
        end
      end
    end

    product.parts.each do |prt|
      prt.identifiers.each do |ident|
        if @vault[ident.uniq_id]
          prt.product=@vault[ident.uniq_id]
        end
      end
    end
  end
end

#merge!(other) ⇒ Object

merge another message in this one current object erase other values



73
74
75
76
77
78
# File 'lib/onix/onix_message.rb', line 73

def merge!(other)
  @products+=other.products
  @products=@products.uniq { |p| p.ean }
  init_vault
  self
end

#open(arg, force_encoding = nil) ⇒ Object

open with arg detection



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/onix/onix_message.rb', line 116

def open(arg, force_encoding=nil)
  data=ONIX::Helper.arg_to_data(arg)

  xml=nil
  if force_encoding
    xml=Nokogiri::XML.parse(data, nil, force_encoding)
  else
    xml=Nokogiri::XML.parse(data)
  end

  xml.remove_namespaces!
  xml
end

#parse(arg, force_encoding = nil, force_release = nil) ⇒ Object

parse filename or file



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/onix/onix_message.rb', line 131

def parse(arg, force_encoding=nil, force_release=nil)

  xml=open(arg, force_encoding)
  @products=[]

  root = xml.root
  case root
    when tag_match("ONIXMessage")
      @release=root["release"]
      if force_release
        @release=force_release.to_s
      end
      root.elements.each do |e|
        case e
          when tag_match("Header")
            e.elements.each do |t|
              case t
                when tag_match("Sender")
                  @sender=Sender.parse(t)
                when tag_match("Addressee")
                  @addressee=Addressee.parse(t)
                when tag_match("SentDateTime")
                  tm=t.text
                  @sent_date_time=Time.strptime(tm, "%Y%m%dT%H%M%S") rescue Time.strptime(tm, "%Y%m%dT%H%M") rescue Time.strptime(tm, "%Y%m%d") rescue nil
                when tag_match("DefaultLanguageOfText")
                  @default_language_of_text=LanguageCode.parse(t)
                when tag_match("DefaultCurrencyCode")
                  @default_currency_code=t.text
                else
                  unsupported(t)
              end
            end
          when tag_match("Product")
            product=nil
            if @release =~ /^3.0/
              product=Product.parse(e)
            else
              product=ONIX21::Product.parse(e)
            end
            product.default_language_of_text=@default_language_of_text
            product.default_currency_code=@default_currency_code
            @products << product
        end
      end

    when tag_match("Product")
      product=Product.parse(xml.root)
      product.default_language_of_text=@default_language_of_text
      product.default_currency_code=@default_currency_code
      @products << product
  end

  init_vault
end

#select!(&block) ⇒ Object

keep products for which block return true



81
82
83
84
85
# File 'lib/onix/onix_message.rb', line 81

def select! &block
  @products.select! { |p| block.call(p) }
  init_vault
  self
end

#vaultObject



63
64
65
# File 'lib/onix/onix_message.rb', line 63

def vault
  @vault
end

#vault=(v) ⇒ Object



67
68
69
# File 'lib/onix/onix_message.rb', line 67

def vault= v
  @vault = v
end