From d5ea9b25e8768c9c0e797abad8661bfe454d9c54 Mon Sep 17 00:00:00 2001 From: Dylan Waits Date: Mon, 13 Jun 2016 17:13:28 -0700 Subject: [PATCH] Add `get_metadata`, `get_temporary_link`, and `list_revisions` methods --- lib/dropbox/client.rb | 20 ++++++++++++++++++-- test/test_client.rb | 44 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/lib/dropbox/client.rb b/lib/dropbox/client.rb index f6a4cee..640bc8b 100644 --- a/lib/dropbox/client.rb +++ b/lib/dropbox/client.rb @@ -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) @@ -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' diff --git a/test/test_client.rb b/test/test_client.rb index 893fbd9..31ed2af 100644 --- a/test/test_client.rb +++ b/test/test_client.rb @@ -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') @@ -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