Class: Download::Object
- Inherits:
-
Object
- Object
- Download::Object
- Defined in:
- lib/download.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#file_path ⇒ Object
return a string with a file path where the file will be saved.
-
#initialize(hash = {}) ⇒ Object
constructor
A new instance of Object.
-
#start(hash = {}) ⇒ Object
start the downloading process.
- #uri_file_name ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Object
Returns a new instance of Object.
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
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/download.rb', line 8 def @options end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/download.rb', line 8 def path @path end |
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/download.rb', line 8 def url @url end |
Instance Method Details
#file_path ⇒ Object
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,].compact) do |fin| while (buf = fin.read(8192)) file_obj << buf end end end return file_path end |
#uri_file_name ⇒ Object
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 |