Module: ODDB::Util::Updater

Defined in:
lib/oddb/util/updater.rb

Constant Summary collapse

DIMDI_INDEX =
"http://www.dimdi.de/static/de/amg/fbag/index.htm"

Class Method Summary collapse

Class Method Details

._reported_import(importer, args = {}, error_args = {}, &block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/oddb/util/updater.rb', line 108

def Updater._reported_import(importer, args={}, error_args={}, &block)
  lines = [
    sprintf("%s: %s %s#import", Time.now.strftime('%c'),
            ODDB.config.server_name, importer.class)
  ]
  lines.concat block.call(importer)
rescue Exception => err
  lines.push(err.class.to_s, err.message, *err.backtrace)
  if importer.respond_to?(:report)
    report = if importer.method(:report).arity == 0
               importer.report
             else
               importer.report(error_args)
             end rescue [$!.message]
    lines.concat report
  end
ensure
  ft = args[:filetype]
  fmt = ft ? "%s: %s %s (%s)" : "%s: %s %s"
  subject = sprintf(fmt, Time.now.strftime('%c'), ODDB.config.server_name,
                    args[:subject] || importer.class, ft)
  Mail.notify_admins(subject, lines)
end

.import_dimdiObject



17
18
19
20
21
22
23
# File 'lib/oddb/util/updater.rb', line 17

def Updater.import_dimdi
  if(date = Import::Dimdi.current_date(DIMDI_INDEX))
    import_dimdi_substances(date)
    import_dimdi_galenic_forms(date)
    import_dimdi_products(date)
  end
end

.import_dimdi_galenic_forms(date) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/oddb/util/updater.rb', line 24

def Updater.import_dimdi_galenic_forms(date)
  file = date.strftime("darreichungsformen-%Y%m.xls")
  Import::Dimdi.download(file) { |io|
    reported_import(Import::Dimdi::GalenicForm.new(date), io,
                    :filetype => 'XLS')
  }
end

.import_dimdi_products(date) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/oddb/util/updater.rb', line 31

def Updater.import_dimdi_products(date)
  file = date.strftime("festbetraege-%Y%m.xls")
  Import::Dimdi.download(file) { |io|
    reported_import(Import::Dimdi::Product.new(date), io,
                   :subject => "Update Festbeträge",
                   :filetype => 'XLS')
  }
end

.import_dimdi_substances(date) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/oddb/util/updater.rb', line 39

def Updater.import_dimdi_substances(date)
  file = date.strftime("wirkstoffkuerzel-%Y%m.xls")
  Import::Dimdi.download(file) { |io|
    reported_import(Import::Dimdi::Substance.new(date), io,
                    :filetype => 'XLS')
  }
end

.import_fachinfos(term, opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/oddb/util/updater.rb', line 46

def Updater.import_fachinfos(term, opts = {})
  importer = Import::PharmNet::Importer.new
  _reported_import(importer, :filetype => 'HTML') {
    importer.import_missing(Mechanize.new, term, opts)
  }
rescue StandardError => error
  ODDB.logger.error('Updater') { error.message }
end

.import_gkv(opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/oddb/util/updater.rb', line 54

def Updater.import_gkv(opts = {})
  importer = Import::Gkv.new
  if url = opts[:pdf] || importer.latest_url(Mechanize.new, opts)
    importer.download_latest url, opts do |fh|
      reported_import(importer, fh,
                      :subject => 'Zubef', :filetype => 'PDF')
    end
  else
    # This is temporary solution.
    # This should be composed in reporting email or logging process.
    host = 'https://www.gkv-spitzenverband.de'
    url = '/Befreiungsliste_Arzneimittel_Versicherte.gkvnet'
    print "WARNING: Updater.import_gkv did nothing. It looks failing in grabbing PDF link.\n" 
    print "Check HTML source code at " + host + url + "\n"
    print "Probably you have to modify Gkv#latest_url method."
    print "20100910 masa\n"
    return nil
  end
end

.import_missing(name) ⇒ Object



73
74
75
76
77
# File 'lib/oddb/util/updater.rb', line 73

def Updater.import_missing(name)
  name.split('|').each do |term|
    Updater.import_fachinfos term, :info_unrestricted => true
  end
end

.import_pharmnet(opts = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/oddb/util/updater.rb', line 78

def Updater.import_pharmnet(opts = {})
  opts = { :replace => true,  :reload  => false, 
           :remove  => false, :repair  => false,
           :reparse => false, :retries => 3,
           :retry_unit => 60 }.update opts
  importer = Import::PharmNet::Importer.new
  _reported_import(importer, {:filetype => 'HTML'}, {:skip_totals => true}) {
    importer._import(Mechanize.new, Drugs::Sequence.all, opts)
  }
rescue StandardError => error
  ODDB.logger.error('Updater') { error.message }
end

.import_product_infosObject



90
91
92
93
94
# File 'lib/oddb/util/updater.rb', line 90

def Updater.import_product_infos
  Import::Csv::ProductInfos.download_latest { |io|
    reported_import(Import::Csv::ProductInfos.new, io, :filetype => 'CSV')
  }
end

.import_whocc_guidelinesObject



95
96
97
98
# File 'lib/oddb/util/updater.rb', line 95

def Updater.import_whocc_guidelines
  reported_import(Import::Whocc::Guidelines.new, 
                  Mechanize.new, :filetype => 'HTML')
end

.report_fachinfosObject



99
100
101
102
103
104
# File 'lib/oddb/util/updater.rb', line 99

def Updater.report_fachinfos
  importer = Import::PharmNet::Importer.new
  _reported_import(importer) {
    importer.report
  }
end

.reported_import(importer, io, subject = nil) ⇒ Object



105
106
107
# File 'lib/oddb/util/updater.rb', line 105

def Updater.reported_import(importer, io, subject=nil)
  _reported_import(importer, subject) { importer.import io }
end

.run(today = Date.today) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/oddb/util/updater.rb', line 131

def Updater.run(today = Date.today)
  run_logged_job 'import_dimdi'
  run_logged_job 'import_gkv'
  case today.day
  when 1
    run_logged_job 'import_pharmnet'
    run_logged_job 'import_whocc'
  when 15
    run_logged_job 'import_pharma24'
  end
end

.run_logged_job(job) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/oddb/util/updater.rb', line 142

def Updater.run_logged_job job
  dir = ODDB.config.oddb_dir
  cmd = File.join dir, 'jobs', job
  log = File.join dir, 'log', job
  IO.popen "#{cmd} log_file=#{log}" do |io|
    # wait for importer to exit
  end
end

.update_prices(packages = Drugs::Package.all, opts = {:all => false}) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/oddb/util/updater.rb', line 150

def Updater.update_prices(packages = Drugs::Package.all,
                          opts={:all => false})
  importer = Import::Pharma24.new
  _reported_import(importer) {
    importer.import Mechanize.new, packages
  }
rescue StandardError => error
  ODDB.logger.error('Updater') { error.message }
end