Class: Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/sekka/downloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, savepath = nil) ⇒ Downloader

Returns a new instance of Downloader.



41
42
43
44
# File 'lib/sekka/downloader.rb', line 41

def initialize( url, savepath = nil )
  @url_str = url
  @body = nil
end

Instance Method Details

#calcMD5Object



98
99
100
# File 'lib/sekka/downloader.rb', line 98

def calcMD5()
  Digest::MD5.hexdigest @body
end

#clearBodyObject



94
95
96
# File 'lib/sekka/downloader.rb', line 94

def clearBody()
  @body = nil
end

#downloadObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sekka/downloader.rb', line 46

def download()
  url = URI.parse(@url_str)
  if(url)
    req = Net::HTTP::Get.new(url.path)
    http = Net::HTTP.new(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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sekka/downloader.rb', line 60

def downloadToFile(path)
  url = URI.parse(@url_str)
  req = Net::HTTP::Get.new url.path
  http = Net::HTTP.new(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

#getBodyObject



84
85
86
# File 'lib/sekka/downloader.rb', line 84

def getBody()
  return @body
end

#getBodySizeObject



76
77
78
79
80
81
82
# File 'lib/sekka/downloader.rb', line 76

def getBodySize()
  size = 0
  if @body
    size = @body.size
  end
  return size
end

#saveAs(path) ⇒ Object



88
89
90
91
92
# File 'lib/sekka/downloader.rb', line 88

def saveAs(path)
  open(path,"w") {|f|
    f.write(@body)
  }
end