Skip to content

Commit

Permalink
Add save_url method
Browse files Browse the repository at this point in the history
  • Loading branch information
waits committed Jun 14, 2016
1 parent e1a4b4d commit 45ffcd3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/dropbox/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']) }
Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/dropbox/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45ffcd3

Please sign in to comment.