Skip to content

Commit

Permalink
Add get_metadata, get_temporary_link, and list_revisions methods
Browse files Browse the repository at this point in the history
  • Loading branch information
waits committed Jun 14, 2016
1 parent 279fecd commit d5ea9b2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
20 changes: 18 additions & 2 deletions lib/dropbox/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,27 @@ def delete(path)
object_from_response(resp)
end

def get_metadata(path)
resp = request('/get_metadata', path: path)
object_from_response(resp)
end

def get_temporary_link(path)
resp = request('/get_temporary_link', path: path)
return object_from_response(resp['metadata'], 'file'), resp['link']
end

def list_folder(path)
resp = request('/list_folder', path: path)
resp['entries'].map { |e| object_from_response(e) }
end

def list_revisions(path)
resp = request('/list_revisions', path: path)
entries = resp['entries'].map { |e| object_from_response(e, 'file') }
return entries, resp['is_deleted']
end

def move(from, to)
resp = request('/move', from_path: from, to_path: to)
object_from_response(resp)
Expand All @@ -42,8 +58,8 @@ def search(query, path='', max=100)
end

private
def object_from_response(resp)
case resp['.tag']
def object_from_response(resp, tag=resp['.tag'])
case tag
when 'file'
FileMetadata.new(resp['id'], resp['path_lower'], resp['size'])
when 'folder'
Expand Down
44 changes: 43 additions & 1 deletion test/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ def test_delete_folder_error
end
end

def test_get_metadata
file = @client.get_metadata('/file.txt')

assert file.is_a?(Dropbox::FileMetadata)
assert_equal 'file.txt', file.name
assert_equal 22, file.size
end

def test_get_metadata_error
assert_raises(Dropbox::APIError) do
@client.get_metadata('/does_not_exist')
end
end

def test_get_temporary_link
file, link = @client.get_temporary_link('/file.txt')

assert file.is_a?(Dropbox::FileMetadata)
assert_equal 'file.txt', file.name
assert_equal 22, file.size
assert_match 'https://dl.dropboxusercontent.com/apitl/1', link
end

def test_get_temporary_link_error
assert_raises(Dropbox::APIError) do
@client.get_temporary_link('/folder_to_search')
end
end

def test_list_folder
entries = @client.list_folder('/folder_to_list')

Expand All @@ -87,7 +116,20 @@ def test_list_folder

def test_list_folder_error
assert_raises(Dropbox::APIError) do
@client.list_folder('/doesnotexist')
@client.list_folder('/file.txt')
end
end

def test_list_revisions
entries, is_deleted = @client.list_revisions('/file.txt')

assert_equal 1, entries.length
assert_equal false, is_deleted
end

def test_list_revisions_error
assert_raises(Dropbox::APIError) do
@client.list_revisions('/folder_to_search')
end
end

Expand Down

0 comments on commit d5ea9b2

Please sign in to comment.