Class: Jaxx::Download

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Download

Returns a new instance of Download.



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

def initialize(args = {})
  @process = Process.new(args)
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#processObject (readonly)

Returns the value of attribute process.



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

def process
  @process
end

Instance Method Details

#executeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jaxx/download.rb', line 25

def execute
  process.start do |storage|
    directory = storage.directories.get(process.bucket)

    files(directory).each do |target, source|

      # Ensure directory exists
      dir = File.dirname(target)
      FileUtils.mkdir_p(dir) unless File.exist?(dir)

      # Create file
      File.open(target, 'wb') do |file|
        directory.files.get(source) {|chunk, byt_remain, byt_total| file.write(chunk) }
      end

    end
  end
end

#files(directory) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jaxx/download.rb', line 13

def files(directory)
  if process.file.match(%r{/$})
    directory.files.inject({}) do |hsh, f|
      key = f.key.gsub(process.file, '')
      hsh[key] = f.key if !key.empty? and f.key.match(process.file)
      hsh
    end
  else
    { File.basename(process.file) => process.file }
  end
end