Skip to content

Commit

Permalink
Add copy and move methods
Browse files Browse the repository at this point in the history
  • Loading branch information
waits committed Jun 13, 2016
1 parent bc78cc3 commit 279fecd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
10 changes: 10 additions & 0 deletions lib/dropbox/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def initialize(access_token)
@access_token = access_token
end

def copy(from, to)
resp = request('/copy', from_path: from, to_path: to)
object_from_response(resp)
end

def create_folder(path)
resp = request('/create_folder', path: path)
FolderMetadata.new(resp['id'], resp['path_lower'])
Expand All @@ -26,6 +31,11 @@ def list_folder(path)
resp['entries'].map { |e| object_from_response(e) }
end

def move(from, to)
resp = request('/move', from_path: from, to_path: to)
object_from_response(resp)
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 Down
54 changes: 38 additions & 16 deletions test/test_client.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
require 'minitest/autorun'
require 'dropbox'

class DropboxTest < Minitest::Test
class DropboxClientTest < Minitest::Test
def setup
@client = Dropbox::Client.new(ENV['DROPBOX_SDK_ACCESS_TOKEN'])
@nonce = Time.now.to_i.to_s
end

def test_class
assert Dropbox::Client.is_a?(Class), 'Dropbox::Client is not a Class'
end

def test_client_initialize
assert @client.is_a?(Dropbox::Client), 'Dropbox::Client did not initialize'
end
Expand All @@ -27,27 +23,35 @@ def test_client_initialize_error

def test_invalid_access_token
dbx = Dropbox::Client.new('12345678' * 8)

assert_raises(Dropbox::APIError) do
dbx.list_folder('/somedir')
end
end

def test_folder_initialize
folder = Dropbox::FolderMetadata.new('id:123', '/parent/middle/child')
assert_equal 'child', folder.name
def test_copy
path = '/copied_folder'
folder = @client.copy('/folder_to_copy', path)

assert_equal 'copied_folder', folder.name

@client.delete(path)
end

def test_file_initialize
file = Dropbox::FileMetadata.new('id:123', '/folder/file', 11)
assert_equal 'file', file.name
assert_equal 11, file.size
def test_copy_error
assert_raises(Dropbox::APIError) do
@client.copy('/folder_to_copy', '/folder_to_copy')
end
end

def test_create_folder
path = '/dropbox_ruby_sdk_test_dir_' + @nonce
path = '/temp_dir'
folder = @client.create_folder(path)

assert folder.is_a?(Dropbox::FolderMetadata)
assert_equal path, folder.path

@client.delete(path)
end

def test_create_folder_error
Expand All @@ -60,6 +64,7 @@ def test_delete_folder
path = '/folder_to_delete'
@client.create_folder(path)
folder = @client.delete(path)

assert folder.is_a?(Dropbox::FolderMetadata)
assert_equal path[1..-1], folder.name
end
Expand All @@ -72,6 +77,7 @@ def test_delete_folder_error

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

assert entries[0].is_a?(Dropbox::FolderMetadata)
assert_equal 'subfolder', entries[0].name
assert entries[1].is_a?(Dropbox::FileMetadata)
Expand All @@ -85,11 +91,27 @@ def test_list_folder_error
end
end

def test_move
from, to = '/folder_to_move', '/moved_folder'
@client.create_folder(from)
folder = @client.move(from, to)

assert_equal 'moved_folder', folder.name

@client.delete(to)
end

def test_move_error
assert_raises(Dropbox::APIError) do
@client.move('/does_not_exist', '/does_not_exist')
end
end

def test_search
matches = @client.search('folder')
assert_equal 2, matches.length
assert matches[1].is_a?(Dropbox::FolderMetadata)
assert_equal 'folder_to_search', matches[1].name
assert_equal 3, matches.length
assert matches[2].is_a?(Dropbox::FolderMetadata)
assert_equal 'folder_to_search', matches[2].name

matches = @client.search('sub', '/folder_to_search')
assert_equal 2, matches.length
Expand Down
15 changes: 15 additions & 0 deletions test/test_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'minitest/autorun'
require 'dropbox'

class DropboxMetadataTest < Minitest::Test
def test_folder_initialize
folder = Dropbox::FolderMetadata.new('id:123', '/parent/middle/child')
assert_equal 'child', folder.name
end

def test_file_initialize
file = Dropbox::FileMetadata.new('id:123', '/folder/file', 11)
assert_equal 'file', file.name
assert_equal 11, file.size
end
end

0 comments on commit 279fecd

Please sign in to comment.