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

#correct_code, #initialize

Constructor Details

This class inherits a constructor from Oddb2xml::Extractor

Instance Method Details

#to_hashObject



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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
# File 'lib/oddb2xml/extractor.rb', line 42

def to_hash
  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[:swissmedic_number5] = (num5 = seq.at_xpath('.//SwissmedicNo5')) ? (num5.text.rjust(5,'0')) : ''
    item[:org_gen_code] = (orgc = seq.at_xpath('.//OrgGenCode'))    ? orgc.text : ''
    item[:deductible]   = (ddbl = seq.at_xpath('.//FlagSB20'))      ? ddbl.text : ''
    item[:atc_code]     = (atcc = seq.at_xpath('.//AtcCode'))       ? atcc.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,
        :name     => (name = sub.at_xpath('.//DescriptionLa')) ? name.text : '',
        :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 = correct_code(phar.to_s, 7)
        # as common key with swissINDEX
        item[:pharmacodes] << phar
        # packages
        item[:packages][phar] = {
          :pharmacode          => phar,
          :ean                 => (ean = pac.at_xpath('.//GTIN')) ? ean.text : '',
          :swissmedic_category => (cat = pac.at_xpath('.//SwissmedicCategory')) ? cat.text : '',
          :swissmedic_number8  => (num = pac.at_xpath('.//SwissmedicNo8'))      ? num.text.rjust(8, '0') : '',
          :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 : '',
            }
          }
        }
        # related all limitations
        item[:packages][phar][:limitations] = []
        limitations = Hash.new{|h,k| h[k] = [] }
        # in seq
        limitations[:seq] = (lims = seq.xpath('.//Limitations/Limitation')) ? lims.to_a : nil
        # in it-codes
        limitations[:itc] = (lims = seq.xpath('.//ItCodes/ItCode/Limitations/Limitation')) ? lims.to_a : nil
        # in pac
        limitations[:pac] = (lims = pac.xpath('.//Limitations/Limitation')) ? lims.to_a : nil
        limitations.each_pair do |lim_key, lims|
          key = ''
          id  = ''
          case lim_key
          when :seq, :itc
            key = :swissmedic_number5
            id  = item[key].to_s
          when :pac
            key = :swissmedic_number8
            id  = item[:packages][phar][key].to_s
          end
          if id.empty? or id == '0'
            key = :pharmacode
            id  = phar.to_s
          end
          lims.each do |lim|
            limitation = {
              :it      => item[:it_code],
              :key     => key,
              :id      => id,
              :code    => (lic = lim.at_xpath('.//LimitationCode'))   ? lic.text : '',
              :type    => (lit = lim.at_xpath('.//LimitationType'))   ? lit.text : '',
              :value   => (liv = lim.at_xpath('.//LimitationValue'))  ? liv.text : '',
              :niv     => (niv = lim.at_xpath('.//LimitationNiveau')) ? niv.text : '',
              :desc_de => (dsc = lim.at_xpath('.//DescriptionDe'))    ? dsc.text : '',
              :desc_fr => (dsc = lim.at_xpath('.//DescriptionFr'))    ? dsc.text : '',
              :vdate   => (dat = lim.at_xpath('.//ValidFromDate'))    ? dat.text : '',
            }
            deleted = false
            if upto = ((thr = lim.at_xpath('.//ValidThruDate')) ? thr.text : nil) and
               upto =~ /\d{2}\.\d{2}\.\d{2}/
              begin
                deleted = true if Date.strptime(upto, '%d.%m.%y') >= Date.today
              rescue ArgumentError
              end
            end
            limitation[:del] = deleted
            item[:packages][phar][:limitations] << limitation
          end
        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