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

Methods inherited from Extractor

#correct_code

Constructor Details

#initialize(xml, type) ⇒ SwissIndexExtractor

Returns a new instance of SwissIndexExtractor.



163
164
165
166
# File 'lib/oddb2xml/extractor.rb', line 163

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

Instance Method Details

#to_hashObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/oddb2xml/extractor.rb', line 167

def to_hash
  data = {}
  doc = Nokogiri::XML(@xml)
  doc.remove_namespaces!
  doc.xpath("//Envelope/Body/#{@type}/ITEM").each do |pac|
    item = {}
    item[:_type]           = @type.downcase.intern
    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.to_s : ''
    # 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?
      item[:pharmacode] = correct_code(item[:pharmacode].to_s, 7)
      unless data[item[:pharmacode]] # pharmacode => GTINs
        data[item[:pharmacode]] = []
      end
      data[item[:pharmacode]] << item
    end
  end
  data
end