Sha256: 32ea44b100f4a2675c5718e891d46d21f361bc17653e1bb2eaf6cbb5894d6c2d

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module Downer
  
  class FailedDownload < StandardError
    attr_accessor :http_code, :url
  end
  
  class DownloadItem
    attr_reader :url
    attr_reader :content
    
    def initialize(url, destination)
      @url, @destination = sanitize_url(url), destination
      @uri = URI.parse(url)
    end
    
    # Returns the name which this file will be saved as
    def get_save_filename
      file_name = @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
    
    def download
      @http = Net::HTTP.new(@uri.host, @uri.port)
      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
    end
    
    private
    
      def write_to_file
        file_out_path = @destination + "/#{get_save_filename}"
        fout = File.new(file_out_path, 'w')
        fout.puts @content
      end
      
      def sanitize_url(unsafe_url)
        url = unsafe_url
        url.gsub!('[', '%5B')
        url.gsub!(']', '%5D')
        url.gsub!(',', '%2C')
        url
      end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
downer-0.2.2 lib/downer/download_item.rb
downer-0.1.1 lib/downer/download_item.rb