<< | Index | >>
Test-cases are not yet 100% covered
Task
Test run swissindex.rb
masa@masa ~/work $ ruby swissindex_nonpharma_getbypharmacode.rb /usr/lib64/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/utils.rb:138:in `union': can't convert Array into String (TypeError) from /usr/lib64/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/utils.rb:138 from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from /usr/lib64/ruby/gems/1.8/gems/httpi-0.9.0/lib/httpi/request.rb:3 from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from /usr/lib64/ruby/gems/1.8/gems/savon-0.8.6/lib/savon/client.rb:1 from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from /usr/lib64/ruby/gems/1.8/gems/savon-0.8.6/lib/savon.rb:3 from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require' from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require' from swissindex_nonpharma_getbypharmacode.rb:4
Note
Sample script
#! /usr/bin/env ruby # search_migel_by_pharmacode -- 02.05.2011 -- mhatakeyama@ywesee.com require 'rubygems' require 'mechanize' pharmacode = '2355664' base_url = 'https://sample url' agent = Mechanize.new agent.get(base_url + pharmacode) puts agent.page.uri agent.page.search('td').each do |td| puts td.inner_text.chomp.strip puts td.inner_text.chomp.strip.length puts '---' end
Note
(updated) swissindex.rb (for one pharmacode and output)
require 'rubygems' require 'savon' out_file = 'out.csv' pharmacode_file = 'pharmacode_list.dat' class Swissindex def initialize Savon.configure do |config| config.log = false # disable logging config.log_level = :info # changing the log level # config.logger = Rails.logger # using the Rails logger end @client = Savon::Client.new do | wsdl, http | wsdl.document = "https://index.ws.e-mediat.net/Swissindex/NonPharma/ws_NonPharma_V101.asmx?WSDL" end end def get_item(pharmacode) response = @client.request :get_by_pharmacode do soap.xml = '<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <pharmacode xmlns="http://swissindex.e-mediat.net/SwissindexNonPharma_out_V101">' + pharmacode + '</pharmacode> <lang xmlns="http://swissindex.e-mediat.net/SwissindexNonPharma_out_V101">DE</lang> </soap:Body> </soap:Envelope>' end if nonpharma = response.to_hash[:nonpharma] return nonpharma[:item] else return nil end end end # # initialize Savon # swissindex = Swissindex.new # # read pharmacode list # pharmacode_list = File.readlines(pharmacode_file).to_a.map{|line| line.chomp} pharmacode_list.delete("") #p pharmacode_list # # output # open(out_file, "w") do |f| f.print "pharmacode, GTIN, date, status, stdate, lang, description, additional description, company name, company GLN, pharmpreis, ppub, faktor, pzr\n" pharmacode_list.each do |pharmacode| line = [] line << pharmacode if item = swissindex.get_item(pharmacode) line << item[:gtin] line << item[:dt] line << item[:status] line << item[:stdate] line << item[:lang] line << item[:dscr] line << item[:addscr] if company = item[:comp] line << company[:name] line << company[:gln] else line << ""*2 end else line << ""*9 end f.print line.join(','), "\n" end end
Another script (to get date from the other site)
#! /usr/bin/env ruby # encoding: utf-8 # search_migel_by_pharmacode -- 02.05.2011 -- mhatakeyama@ywesee.com require 'rubygems' require 'mechanize' require 'hpricot' pharmacode = '2355664' class Swissindex def initialize @base_url = 'https://sample url' @agent = Mechanize.new end def search(pharmacode) @agent.get(@base_url + pharmacode) count = 100 line = [] @agent.page.search('td').each do |td| text = td.inner_text.chomp.strip if text.is_a?(String) && text.length == 7 && text == pharmacode count = 0 end if count < 7 text = text.split(/\n/)[1] || text.split(/\n/)[0] text = text.gsub(/\302\240/, '').strip line << text count += 1 end end #p line.join(",") line end end #puts agent.page.uri swissindex = Swissindex.new p swissindex.search(pharmacode).join(",")
Merge together above two script (swissindex.rb)
Note