class Gtin2atc::Downloader

Attributes

type[R]

Public Class Methods

new(options={}, url=nil) click to toggle source
# File lib/gtin2atc/downloader.rb, line 38
def initialize(options={}, url=nil)
  @options     = options
  @url         = url
  @retry_times = 3
  HTTPI.log = false # disable httpi warning
  Gtin2atc.log "Downloader from #{@url} for #{self.class}"
  init
end

Public Instance Methods

init() click to toggle source
# File lib/gtin2atc/downloader.rb, line 46
def init
  @agent = Mechanize.new
  @agent.user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0'
  @agent.redirect_ok         = true
  @agent.redirection_limit   = 5
  @agent.follow_meta_refresh = true
  if RUBY_PLATFORM =~ /mswin|mingw|bccwin|cygwin/i and
     ENV['SSL_CERT_FILE'].nil?
    cert_store = OpenSSL::X509::Store.new
    cert_store.add_file(File.expand_path('../../../tools/cacert.pem', __FILE__))
    @agent.cert_store = cert_store
  end
  Util.debug_msg "Downloader @agent ist #{@agent}"
end

Protected Instance Methods

read_xml_from_zip(target, zipfile) click to toggle source
# File lib/gtin2atc/downloader.rb, line 70
def read_xml_from_zip(target, zipfile)
  Gtin2atc.log "read_xml_from_zip target is #{target} zip: #{zipfile} #{File.exists?(zipfile)}"
  entry = nil
  Dir.glob(File.join(Util.get_archive, '*')).each { |name| if target.match(name) then entry = name; break end }
  if entry
    dest = "#{Util.get_archive}/#{File.basename(entry)}"
    if File.exists?(dest)
      Gtin2atc.log "read_xml_from_zip return content of #{dest} #{File.size(dest)} bytes "
      return IO.read(dest)
    else
      Gtin2atc.log "read_xml_from_zip could not read #{dest}"
    end
  else
    Gtin2atc.log "read_xml_from_zip could not find #{target.to_s}"
  end
  xml = ''
  if RUBY_PLATFORM =~ /mswin|mingw|bccwin|cygwin/i
    Zip::File.open(zipfile) do |zipFile|
      zipFile.each do |entry|
        if entry.name =~ target
          Gtin2atc.log "read_xml_from_zip reading #{__LINE__}: #{entry.name}"
          io = entry.get_input_stream
          until io.eof?
            bytes = io.read(1024)
            xml << bytes
            bytes = nil
          end
          io.close if io.respond_to?(:close)
          dest = "#{Util.get_archive}/#{File.basename(entry.name)}"
          File.open(dest, 'w+') { |f| f.write xml }
          Gtin2atc.log "read_xml_from_zip saved as #{dest}"
        end
      end
    end
  else
    Zip::File.foreach(zipfile) do |entry|
      if entry.name =~ target
        Gtin2atc.log "read_xml_from_zip #{__LINE__}: reading #{entry.name}"
        dest = "#{Util.get_archive}/#{File.basename(entry.name)}"
        entry.get_input_stream { |io| xml = io.read }
        File.open(dest, 'w+') { |f| f.write xml }
        Gtin2atc.log "read_xml_from_zip saved as #{dest}"
      end
    end
  end
  xml
end
retrievable?() click to toggle source
# File lib/gtin2atc/downloader.rb, line 61
def retrievable?
  if @retry_times > 0
    sleep 5
    @retry_times -= 1
    true
  else
    false
  end
end