From 3696d31948caa0ac00eca023ee1f9c39351b97e5 Mon Sep 17 00:00:00 2001 From: Dylan Waits Date: Mon, 20 Jun 2016 19:49:38 -0700 Subject: [PATCH] Add `get_copy_reference` and `save_copy_reference` methods --- lib/dropbox/client.rb | 21 +++++++++++++++++++++ test/test_files.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/dropbox/client.rb b/lib/dropbox/client.rb index dd7fae4..ca90044 100644 --- a/lib/dropbox/client.rb +++ b/lib/dropbox/client.rb @@ -72,6 +72,17 @@ def get_account_batch(account_ids) resp.map { |a| BasicAccount.new(a) } end + # Get a copy reference to a file or folder. + # + # @param [String] path + # @return [Dropbox::Metadata] metadata + # @return [String] copy_reference + def get_copy_reference(path) + resp = request('/files/copy_reference/get', path: path) + metadata = parse_tagged_response(resp['metadata']) + return metadata, resp['copy_reference'] + end + # Get information about the current user's account. # # @return [Dropbox::FullAccount] @@ -177,6 +188,16 @@ def revoke_token raise APIError.new(r) if r.code != 200 end + # Save a copy reference to the user's Dropbox. + # + # @param [String] copy_reference + # @param [String] path + # @return [Dropbox::Metadata] metadata + def save_copy_reference(copy_reference, path) + resp = request('/files/copy_reference/save', copy_reference: copy_reference, path: path) + parse_tagged_response(resp['metadata']) + end + # Save a specified URL into a file in user's Dropbox. # # @param [String] path diff --git a/test/test_files.rb b/test/test_files.rb index 80ae060..390551c 100644 --- a/test/test_files.rb +++ b/test/test_files.rb @@ -68,6 +68,28 @@ def test_download_error end end +# TODO: Getting 500 errors from the API. Mention it to Dropbox. +# +# def test_get_save_copy_reference +# meta, ref = @client.get_copy_reference('/file.txt') +# +# assert meta.is_a?(Dropbox::FileMetadata) +# assert ref.is_a?(String) +# assert_match /^[a-z0-9]+$/i, ref +# +# saved_meta = @client.save_copy_reference('/copied_file.txt') +# +# assert meta.is_a?(Dropbox::FileMetadata) +# assert_equal 'copied_file.txt', file.name +# assert_equal 22, file.size +# end + + def test_get_copy_reference_error + assert_raises(Dropbox::APIError) do + @client.get_copy_reference('/not_found') + end + end + def test_get_metadata file = @client.get_metadata('/file.txt') @@ -201,6 +223,12 @@ def test_save_url_error end end + def test_save_copy_reference_error + assert_raises(Dropbox::APIError) do + @client.save_copy_reference('invalid', '/saved_copy_reference.txt') + end + end + def test_search matches = @client.search('empty') assert_equal 2, matches.length