class Downloader

Public Class Methods

new( url, savepath = nil ) click to toggle source
# File lib/sekka/downloader.rb, line 41
def initialize( url, savepath = nil )
  @url_str = url
  @body = nil
end

Public Instance Methods

calcMD5() click to toggle source
# File lib/sekka/downloader.rb, line 114
def calcMD5()
  Digest::MD5.hexdigest @body
end
clearBody() click to toggle source
# File lib/sekka/downloader.rb, line 110
def clearBody()
  @body = nil
end
download() click to toggle source
# File lib/sekka/downloader.rb, line 62
def download()
  url = URI.parse(@url_str)
  if(url)
    req = Net::HTTP::Get.new(url.path)
    http = httpInstance(url.scheme,url.host,url.port)
    if url.scheme == 'https'
      http.use_ssl = true
    end
    res = http.request(req)
    @body = res.body
  end
  return @body
end
downloadToFile(path) click to toggle source
# File lib/sekka/downloader.rb, line 76
def downloadToFile(path)
  url = URI.parse(@url_str)
  req = Net::HTTP::Get.new(url.path)
  http = httpInstance(url.scheme,url.host,url.port)
  if url.scheme == 'https'
    http.use_ssl = true
  end
  http.request req do |response|
    open path, 'wb' do |io|
      response.read_body do |chunk|
        io.write chunk
      end
    end
  end
end
getBody() click to toggle source
# File lib/sekka/downloader.rb, line 100
def getBody()
  return @body
end
getBodySize() click to toggle source
# File lib/sekka/downloader.rb, line 92
def getBodySize()
  size = 0
  if @body
    size = @body.size
  end
  return size
end
httpInstance(scheme,host,port) click to toggle source
# File lib/sekka/downloader.rb, line 46
def httpInstance(scheme,host,port)
  if scheme == 'https'
    http_proxy = ENV['https_proxy']
  else
    http_proxy = ENV['http_proxy']
  end
  if http_proxy
    (p_host,p_port) = http_proxy.split(':')
    proxy_class = Net::HTTP::Proxy(p_host,p_port.to_i)
    http = proxy_class.new(host,port)
  else
    http = Net::HTTP.new(host,port)
  end
  return http
end
saveAs(path) click to toggle source
# File lib/sekka/downloader.rb, line 104
def saveAs(path)
  open(path,"w") {|f|
    f.write(@body)
  }
end