class PopflashMatchDownloader::Downloader

Public Class Methods

new(download_directory) click to toggle source
# File lib/popflash_match_downloader/downloader.rb, line 5
def initialize download_directory
  @download_directory = File.expand_path(download_directory)
end

Public Instance Methods

download_file(name) click to toggle source
# File lib/popflash_match_downloader/downloader.rb, line 17
def download_file name
  file_location = '/' + name

  Net::HTTP.start('s3.popflash.site') do |http|
    http.request_get file_location do |response|
      while response.is_a? Net::HTTPRedirection
        location = response['location']
        puts "redirecting to #{location}"
        response = http.request_get location
      end

      if response.is_a? Net::HTTPSuccess
        File.open file_path(name), 'wb' do |f|
          response.read_body { |seg| f.write seg }
        end
      else
        puts "couldn't donwload demo. error: #{response.message}"
      end
    end
  end
end
download_file_if_needed(name) click to toggle source
# File lib/popflash_match_downloader/downloader.rb, line 39
def download_file_if_needed name
  if is_file_downloaded? name
    puts "already downloaded #{name}"
    return
  end

  puts "downloading #{name}"
  download_file name
end
file_path(name) click to toggle source
# File lib/popflash_match_downloader/downloader.rb, line 9
def file_path name
  File.join @download_directory, name
end
is_file_downloaded?(name) click to toggle source
# File lib/popflash_match_downloader/downloader.rb, line 13
def is_file_downloaded? name
  File.exist? file_path(name)
end