Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/dropbox/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ def list_folder(path)
resp['entries'].map { |e| parse_tagged_response(e) }
end

# Get all files (recursively) from a folder
#
# @param [String] path
# @return [Array<Dropbox::Metadata>]
def list_all_files(path)
resp = request('/files/list_folder', path: path, recursive: true)
has_more = resp['has_more']
cursor = resp['cursor']
entries = resp['entries']
while has_more && !cursor.nil?
resp = request('/files/list_folder/continue', cursor: cursor)
has_more = resp['has_more']
cursor = resp['cursor']
entries += resp['entries']
end
entries.map { |e| parse_tagged_response(e) if e['.tag'] == 'file' }.compact
end

# Get the contents of a folder that are after a cursor.
#
# @param [String] cursor
Expand Down
33 changes: 33 additions & 0 deletions test/stubs/recursive_folder_contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HTTP/1.1 200 OK
Content-Type: application/json

{
"entries": [
{
".tag": "folder",
"name": "math",
"id": "id:a4ayc_80_OEAAAAAAAAAXz",
"path_lower": "/homework/math",
"path_display": "/Homework/math",
"sharing_info": {
"read_only": false,
"parent_shared_folder_id": "84528192421",
"traverse_only": false,
"no_access": false
},
"property_groups": [
{
"template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
"fields": [
{
"name": "Security Policy",
"value": "Confidential"
}
]
}
]
}
],
"cursor": "PART1_CURSOR",
"has_more": true
}
37 changes: 37 additions & 0 deletions test/stubs/recursive_folder_contents_part_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
HTTP/1.1 200 OK
Content-Type: application/json

{
"entries": [
{
".tag": "file",
"name": "Prime_Numbers.txt",
"id": "id:a4ayc_80_OEAAAAAAAAAXw",
"client_modified": "2015-05-12T15:50:38Z",
"server_modified": "2015-05-12T15:50:38Z",
"rev": "a1c10ce0dd78",
"size": 7212,
"path_lower": "/homework/math/prime_numbers.txt",
"path_display": "/Homework/math/Prime_Numbers.txt",
"sharing_info": {
"read_only": true,
"parent_shared_folder_id": "84528192421",
"modified_by": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc"
},
"property_groups": [
{
"template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
"fields": [
{
"name": "Security Policy",
"value": "Confidential"
}
]
}
],
"has_explicit_shared_members": false
}
],
"cursor": "PART2_CURSOR",
"has_more": true
}
37 changes: 37 additions & 0 deletions test/stubs/recursive_folder_contents_part_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
HTTP/1.1 200 OK
Content-Type: application/json

{
"entries": [
{
".tag": "file",
"name": "Irrational_Numbers.txt",
"id": "id:a4ayc_80_OEAAAAAAAAAXZ",
"client_modified": "2015-05-12T15:50:38Z",
"server_modified": "2015-05-12T15:50:38Z",
"rev": "a1c10ce0dd79",
"size": 7200,
"path_lower": "/homework/math/irrational_numbers.txt",
"path_display": "/Homework/math/Irrational_Numbers.txt",
"sharing_info": {
"read_only": true,
"parent_shared_folder_id": "84528192421",
"modified_by": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngd"
},
"property_groups": [
{
"template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
"fields": [
{
"name": "Security Policy",
"value": "Confidential"
}
]
}
],
"has_explicit_shared_members": false
}
],
"cursor": "ZZZZZ_EHj3x7PMkVuFIhwKYXEpwpLwyxp9vMKomUhllil9q7eWiAB",
"has_more": false
}
21 changes: 21 additions & 0 deletions test/test_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ def test_list_folder
assert_equal 'math', entries[1].name
end

def test_list_all_files
stub_request(:post, url('files/list_folder'))
.with(body: { recursive: true, path: '/Homework/math' } )
.to_return(stub('recursive_folder_contents'))
stub_request(:post, url('files/list_folder/continue'))
.with(body: { cursor: "PART1_CURSOR" })
.to_return(stub('recursive_folder_contents_part_2'))
stub_request(:post, url('files/list_folder/continue'))
.with(body: { cursor: "PART2_CURSOR" })
.to_return(stub('recursive_folder_contents_part_3'))

entries = @client.list_all_files('/Homework/math')

assert_equal 2, entries.length
assert entries[0].is_a?(Dropbox::FileMetadata)
assert_equal 'Prime_Numbers.txt', entries[0].name
assert_equal 7212, entries[0].size
assert_equal 'Irrational_Numbers.txt', entries[1].name
assert_equal 7200, entries[1].size
end

def test_list_folder_empty
stub_request(:post, url('files/list_folder')).to_return(stub('empty_folder_contents'))
entries = @client.list_folder('/empty_folder')
Expand Down