From 45ffcd3e96285dccbfc345ccf30db3beae05134f Mon Sep 17 00:00:00 2001 From: Dylan Waits Date: Mon, 13 Jun 2016 18:22:32 -0700 Subject: [PATCH] Add `save_url` method --- lib/dropbox/client.rb | 14 +++++++++++++- lib/dropbox/error.rb | 4 ++++ test/test_client.rb | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/dropbox/client.rb b/lib/dropbox/client.rb index c9a4ddf..12797ab 100644 --- a/lib/dropbox/client.rb +++ b/lib/dropbox/client.rb @@ -57,6 +57,18 @@ def restore(path, rev) object_from_response(resp, 'file') end + def save_url(path, url) + resp = request('/save_url', path: path, url: url) + case resp['.tag'] + when 'complete' + object_from_response(resp['complete'], 'file') + when 'async_job_id' + resp['async_job_id'] + else + raise ClientError.unknown_response_type + end + end + def search(query, path='', max=100) resp = request('/search', path: path, query: query, max_results: max) resp['matches'].map { |m| object_from_response(m['metadata']) } @@ -70,7 +82,7 @@ def object_from_response(resp, tag=resp['.tag']) when 'folder' FolderMetadata.new(resp['id'], resp['path_lower']) else - raise ClientError.new('Unknown response type') + raise ClientError.unknown_response_type end end diff --git a/lib/dropbox/error.rb b/lib/dropbox/error.rb index 9291a4a..2a73148 100644 --- a/lib/dropbox/error.rb +++ b/lib/dropbox/error.rb @@ -4,6 +4,10 @@ module Dropbox class ClientError < StandardError attr_reader :message + def self.unknown_response_type + self.new("Unknown response type '#{str}'") + end + def initialize(message=nil) @message = message end diff --git a/test/test_client.rb b/test/test_client.rb index 9762cb6..86cde0b 100644 --- a/test/test_client.rb +++ b/test/test_client.rb @@ -162,6 +162,19 @@ def test_restore_error end end + def test_save_url + job_id = @client.save_url('/saved_file.txt', 'https://www.dropbox.com/robots.txt') + + assert job_id.is_a?(String) + assert_match /^[a-z0-9_-]{22}$/i, job_id + end + + def test_save_url_error + assert_raises(Dropbox::APIError) do + @client.save_url('/saved_file.txt', 'ht:/invalid_url') + end + end + def test_search matches = @client.search('folder') assert_equal 3, matches.length