Class: Oddb2xml::RefdataDownloader

Inherits:
Downloader show all
Defined in:
lib/oddb2xml/downloader.rb

Instance Attribute Summary

Attributes inherited from Downloader

#agent, #type

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, type = :pharma) ⇒ RefdataDownloader

Returns a new instance of RefdataDownloader.



210
211
212
213
214
# File 'lib/oddb2xml/downloader.rb', line 210

def initialize(options={}, type=:pharma)
  @type = (type == :pharma ? 'Pharma' : 'NonPharma')
  url = "http://refdatabase.refdata.ch/Service/Article.asmx?WSDL"
  super(options, url)
end

Instance Method Details

#downloadObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/oddb2xml/downloader.rb', line 225

def download
  begin
    filename =  "refdata_#{@type}.xml"
    file2save = File.join(Downloads, "refdata_#{@type}.xml")
soap = %(<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://refdatabase.refdata.ch/Article_in" xmlns:ns2="http://refdatabase.refdata.ch/">
  <SOAP-ENV:Body>
<ns2:DownloadArticleInput>
  <ns1:ATYPE>#{@type.upcase}</ns1:ATYPE>
</ns2:DownloadArticleInput>
  </SOAP-ENV:Body>
  </SOAP-ENV:Envelope>
</ns1:ATYPE></ns2:DownloadArticleInput></SOAP-ENV:Body>
)
    return IO.read(file2save) if Oddb2xml.skip_download? and File.exists?(file2save)
    FileUtils.rm_f(file2save, :verbose => false)
    response = @client.call(:download, :xml => soap)
    if response.success?
      if xml = response.to_xml
        response = nil # win
        FileUtils.makedirs(Downloads)
        File.open(file2save, 'w+') { |file| file.write xml }
      else
        # received broken data or internal error
        raise StandardError
      end
    else
      raise Timeout::Error
    end
  rescue HTTPI::SSLError
    exit # catch me in Cli class
  rescue Timeout::Error, Errno::ETIMEDOUT
    retrievable? ? retry : raise
  end
  xml
end

#initObject



215
216
217
218
219
220
221
222
223
224
# File 'lib/oddb2xml/downloader.rb', line 215

def init
  config = {
    :log_level       => :info,
    :log             => false, # $stdout
    :raise_errors    => true,
    :ssl_version     => :SSLv3,
    :wsdl            => @url
  }
  @client = Savon::Client.new(config)
end