Class: Download::Object

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Object

Returns a new instance of Object.

Raises:

  • (ArgumentError)


10
11
12
13
# File 'lib/download.rb', line 10

def initialize(hash={})
  set_multi(hash)
  raise(ArgumentError, 'url is required') unless url
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#file_pathObject

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



20
21
22
23
24
25
26
27
28
29
# 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 = {}) ⇒ Object

start the downloading process



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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_nameObject



15
16
17
# File 'lib/download.rb', line 15

def uri_file_name
  @uri_file_name ||= URI.parse(url).path.to_s.split('/').last
end