Class: Oddb2xml::RefdataExtractor

Inherits:
Extractor
  • Object
show all
Defined in:
lib/oddb2xml/extractor.rb

Instance Attribute Summary

Attributes inherited from Extractor

#xml

Instance Method Summary collapse

Constructor Details

#initialize(xml, type) ⇒ RefdataExtractor

Returns a new instance of RefdataExtractor.



204
205
206
207
208
# File 'lib/oddb2xml/extractor.rb', line 204

def initialize(xml, type)
  @type = (type.to_s.upcase == "PHARMA" ? "PHARMA" : "NONPHARMA")
  xml = xml.dup.force_encoding("UTF-8") if xml.encoding.name != "UTF-8"
  super(xml)
end

Instance Method Details

#to_hashObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/oddb2xml/extractor.rb', line 210

def to_hash
  data = {}
  result = SwissRegArticles.parse(@xml.sub(STRIP_FOR_SAX_MACHINE, ""), lazy: true)
  result.Article.each do |article|
    article_type = article.MedicinalProduct.ProductClassification.ProductClass
    if article_type != @type
      next
    end
    ean13 = @type == "PHARMA" ? article.PackagedProduct.DataCarrierIdentifier : article.MedicinalProduct.Identifier
    if ean13.nil? || ean13.empty?
      # Refdata publishes a handful of PHARMA articles that carry no barcode at
      # all -- e.g. the Swiss Red Cross blood products under the collective
      # registration 99999 (Erythrozytenkonzentrat & co), which appeared in
      # August 2026. They have no article identifier we could key on, so skip
      # them. Before this guard the nil blew up the whole extraction, and
      # Cli#download_as rescues that error into a silent Oddb2xml.log call, so
      # *every* Refdata PHARMA article vanished from the feeds while the build
      # still reported success. See GitHub issue #122.
      puts "Refdata #{@type} skipping #{article.MedicinalProduct.Identifier} without an EAN13" if $VERBOSE
      next
    end
    if ean13.size < 13
      puts "Refdata #{@type} use 13 chars not #{ean13.size} for #{ean13}" if $VERBOSE
      ean13 = ean13.rjust(13, "0")
    end
    if ean13.size == 14 && ean13[0] == "0"
      puts "Refdata #{@type} remove leading '0' for #{ean13}" if $VERBOSE
      ean13 = ean13[1..-1]
    end
    item = {}
    item[:ean13] = ean13
    item[:no8] = article.PackagedProduct.RegulatedAuthorisationIdentifier || ""
    item[:data_origin] = "refdata"
    item[:refdata] = true
    item[:_type] = @type.downcase.to_sym
    item[:last_change] = "" # TODO: Date and time of last data change
    item[:desc_de] = ""
    item[:desc_fr] = ""
    item[:desc_it] = ""
    article.PackagedProduct.Name.each do |name|
      if name.Language == "DE"
        item[:desc_de] = name.FullName
      elsif name.Language == "FR"
        item[:desc_fr] = name.FullName
      elsif name.Language == "IT"
        item[:desc_it] = name.FullName
      end
    end
    item[:desc_it] = item[:desc_de] if item[:desc_it].empty? # refdata has no italian name
    item[:atc_code] = article.MedicinalProduct.ProductClassification.Atc || ""
    item[:company_name] = article.PackagedProduct.Holder.Name || ""
    item[:company_ean] = article.PackagedProduct.Holder.Identifier || ""
    data[ean13] = item
  end
  data
end