Class: Oddb2xml::BagXmlExtractor

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

Instance Attribute Summary

Attributes inherited from Extractor

#xml

Instance Method Summary collapse

Methods inherited from Extractor

#initialize

Constructor Details

This class inherits a constructor from Oddb2xml::Extractor

Instance Method Details

#to_hashObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/oddb2xml/extractor.rb', line 13

def to_hash
  #File.open('../bagxml.xml', 'r:ASCII-8BIT') {|f| @xml = f.read }
  # pharmacode => sequence
  data = {}
  doc = Nokogiri::XML(@xml)
  doc.xpath('//Preparation').each do |seq|
    item = {}
    item[:product_key]  = seq.attr('ProductCommercial').to_s
    item[:desc_de]      = (desc = seq.at_xpath('.//DescriptionDe')) ? desc.text : ''
    item[:desc_fr]      = (desc = seq.at_xpath('.//DescriptionFr')) ? desc.text : ''
    item[:name_de]      = (name = seq.at_xpath('.//NameDe'))        ? name.text : ''
    item[:name_fr]      = (name = seq.at_xpath('.//NameFr'))        ? name.text : ''
    item[:org_gen_code] = (orgc = seq.at_xpath('.//OrgGenCode'))    ? orgc.text : ''
    item[:deductible]   = (ddbl = seq.at_xpath('.//FlagSB20'))      ? ddbl.text : ''
    item[:atc_code]     = (orgc = seq.at_xpath('.//AtcCode'))       ? orgc.text : ''
    item[:comment_de]   = (info = seq.at_xpath('.//CommentDe'))     ? info.text : ''
    item[:comment_fr]   = (info = seq.at_xpath('.//CommentFr'))     ? info.text : ''
    item[:it_code]      = ''
    seq.xpath('.//ItCode').each do |itc|
      if item[:it_code].to_s.empty?
        it_code = itc.attr('Code').to_s
        item[:it_code] = (it_code =~ /(\d+)\.(\d+)\.(\d+)./) ? it_code : ''
      end
    end
    item[:substances] = []
    seq.xpath('.//Substance').each_with_index do |sub, i|
      item[:substances] << {
        :index    => i.to_s,
        :quantity => (qtty = sub.at_xpath('.//Quantity'))     ? qtty.text : '',
        :unit     => (unit = sub.at_xpath('.//QuantityUnit')) ? unit.text : '',
      }
    end
    item[:pharmacodes] = []
    item[:packages]    = {} # pharmacode => package
    seq.xpath('.//Pack').each do |pac|
      if phar = pac.attr('Pharmacode')
        phar = phar.to_s
        # as common key with swissINDEX
        item[:pharmacodes] << phar
        # packages
        item[:packages][phar] = {
          :pharmacode          => phar,
          :swissmedic_category => (cat = pac.at_xpath('.//SwissmedicCategory')) ? cat.text : '',
          :swissmedic_number   => (num = pac.at_xpath('.//SwissmedicNo8'))      ? num.text : '',
          :narcosis_flag       => (flg = pac.at_xpath('.//FlagNarcosis'))       ? flg.text : '',
          :prices              => {
            :exf_price => {
              :price      => (exf = pac.at_xpath('.//ExFactoryPrice/Price'))         ? exf.text : '',
              :valid_date => (exf = pac.at_xpath('.//ExFactoryPrice/ValidFromDate')) ? exf.text : '',
              :price_code => (exf = pac.at_xpath('.//ExFactoryPrice/PriceTypeCode')) ? exf.text : '',
            },
            :pub_price => {
              :price      => (pub = pac.at_xpath('.//PublicPrice/Price'))         ? pub.text : '',
              :valid_date => (pub = pac.at_xpath('.//PublicPrice/ValidFromDate')) ? pub.text : '',
              :price_code => (pub = pac.at_xpath('.//PublicPrice/PriceTypeCode')) ? pub.text : '',
            }
          }
        }
        # limitation
        item[:packages][phar][:limitations] = []
        pac.xpath('.//Limitation').each do |lim|
          item[:packages][phar][:limitations] << {
            :code => (lic = lim.at_xpath('.//LimitationCode')) ? lic.text : '',
            :type => (lit = lim.at_xpath('.//LimitationType')) ? lit.text : '',
          }
        end
        # limitation points
        pts = pac.at_xpath('.//PointLimitations/PointLimitation/Points') # only first points
        item[:packages][phar][:limitation_points] = pts ? pts.text : ''
        # pharmacode => seq (same data)
        data[phar] = item
      end
    end
  end
  data
end