Class: Dropbox::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dropbox/client.rb

Overview

Client contains all the methods that map to the Dropbox API endpoints.

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Client

Initialize a new client.

Parameters:

  • access_token (String)


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.

Parameters:

  • from_path (String)
  • to_path (String)

Returns:



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.

Parameters:

  • path (String)

Returns:



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.

Parameters:

  • path (String)

Returns:



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.

Parameters:

  • path (String)

Returns:



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.

Parameters:

  • account_id (String)

Returns:



61
62
63
64
# File 'lib/dropbox/client.rb', line 61

def ()
  resp = request('/users/get_account', account_id: )
  parse_tagged_response(resp, 'basic_account')
end

#get_account_batch(account_ids) ⇒ Array<Dropbox::BasicAccount>

Get information about multiple user accounts.

Parameters:

  • account_ids (Array<String>)

Returns:



70
71
72
73
# File 'lib/dropbox/client.rb', line 70

def ()
  resp = request('/users/get_account_batch', account_ids: ids)
  resp.map { |a| parse_tagged_response(a, 'basic_account') }
end

#get_current_accountDropbox::FullAccount

Get information about the current user’s account.



78
79
80
81
# File 'lib/dropbox/client.rb', line 78

def 
  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.

Parameters:

  • path (String)

Returns:



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.

Parameters:

  • path (String)

Returns:



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_usageDropbox::SpaceUsage

Get the space usage information for the current user’s account.

Returns:



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 a temporary link to stream content of a file.

Parameters:

  • path (String)

Returns:



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.

Parameters:

  • path (String)
  • format (String) (defaults to: 'jpeg')
  • size (String) (defaults to: 'w64h64')

Returns:



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.

Parameters:

  • path (String)

Returns:



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.

Parameters:

  • path (String)

Returns:



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.

Parameters:

  • from_path (String)
  • to_path (String)

Returns:



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.

Parameters:

  • path (String)
  • rev (String)

Returns:



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_tokenvoid

This method returns an undefined value.

Disable the access token used to authenticate the call.

Raises:



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.

Parameters:

  • path (String)
  • url (String)

Returns:

  • (String)

    the job id, if the processing is asynchronous.

  • (Dropbox::FileMetadata)

    if the processing is synchronous.



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.

Parameters:

  • query (String)
  • path (String) (defaults to: '')
  • max_results (Integer) (defaults to: 100)

Returns:



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.

Parameters:

  • path (String)
  • body (String, Enumerable)
  • mode (String) (defaults to: 'add')
  • autorename (Boolean) (defaults to: false)
  • client_modified (String, Time) (defaults to: nil)
  • mute (Boolean) (defaults to: false)

Returns:



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