class Download::Object

Attributes

options[RW]
path[RW]
url[RW]

Public Class Methods

new(hash={}) click to toggle source
# File lib/download.rb, line 10
def initialize(hash={})
  set_multi(hash)
  raise(ArgumentError, 'url is required') unless url
end

Public Instance Methods

file_path() click to toggle source

return a string with a file path where the file will be saved

# File lib/download.rb, line 20
def file_path

  self.path= File.join(Dir.pwd, uri_file_name) unless path
  if File.directory?(path)
    self.path= File.join(self.path, uri_file_name)
  end

  self.path

end
start(hash={}) click to toggle source

start the downloading process

# File lib/download.rb, line 32
def start(hash={})

  set_multi(hash)

  File.delete(file_path) if File.exist?(file_path)
  File.open(file_path, 'wb') do |file_obj|
    Kernel.open(*[url,options].compact) do |fin|
      while (buf = fin.read(8192))
        file_obj << buf
      end
    end
  end

  return file_path

end
uri_file_name() click to toggle source
# File lib/download.rb, line 15
def uri_file_name
  @uri_file_name ||= URI.parse(url).path.to_s.split('/').last
end

Protected Instance Methods

set_multi(hash={}) click to toggle source
# File lib/download.rb, line 51
def set_multi(hash={})
  raise(ArgumentError, 'input must be hash!') unless hash.is_a?(Hash)
  hash.each_pair { |key, value| self.public_send("#{key}=", value) unless value.nil? }
end