Class: Dropbox::Client
- Inherits:
-
Object
- Object
- Dropbox::Client
- Defined in:
- lib/dropbox/client.rb
Overview
Client contains all the methods that map to the Dropbox API endpoints.
Instance Method Summary collapse
-
#copy(from_path, to_path) ⇒ Dropbox::Metadata
Copy a file or folder to a different location in the user’s Dropbox.
-
#create_folder(path) ⇒ Dropbox::FolderMetadata
Create a folder at a given path.
-
#delete(path) ⇒ Dropbox::Metadata
Delete the file or folder at a given path.
-
#download(path) ⇒ Dropbox::FileMetadata, HTTP::Response::Body
Download a file from a user’s Dropbox.
-
#get_account(account_id) ⇒ Dropbox::BasicAccount
Get information about a user’s account.
-
#get_account_batch(account_ids) ⇒ Array<Dropbox::BasicAccount>
Get information about multiple user accounts.
-
#get_current_account ⇒ Dropbox::FullAccount
Get information about the current user’s account.
-
#get_metadata(path) ⇒ Dropbox::Metadata
Get the metadata for a file or folder.
-
#get_preview(path) ⇒ Dropbox::FileMetadata, HTTP::Response::Body
Get a preview for a file.
-
#get_space_usage ⇒ Dropbox::SpaceUsage
Get the space usage information for the current user’s account.
-
#get_temporary_link(path) ⇒ Dropbox::FileMetadata, String
Get a temporary link to stream content of a file.
-
#get_thumbnail(path, format = 'jpeg', size = 'w64h64') ⇒ Dropbox::FileMetadata, HTTP::Response::Body
Get a thumbnail for an image.
-
#initialize(access_token) ⇒ Client
constructor
Initialize a new client.
-
#list_folder(path) ⇒ Array<Dropbox::Metadata]
Get the contents of a folder.
-
#list_revisions(path) ⇒ Array<Dropbox::FileMetadata>, Boolean
Get the revisions of a file.
-
#move(from_path, to_path) ⇒ Dropbox::Metadata
Move a file or folder to a different location in the user’s Dropbox.
-
#restore(path, rev) ⇒ Dropbox::FileMetadata
Restore a file to a specific revision.
-
#revoke_token ⇒ void
Disable the access token used to authenticate the call.
-
#save_url(path, url) ⇒ String, Dropbox::FileMetadata
Save a specified URL into a file in user’s Dropbox.
-
#search(query, path = '', max_results = 100) ⇒ Array<Dropbox::Metadata>
Search for files and folders.
-
#upload(path, body, mode = 'add', autorename = false, client_modified = nil, mute = false) ⇒ Dropbox::FileMetadata
Create a new file.
Constructor Details
#initialize(access_token) ⇒ Client
Initialize a new client.
11 12 13 14 15 16 17 |
# File 'lib/dropbox/client.rb', line 11 def initialize(access_token) unless access_token =~ /^[a-z0-9_-]{64}$/i raise ClientError.invalid_access_token end @access_token = access_token end |
Instance Method Details
#copy(from_path, to_path) ⇒ Dropbox::Metadata
Copy a file or folder to a different location in the user’s Dropbox.
24 25 26 27 |
# File 'lib/dropbox/client.rb', line 24 def copy(from_path, to_path) resp = request('/files/copy', from_path: from_path, to_path: to_path) parse_tagged_response(resp) end |
#create_folder(path) ⇒ Dropbox::FolderMetadata
Create a folder at a given path.
33 34 35 36 |
# File 'lib/dropbox/client.rb', line 33 def create_folder(path) resp = request('/files/create_folder', path: path) parse_tagged_response(resp, 'folder') end |
#delete(path) ⇒ Dropbox::Metadata
Delete the file or folder at a given path.
42 43 44 45 |
# File 'lib/dropbox/client.rb', line 42 def delete(path) resp = request('/files/delete', path: path) parse_tagged_response(resp) end |
#download(path) ⇒ Dropbox::FileMetadata, HTTP::Response::Body
Download a file from a user’s Dropbox.
52 53 54 55 |
# File 'lib/dropbox/client.rb', line 52 def download(path) resp, body = content_request('/files/download', path: path) return parse_tagged_response(resp, 'file'), body end |
#get_account(account_id) ⇒ Dropbox::BasicAccount
Get information about a user’s account.
61 62 63 64 |
# File 'lib/dropbox/client.rb', line 61 def get_account(account_id) resp = request('/users/get_account', account_id: account_id) parse_tagged_response(resp, 'basic_account') end |
#get_account_batch(account_ids) ⇒ Array<Dropbox::BasicAccount>
Get information about multiple user accounts.
70 71 72 73 |
# File 'lib/dropbox/client.rb', line 70 def get_account_batch(account_ids) resp = request('/users/get_account_batch', account_ids: ids) resp.map { |a| parse_tagged_response(a, 'basic_account') } end |
#get_current_account ⇒ Dropbox::FullAccount
Get information about the current user’s account.
78 79 80 81 |
# File 'lib/dropbox/client.rb', line 78 def get_current_account resp = request('/users/get_current_account') parse_tagged_response(resp, 'full_account') end |
#get_metadata(path) ⇒ Dropbox::Metadata
Get the metadata for a file or folder.
87 88 89 90 |
# File 'lib/dropbox/client.rb', line 87 def (path) resp = request('/files/get_metadata', path: path) parse_tagged_response(resp) end |
#get_preview(path) ⇒ Dropbox::FileMetadata, HTTP::Response::Body
Get a preview for a file.
97 98 99 100 |
# File 'lib/dropbox/client.rb', line 97 def get_preview(path) resp, body = content_request('/files/get_preview', path: path) return parse_tagged_response(resp, 'file'), body end |
#get_space_usage ⇒ Dropbox::SpaceUsage
Get the space usage information for the current user’s account.
105 106 107 108 |
# File 'lib/dropbox/client.rb', line 105 def get_space_usage resp = request('/users/get_space_usage') SpaceUsage.new(resp) end |
#get_temporary_link(path) ⇒ Dropbox::FileMetadata, String
Get a temporary link to stream content of a file.
115 116 117 118 |
# File 'lib/dropbox/client.rb', line 115 def get_temporary_link(path) resp = request('/files/get_temporary_link', path: path) return parse_tagged_response(resp['metadata'], 'file'), resp['link'] end |
#get_thumbnail(path, format = 'jpeg', size = 'w64h64') ⇒ Dropbox::FileMetadata, HTTP::Response::Body
Get a thumbnail for an image.
127 128 129 130 |
# File 'lib/dropbox/client.rb', line 127 def get_thumbnail(path, format='jpeg', size='w64h64') resp, body = content_request('/files/get_thumbnail', path: path, format: format, size: size) return parse_tagged_response(resp, 'file'), body end |
#list_folder(path) ⇒ Array<Dropbox::Metadata]
Get the contents of a folder.
136 137 138 139 |
# File 'lib/dropbox/client.rb', line 136 def list_folder(path) resp = request('/files/list_folder', path: path) resp['entries'].map { |e| parse_tagged_response(e) } end |
#list_revisions(path) ⇒ Array<Dropbox::FileMetadata>, Boolean
Get the revisions of a file.
146 147 148 149 150 |
# File 'lib/dropbox/client.rb', line 146 def list_revisions(path) resp = request('/files/list_revisions', path: path) entries = resp['entries'].map { |e| parse_tagged_response(e, 'file') } return entries, resp['is_deleted'] end |
#move(from_path, to_path) ⇒ Dropbox::Metadata
Move a file or folder to a different location in the user’s Dropbox.
157 158 159 160 |
# File 'lib/dropbox/client.rb', line 157 def move(from_path, to_path) resp = request('/files/move', from_path: from, to_path: to) parse_tagged_response(resp) end |
#restore(path, rev) ⇒ Dropbox::FileMetadata
Restore a file to a specific revision.
167 168 169 170 |
# File 'lib/dropbox/client.rb', line 167 def restore(path, rev) resp = request('/files/restore', path: path, rev: rev) parse_tagged_response(resp, 'file') end |
#revoke_token ⇒ void
This method returns an undefined value.
Disable the access token used to authenticate the call.
175 176 177 178 |
# File 'lib/dropbox/client.rb', line 175 def revoke_token r = HTTP.auth('Bearer ' + @access_token).post(API + '/auth/token/revoke') raise APIError.new(r) if r.code != 200 end |
#save_url(path, url) ⇒ String, Dropbox::FileMetadata
Save a specified URL into a file in user’s Dropbox.
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/dropbox/client.rb', line 186 def save_url(path, url) resp = request('/files/save_url', path: path, url: url) case resp['.tag'] when 'complete' parse_tagged_response(resp['complete'], 'file') when 'async_job_id' resp['async_job_id'] else raise ClientError.unknown_response_type(resp['.tag']) end end |
#search(query, path = '', max_results = 100) ⇒ Array<Dropbox::Metadata>
Search for files and folders.
204 205 206 207 |
# File 'lib/dropbox/client.rb', line 204 def search(query, path='', max_results=100) resp = request('/files/search', path: path, query: query, max_results: max) resp['matches'].map { |m| parse_tagged_response(m['metadata']) } end |
#upload(path, body, mode = 'add', autorename = false, client_modified = nil, mute = false) ⇒ Dropbox::FileMetadata
Create a new file.
218 219 220 221 222 223 |
# File 'lib/dropbox/client.rb', line 218 def upload(path, body, mode='add', autorename=false, client_modified=nil, mute=false) client_modified = client_modified.iso8601 if client_modified.is_a?(Time) resp = upload_request('/files/upload', body, path: path, mode: mode, autorename: autorename, client_modified: client_modified, mute: mute) parse_tagged_response(resp, 'file') end |