Class: Oddb2xml::SwissIndexExtractor

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) ⇒ SwissIndexExtractor

Returns a new instance of SwissIndexExtractor.



91
92
93
94
# File 'lib/oddb2xml/extractor.rb', line 91

def initialize(xml, type)
  @type = (type == :pharma ? 'PHARMA' : 'NONPHARMA')
  super(xml)
end

Instance Method Details

#to_hashObject



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
# File 'lib/oddb2xml/extractor.rb', line 95

def to_hash
  #File.open("../swissindex_#{@type.downcase}.xml", 'r:ASCII-8BIT'){|f| @xml = f.read }
  # pharmacode => package
  data = {}
  doc = Nokogiri::XML(@xml)
  doc.remove_namespaces!
  doc.xpath("//Envelope/Body/#{@type}/ITEM").each do |pac|
    item = {}
    item[:ean]             = (gtin = pac.at_xpath('.//GTIN'))   ? gtin.text : ''
    item[:pharmacode]      = (phar = pac.at_xpath('.//PHAR'))   ? phar.text : ''
    item[:status]          = (stat = pac.at_xpath('.//STATUS')) ? stat.text : ''
    item[:stat_date]       = (date = pac.at_xpath('.//SDATE'))  ? date.text : ''
    item[:lang]            = (lang = pac.at_xpath('.//LANG'))   ? lang.text : ''
    item[:desc]            = (dscr = pac.at_xpath('.//DSCR'))   ? dscr.text : ''
    item[:atc_code]        = (code = pac.at_xpath('.//ATC'))    ? code.text : ''
    # as quantity text
    item[:additional_desc] = (dscr = pac.at_xpath('.//ADDSCR')) ? dscr.text : ''
    if comp = pac.xpath('.//COMP')
      item[:company_name] = (nam = comp.at_xpath('.//NAME')) ? nam.text : ''
      item[:company_ean]  = (gln = comp.at_xpath('.//GLN'))  ? gln.text : ''
    end
    unless item[:pharmacode].empty?
      data[item[:pharmacode]] = item
    end
  end
  data
end