Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# How to download Systembolaget's products into your Rails app

require 'rubygems'
require 'hpricot'

field_map = {
  "@nr" => :sku,
  "namn/text()|namn2/text()" => :name,
  "argang/text()" => :vintage,
  "ursprung/text()" => :region,
  "ursprunglandnamn/text()" => :country,
  "producent/text()" => :producer,
  "volymiml/text()" => :volume,
  "alkoholhalt/text()" => :proof,
  "prisinklmoms/text()" => :price,
  "provadargang/text()" => nil,
  "saljstart/text()" => nil,
  "slutlev/text()" => nil,
  "varugrupp/text()" => nil,
}

# http://systembolaget.se/Applikationer/Knappar/Press/Alla+Artiklar/
doc = Hpricot(File.read('/Users/plindberg/Downloads/systemet.xml'))
(doc/"artikel[varugrupp:contains('VINER')]").each do |article|
  wine = Wine.new
  for expression, field in field_map
    next if field.nil?
    wine[field] = unless expression =~ /@(.*)/
      (article/expression).join(' ')
    else
      article[$1.intern]
    end
  end
  wine.save
end