Skip to content

Commit 1ce7d06

Browse files
committed
Initialize response objects directly in methods when possible
1 parent 4a2be83 commit 1ce7d06

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

lib/dropbox/client.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def copy(from_path, to_path)
3232
# @return [Dropbox::FolderMetadata]
3333
def create_folder(path)
3434
resp = request('/files/create_folder', path: path)
35-
parse_tagged_response(resp, 'folder')
35+
FolderMetadata.new(resp)
3636
end
3737

3838
# Delete the file or folder at a given path.
@@ -51,7 +51,7 @@ def delete(path)
5151
# @return [HTTP::Response::Body] body
5252
def download(path)
5353
resp, body = content_request('/files/download', path: path)
54-
return parse_tagged_response(resp, 'file'), body
54+
return FileMetadata.new(resp), body
5555
end
5656

5757
# Get information about a user's account.
@@ -60,24 +60,24 @@ def download(path)
6060
# @return [Dropbox::BasicAccount]
6161
def get_account(account_id)
6262
resp = request('/users/get_account', account_id: account_id)
63-
parse_tagged_response(resp, 'basic_account')
63+
BasicAccount.new(resp)
6464
end
6565

6666
# Get information about multiple user accounts.
6767
#
6868
# @param [Array<String>] account_ids
6969
# @return [Array<Dropbox::BasicAccount>]
7070
def get_account_batch(account_ids)
71-
resp = request('/users/get_account_batch', account_ids: ids)
72-
resp.map { |a| parse_tagged_response(a, 'basic_account') }
71+
resp = request('/users/get_account_batch', account_ids: account_ids)
72+
resp.map { |a| BasicAccount.new(a) }
7373
end
7474

7575
# Get information about the current user's account.
7676
#
7777
# @return [Dropbox::FullAccount]
7878
def get_current_account
7979
resp = request('/users/get_current_account')
80-
parse_tagged_response(resp, 'full_account')
80+
FullAccount.new(resp)
8181
end
8282

8383
# Get the metadata for a file or folder.
@@ -96,7 +96,7 @@ def get_metadata(path)
9696
# @return [HTTP::Response::Body] body
9797
def get_preview(path)
9898
resp, body = content_request('/files/get_preview', path: path)
99-
return parse_tagged_response(resp, 'file'), body
99+
return FileMetadata.new(resp), body
100100
end
101101

102102
# Get the space usage information for the current user's account.
@@ -114,7 +114,7 @@ def get_space_usage
114114
# @return [String] link
115115
def get_temporary_link(path)
116116
resp = request('/files/get_temporary_link', path: path)
117-
return parse_tagged_response(resp['metadata'], 'file'), resp['link']
117+
return FileMetadata.new(resp['metadata']), resp['link']
118118
end
119119

120120
# Get a thumbnail for an image.
@@ -126,7 +126,7 @@ def get_temporary_link(path)
126126
# @return [HTTP::Response::Body] body
127127
def get_thumbnail(path, format='jpeg', size='w64h64')
128128
resp, body = content_request('/files/get_thumbnail', path: path, format: format, size: size)
129-
return parse_tagged_response(resp, 'file'), body
129+
return FileMetadata.new(resp), body
130130
end
131131

132132
# Get the contents of a folder.
@@ -145,7 +145,7 @@ def list_folder(path)
145145
# @return [Boolean] is_deleted
146146
def list_revisions(path)
147147
resp = request('/files/list_revisions', path: path)
148-
entries = resp['entries'].map { |e| parse_tagged_response(e, 'file') }
148+
entries = resp['entries'].map { |e| FileMetadata.new(e) }
149149
return entries, resp['is_deleted']
150150
end
151151

@@ -155,7 +155,7 @@ def list_revisions(path)
155155
# @param [String] to_path
156156
# @return [Dropbox::Metadata]
157157
def move(from_path, to_path)
158-
resp = request('/files/move', from_path: from, to_path: to)
158+
resp = request('/files/move', from_path: from_path, to_path: to_path)
159159
parse_tagged_response(resp)
160160
end
161161

@@ -166,7 +166,7 @@ def move(from_path, to_path)
166166
# @return [Dropbox::FileMetadata]
167167
def restore(path, rev)
168168
resp = request('/files/restore', path: path, rev: rev)
169-
parse_tagged_response(resp, 'file')
169+
FileMetadata.new(resp)
170170
end
171171

172172
# Disable the access token used to authenticate the call.
@@ -187,7 +187,7 @@ def save_url(path, url)
187187
resp = request('/files/save_url', path: path, url: url)
188188
case resp['.tag']
189189
when 'complete'
190-
parse_tagged_response(resp['complete'], 'file')
190+
FileMetadata.new(resp['complete'])
191191
when 'async_job_id'
192192
resp['async_job_id']
193193
else
@@ -202,7 +202,7 @@ def save_url(path, url)
202202
# @param [Integer] max_results
203203
# @return [Array<Dropbox::Metadata>] matches
204204
def search(query, path='', max_results=100)
205-
resp = request('/files/search', path: path, query: query, max_results: max)
205+
resp = request('/files/search', path: path, query: query, max_results: max_results)
206206
resp['matches'].map { |m| parse_tagged_response(m['metadata']) }
207207
end
208208

@@ -219,12 +219,12 @@ def upload(path, body, mode='add', autorename=false, client_modified=nil, mute=f
219219
client_modified = client_modified.iso8601 if client_modified.is_a?(Time)
220220
resp = upload_request('/files/upload', body, path: path, mode: mode,
221221
autorename: autorename, client_modified: client_modified, mute: mute)
222-
parse_tagged_response(resp, 'file')
222+
FileMetadata.new(resp)
223223
end
224224

225225
private
226-
def parse_tagged_response(resp, tag=resp['.tag'])
227-
case tag
226+
def parse_tagged_response(resp)
227+
case resp['.tag']
228228
when 'file'
229229
FileMetadata.new(resp)
230230
when 'folder'
@@ -236,7 +236,7 @@ def parse_tagged_response(resp, tag=resp['.tag'])
236236
when 'full_account'
237237
FullAccount.new(resp)
238238
else
239-
raise ClientError.unknown_response_type(tag)
239+
raise ClientError.unknown_response_type(resp['.tag'])
240240
end
241241
end
242242

test/test_files.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ def test_list_folder
135135
assert_equal 4, entries[1].size
136136
end
137137

138+
def test_list_folder_empty
139+
entries = @client.list_folder('/empty_folder')
140+
141+
assert_equal 0, entries.length
142+
end
143+
138144
def test_list_folder_error
139145
assert_raises(Dropbox::APIError) do
140146
@client.list_folder('/file.txt')
@@ -196,10 +202,10 @@ def test_save_url_error
196202
end
197203

198204
def test_search
199-
matches = @client.search('folder')
200-
assert_equal 3, matches.length
201-
assert matches[2].is_a?(Dropbox::FolderMetadata)
202-
assert_equal 'folder_to_search', matches[2].name
205+
matches = @client.search('empty')
206+
assert_equal 2, matches.length
207+
assert matches[0].is_a?(Dropbox::FolderMetadata)
208+
assert matches[1].is_a?(Dropbox::FileMetadata)
203209

204210
matches = @client.search('sub', '/folder_to_search')
205211
assert_equal 2, matches.length

0 commit comments

Comments
 (0)