Class: Downer::DownloadManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_source, target_directory, output, options = {}) ⇒ DownloadManager

Returns a new instance of DownloadManager.



15
16
17
18
19
20
# File 'lib/downer/download_manager.rb', line 15

def initialize(url_source, target_directory, output, options ={})
  @url_source = url_source
  @output = output
  @target_directory = append_slash_to_path(target_directory) 
  @strategy = StrategyFinder::find_strategy(@url_source, options)
end

Instance Attribute Details

#downloaded_filesObject (readonly)

Returns the value of attribute downloaded_files.



13
14
15
# File 'lib/downer/download_manager.rb', line 13

def downloaded_files
  @downloaded_files
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

#source_typeObject

Returns the value of attribute source_type.



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

def source_type
  @source_type
end

#strategyObject (readonly)

Returns the value of attribute strategy.



13
14
15
# File 'lib/downer/download_manager.rb', line 13

def strategy
  @strategy
end

#target_directoryObject

Returns the value of attribute target_directory.



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

def target_directory
  @target_directory
end

#urlsObject (readonly)

Returns the value of attribute urls.



13
14
15
# File 'lib/downer/download_manager.rb', line 13

def urls
  @urls
end

Instance Method Details

#check_directoryObject

Determine whether the direcotry specified exists. If it does not, see if its parent is writable. If not, give up and throw error



55
56
57
58
59
60
61
62
# File 'lib/downer/download_manager.rb', line 55

def check_directory
  if File.writable?(@target_directory)
    return true
  else
    #FileUtils.mkdir(@target_directory)
    raise WriteFailed
  end
end

Tells us what worked and what failed



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/downer/download_manager.rb', line 37

def print_session_summary(worker)
  @output.puts "Session complete."
  @output.puts "\n\n"
  @output.puts "----------------------------------"
  @output.puts "SUMMARY:"
  @output.puts "Successful downloads: (#{worker.successful_downloads.size})"
  @output.puts "Failed downloads: (#{worker.failed_downloads.size}): "
  worker.failed_downloads.each do |fail|
    @output.puts "  * #{fail}"
  end
end

#startObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/downer/download_manager.rb', line 22

def start
  check_directory
  
  if @strategy && @strategy.source_valid?
    urls = @strategy.get_urls
    @output.puts "Starting download on #{urls.size} files"
    worker = DownloadWorker.new(urls, @target_directory, @output)
    @downloaded_files = worker.start
    print_session_summary(worker)
  else
    @output.puts "Could not open url source #{@url_source}"
  end
end