Class: Worochi::Agent::Dropbox
- Inherits:
-
Worochi::Agent
- Object
- Worochi::Agent
- Worochi::Agent::Dropbox
- 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
Instance Method Summary collapse
-
#delete(path) ⇒ Boolean
Deletes the file at ‘path` from Dropbox.
-
#init_client ⇒ DropboxClient
Initializes Dropbox SDK client.
-
#list(path = nil) ⇒ Array<Hash>
Returns a list of files and subdirectories at the remote path specified by ‘options`.
-
#push_item(item) ⇒ Boolean
Push a single Item to Dropbox.
-
#push_item_chunked(item) ⇒ nil
Uses the ‘/chunked_upload` endpoint to push large files to Dropbox.
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.
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_client ⇒ DropboxClient
Initializes Dropbox SDK client. Refer to official Dropbox documentation.
12 13 14 15 |
# File 'lib/worochi/agent/dropbox.rb', line 12 def init_client @client = DropboxClient.new([:token]) @client end |
#list(path = nil) ⇒ Array<Hash>
Returns a list of files and subdirectories at the remote path specified by ‘options`.
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.
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 > [:chunk_size] push_item_chunked(item) else path = full_path(item.path) @client.put_file(path, item.content, [: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.
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([: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), [:overwrite]) nil end |