Class: Downer::DownloadItem

Inherits:
Object
  • Object
show all
Defined in:
lib/downer/download_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, destination) ⇒ DownloadItem

Returns a new instance of DownloadItem.



11
12
13
14
# File 'lib/downer/download_item.rb', line 11

def initialize(url, destination)
  @url, @destination = sanitize_url(url), destination
  @uri = URI.parse(@url)      
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/downer/download_item.rb', line 9

def content
  @content
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/downer/download_item.rb', line 8

def url
  @url
end

Instance Method Details

#downloadObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/downer/download_item.rb', line 26

def download
  @http = Net::HTTP.new(@uri.host, @uri.port)
  if @uri.respond_to? :request_uri
    req = Net::HTTP::Get.new(@uri.request_uri)
    response = @http.request(req)

    if response.code != '200'
      fd = FailedDownload.new
      fd.http_code = response.code
      fd.url = @url
      raise fd
    else
      @content = response.body
      write_to_file
    end
  else
    puts "WARNING: Ignored download for #{@uri.inspect}"
  end

end

#get_save_filenameObject

Returns the name which this file will be saved as



17
18
19
20
21
22
23
24
# File 'lib/downer/download_item.rb', line 17

def get_save_filename
  file_name = CGI.unescape(@uri.to_s)
  file_name = file_name.gsub(/https?:\/\//,'').split('/').last
  #file_name = file_name.gsub('%5B', '[')
  #file_name = file_name.gsub('%5D', ']')
  file_name
  # TODO : refine to auto append file extentions based on content type
end