Class: Oddb2xml::BagXmlExtractor
- Defined in:
- lib/oddb2xml/extractor.rb
Instance Attribute Summary
Attributes inherited from Extractor
Instance Method Summary collapse
Methods inherited from Extractor
Constructor Details
This class inherits a constructor from Oddb2xml::Extractor
Instance Method Details
#to_hash ⇒ Object
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 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 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/oddb2xml/extractor.rb', line 39 def to_hash data = {} result = PreparationsEntry.parse(@xml.sub(Strip_For_Sax_Machine, ''), :lazy => true) result.Preparations.Preparation.each do |seq| item = {} item[:refdata] = true item[:product_key] = seq.ProductCommercial item[:desc_de] = (desc = seq.DescriptionDe) ? desc : '' item[:desc_fr] = (desc = seq.DescriptionFr) ? desc : '' item[:name_de] = (name = seq.NameDe) ? name : '' item[:name_fr] = (name = seq.NameFr) ? name : '' item[:swissmedic_number5] = (num5 = seq.SwissmedicNo5) ? (num5.rjust(5,'0')) : '' item[:org_gen_code] = (orgc = seq.OrgGenCode) ? orgc : '' item[:deductible] = (ddbl = seq.FlagSB20) ? ddbl : '' item[:atc_code] = (atcc = seq.AtcCode) ? atcc : '' item[:comment_de] = (info = seq.CommentDe) ? info : '' item[:comment_fr] = (info = seq.CommentFr) ? info : '' item[:it_code] = '' seq.ItCodes.ItCode.each do |itc| if item[:it_code].to_s.empty? it_code = itc.Code.to_s item[:it_code] = (it_code =~ /(\d+)\.(\d+)\.(\d+)./) ? it_code : '' end end item[:substances] = [] seq.Substances.Substance.each_with_index do |sub, i| item[:substances] << { :index => i.to_s, :name => (name = sub.DescriptionLa) ? name : '', :quantity => (qtty = sub.Quantity) ? qtty : '', :unit => (unit = sub.QuantityUnit) ? unit : '', } end item[:pharmacodes] = [] item[:packages] = {} # pharmacode => package seq.Packs.Pack.each do |pac| phar = pac.Pharmacode.to_i ean = pac.GTIN.to_i # search_key = phar.to_i != 0 ? phar : ean search_key = ean != 0 ? ean : phar # as common key with RefData Pharma/NonPharma data item[:pharmacodes] << phar # packages exf = {:price => '', :valid_date => '', :price_code => ''} if pac.Prices and pac.Prices.ExFactoryPrice exf[:price] = pac.Prices.ExFactoryPrice.Price if pac.Prices.ExFactoryPrice.Price exf[:valid_date] = pac.Prices.ExFactoryPrice.ValidFromDate if pac.Prices.ExFactoryPrice.ValidFromDate exf[:price_code] = pac.Prices.ExFactoryPrice.PriceTypeCode if pac.Prices.ExFactoryPrice.PriceTypeCode end pub = {:price => '', :valid_date => '', :price_code => ''} if pac.Prices and pac.Prices.PublicPrice pub[:price] = pac.Prices.PublicPrice.Price if pac.Prices.PublicPrice.Price pub[:valid_date] = pac.Prices.PublicPrice.ValidFromDate if pac.Prices.PublicPrice.ValidFromDate pub[:price_code] = pac.Prices.PublicPrice.PriceTypeCode if pac.Prices.PublicPrice.PriceTypeCode end item[:packages][search_key] = { :pharmacode => phar, :ean => (ean) ? ean : '', :swissmedic_category => (cat = pac.SwissmedicCategory) ? cat : '', :swissmedic_number8 => (num = pac.SwissmedicNo8) ? num.rjust(8, '0') : '', :narcosis_flag => (flg = pac.FlagNarcosis) ? flg : '', :prices => { :exf_price => exf, :pub_price => pub }, } # related all limitations item[:packages][search_key][:limitations] = [] limitations = Hash.new{|h,k| h[k] = [] } if seq.Limitations limitations[:seq] = seq.Limitations.Limitation.collect { |x| x } else limitations[:seq] = nil end # in it-codes if seq and seq.ItCodes and seq.ItCodes.ItCode limitations[:itc] = [] seq.ItCodes.ItCode.each { |x| limitations[:itc] += x.Limitations.Limitation if x.Limitations.Limitation} else limitations[:itc] =nil end # in pac if pac and pac.Limitations limitations[:pac] = (lims = pac.Limitations.Limitation) ? lims.to_a : nil else limitations[:pac] = nil end 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][search_key][key].to_s end if id.empty? or id == '0' key = :pharmacode id = phar.to_i end lims.each do |lim| limitation = { :it => item[:it_code], :key => key, :id => id, :code => (lic = lim.LimitationCode) ? lic : '', :type => (lit = lim.LimitationType) ? lit : '', :value => (liv = lim.LimitationValue) ? liv : '', :niv => (niv = lim.LimitationNiveau) ? niv : '', :desc_de => (dsc = lim.DescriptionDe) ? dsc : '', :desc_fr => (dsc = lim.DescriptionFr) ? dsc : '', :vdate => (dat = lim.ValidFromDate) ? dat : '', } deleted = false if upto = ((thr = lim.ValidThruDate) ? thr : 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][search_key][:limitations] << limitation end if lims end # limitation points pts = pac.PointLimitations.PointLimitation.first # only first points item[:packages][search_key][:limitation_points] = pts ? pts.Points : '' data[search_key] = item end end data end |