Class: ODDB::Import::Pharma24
- Defined in:
- lib/oddb/import/pharma24.rb
Instance Method Summary collapse
- #extract_data(page) ⇒ Object
- #get_alphabetical(agent, fst, snd) ⇒ Object
- #import(agent, packages, opts = {:all => false}) ⇒ Object
- #import_company(data) ⇒ Object
- #import_size(data, package) ⇒ Object
-
#initialize ⇒ Pharma24
constructor
A new instance of Pharma24.
- #interesting_tables(node) ⇒ Object
- #report ⇒ Object
- #search(agent, term) ⇒ Object
- #td_with_text(table, text) ⇒ Object
- #update_package(agent, package, opts = {}) ⇒ Object
- #update_price(package, type, amount) ⇒ Object
Methods inherited from Importer
#capitalize_all, #company_name, #postprocess, #utf8
Constructor Details
#initialize ⇒ Pharma24
Returns a new instance of Pharma24.
10 11 12 13 14 15 |
# File 'lib/oddb/import/pharma24.rb', line 10 def initialize @count = 0 @created_companies = 0 @found = 0 @host = 'http://www.apotheke-online-internet.de' end |
Instance Method Details
#extract_data(page) ⇒ Object
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 |
# File 'lib/oddb/import/pharma24.rb', line 73 def extract_data page data = [] ## this should be page/'table[h2/a]' # -> but Nokogiri apparently can't handle that all_tables = interesting_tables page duplicates = [] all_tables.each do |table| duplicates.concat interesting_tables(table) end (all_tables - duplicates).each do |table| link, = table/'h2/a' if link prod = { :name => utf8(link.inner_text), :url => link.attribute('href').to_s, } if price = (table/:strong).first prod.store :price_public, price.inner_text.tr(',', '.').to_f end ## should be (table/'td[text()="Abgabehinweis:"]').first # -> but Nokogiri apparently can't handle that if prescription = td_with_text(table, "Abgabehinweis:") td, = prescription.xpath('following-sibling::td') prod.store :code_prescription, !!/Rezeptpflichtig/.match(td.inner_text) end ## should be (table/'td[text()="Packungsinhalt:"]').first # -> but Nokogiri apparently can't handle that if content = td_with_text(table, "Packungsinhalt:") td, = content.xpath('following-sibling::td') size_str = td.inner_text if match = /\s*(.*)\s+(\S+)\s+(\S+)\s*$/.match(size_str) size = utf8 match[1] unit = utf8 match[2] name = utf8 match[3] if size.empty? size, unit, name = unit, name, nil end prod.update :size => size, :unit => unit, :unitname => name end end if company = (table/'a[@class="liste"]').first prod.store :company, utf8(company.inner_text) end data.push prod end end data end |
#get_alphabetical(agent, fst, snd) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/oddb/import/pharma24.rb', line 62 def get_alphabetical agent, fst, snd url = "#@host/#{fst}#{snd}.html" page = agent.get url data = extract_data page while (link = (page/'//a[@class="pageResults"]').last) \ && link.inner_text == '[n?chste?>>]' page = agent.get link.attributes['href'] data.concat extract_data(page) end data end |
#import(agent, packages, opts = {:all => false}) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/oddb/import/pharma24.rb', line 16 def import(agent, packages, opts={:all => false}) agent.max_history = 1 packages.collect! { |package| package.odba_id } while id = packages.shift update_package(agent, ODBA.cache.fetch(id), opts) end report end |
#import_company(data) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/oddb/import/pharma24.rb', line 24 def import_company(data) name = company_name(data[:company]) company = Business::Company.find_by_name(name) if(company.nil?) @created_companies += 1 company = Business::Company.new company.name.de = name end company end |
#import_size(data, package) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/oddb/import/pharma24.rb', line 34 def import_size(data, package) part = package.parts.first || package.add_part(Drugs::Part.new) dose, size, multi = data[:size].to_s.split(/x/i, 3).reverse.compact unit = data[:unit].to_s if(unit != 'St') part.quantity = Drugs::Dose.new(dose, unit) elsif(multi.nil?) multi = size size = dose end multi = multi.to_i size = size.to_i part.multi = (multi > 0) ? multi : nil part.size = (size > 0) ? size : nil if(unitname = data[:unitname]) unit = Drugs::Unit.find_by_name(unitname) unless(unit) unit = Drugs::Unit.new unit.name.de = unitname unit.save end part.unit = unit end part.save end |
#interesting_tables(node) ⇒ Object
59 60 61 |
# File 'lib/oddb/import/pharma24.rb', line 59 def interesting_tables node (node/'table').find_all do |inner_node| !(inner_node/'h2/a').empty? end.to_a end |
#report ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/oddb/import/pharma24.rb', line 122 def report lines = [ sprintf("Checked %5i Packages", @count), sprintf("Updated %5i Packages", @found), sprintf("Created %5i Companies", @created_companies), ] lines end |
#search(agent, term) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/oddb/import/pharma24.rb', line 130 def search agent, term url = "#@host/advanced_search_result.php?keywords=#{term}" page = agent.get url extract_data page rescue Zlib::GzipFile::Error => err retries ||= 3 if retries > 0 retries -= 1 retry else err. << " after 3 retries - url: #{url}" raise err end rescue StandardError => err err. << " url: #{url}" raise err end |
#td_with_text(table, text) ⇒ Object
147 148 149 150 151 152 |
# File 'lib/oddb/import/pharma24.rb', line 147 def td_with_text table, text nodes = (table/'td').find_all do |node| node.text.strip == text end nodes.first end |
#update_package(agent, package, opts = {}) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/oddb/import/pharma24.rb', line 153 def update_package agent, package, opts={} price = package.price(:public) resale = [ :pharma24, :csv_product_infos ].include?(package.data_origin(:price_public)) needs_update = opts[:all] || price.nil? || resale if needs_update && (code = package.code(:cid, 'DE')) @count += 1 data, = search agent, code.value if data @found += 1 package.name.de = u(data[:name]) presc = data[:code_prescription] if(code = package.code(:prescription)) if(code.value != presc) code.value = presc end else package.add_code Util::Code.new(:prescription, presc, 'DE') end amount = data[:price_public] if(amount > 0) update_price package, :public, amount if presc update_price package, :exfactory, package._price_exfactory end end import_size data, package package.save if((product = package.product) && product.company.nil?) product.company = import_company(data) product.save end end end end |
#update_price(package, type, amount) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/oddb/import/pharma24.rb', line 188 def update_price package, type, amount dotype = :"price_#{type}" # if this price has been edited manually we won't overwrite unless((data_origin = package.data_origin(dotype)) \ && data_origin.to_s.include?('@')) either = false if(price = package.price(type, 'DE')) if(price != amount) price.amount = amount either = true end else price = Util::Money.new(amount, type, 'DE') package.add_price(price) either = true end if either package.data_origins.store dotype, :pharma24 end end end |