Class: Worochi::Agent::Dropbox

Inherits:
Worochi::Agent show all
Defined in:
lib/worochi/agent/dropbox.rb

Overview

The Worochi::Agent for Dropbox API. This wraps around the ‘dropbox-sdk` gem.

Instance Attribute Summary

Attributes inherited from Worochi::Agent

#options

Instance Method Summary collapse

Methods inherited from Worochi::Agent

#files, #files_and_folders, #folders, #initialize, #name, new, #push, #push_items, #remove, #set_dir, #set_options, #type

Constructor Details

This class inherits a constructor from Worochi::Agent

Instance Method Details

#delete(path) ⇒ Boolean

Deletes the file at ‘path` from Dropbox.

Parameters:

  • path (String)

    path relative to current directory

Returns:

  • (Boolean)

    ‘true` if a file was actually deleted



82
83
84
85
86
87
88
89
90
# File 'lib/worochi/agent/dropbox.rb', line 82

def delete(path)
  abs_path = full_path(path)
  begin
    @client.file_delete(abs_path)
  rescue DropboxError
    false
  end
  true
end

#init_clientDropboxClient

Initializes Dropbox SDK client. Refer to official Dropbox documentation.

Returns:

  • (DropboxClient)


12
13
14
15
# File 'lib/worochi/agent/dropbox.rb', line 12

def init_client
  @client = DropboxClient.new(options[:token])
  @client
end

#list(path = nil) ⇒ Array<Hash>

Returns a list of files and subdirectories at the remote path specified by ‘options`.

Parameters:

  • path (String) (defaults to: nil)

    path to list instead of the current directory

Returns:

  • (Array<Hash>)

    list of files and subdirectories



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/worochi/agent/dropbox.rb', line 38

def list(path=nil)
  remote_path = list_path(path)
  
  begin
    response = @client.(remote_path)
  rescue DropboxError
    raise Error, 'Invalid Dropbox folder specified'
  end

  response['contents'].map do |elem|
    {
      name: elem['path'].split('/').last,
      path: elem['path'],
      type: elem['is_dir'] ? 'folder' : 'file'
    }
  end
end

#push_item(item) ⇒ Boolean

Push a single Item to Dropbox.

Parameters:

Returns:

  • (Boolean)

    true if chunk uploader was used



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/worochi/agent/dropbox.rb', line 21

def push_item(item)
  Worochi::Log.debug "Uploading #{item.path} (#{item.size} bytes) to Dropbox..."
  if chunked = item.size > options[:chunk_size]
    push_item_chunked(item)
  else
    path = full_path(item.path)
    @client.put_file(path, item.content, options[:overwrite])
  end
  Worochi::Log.debug "Uploaded"
  chunked
end

#push_item_chunked(item) ⇒ nil

Uses the ‘/chunked_upload` endpoint to push large files to Dropbox. Refer to API documentation.

Parameters:

Returns:

  • (nil)

See Also:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/worochi/agent/dropbox.rb', line 63

def push_item_chunked(item)
  Worochi::Log.debug "Using chunk uploader..."
  uploader = @client.get_chunked_uploader(item.content, item.size)
  while uploader.offset < uploader.total_size
      begin
          uploader.upload(options[:chunk_size])
      rescue DropboxError
          raise Error, 'Dropbox chunk upload failed'
      end
      Worochi::Log.debug "Uploaded #{uploader.offset} bytes"
  end
  uploader.finish(full_path(item.path), options[:overwrite])
  nil
end