-
-
Save travisdmathis/66b3895120f1c9c7a7f6 to your computer and use it in GitHub Desktop.
Recursive function to build a tree of folders based on API JSON data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_tree(folder) | |
items = folder.items | |
items.each do |item| | |
if item.type == "folder" | |
puts item.parent | |
end | |
end | |
end | |
def create_box_folder_tree | |
@tree = [] | |
root_folders = self.box_client.root_folder.folders | |
root_folders.each do |folder| | |
self.get_tree(folder) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def export | |
current_user.update_box_tokens | |
box_folders = BoxFolder.new(:access_token => current_user.oauth2_users.find_by_provider('box_oauth2').access_token) | |
box_folders.create_box_folder_tree | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "folder", | |
"id": "11446498", | |
"sequence_id": "1", | |
"etag": "1", | |
"name": "Pictures", | |
"created_at": "2012-12-12T10:53:43-08:00", | |
"modified_at": "2012-12-12T11:15:04-08:00", | |
"description": "Some pictures I took", | |
"size": 629644, | |
"path_collection": { | |
"total_count": 1, | |
"entries": [ | |
{ | |
"type": "folder", | |
"id": "0", | |
"sequence_id": null, | |
"etag": null, | |
"name": "All Files" | |
} | |
] | |
}, | |
"created_by": { | |
"type": "user", | |
"id": "17738362", | |
"name": "sean rose", | |
"login": "sean@box.com" | |
}, | |
"modified_by": { | |
"type": "user", | |
"id": "17738362", | |
"name": "sean rose", | |
"login": "sean@box.com" | |
}, | |
"owned_by": { | |
"type": "user", | |
"id": "17738362", | |
"name": "sean rose", | |
"login": "sean@box.com" | |
}, | |
"shared_link": { | |
"url": "https://www.box.com/s/vspke7y05sb214wjokpk", | |
"download_url": null, | |
"vanity_url": null, | |
"is_password_enabled": false, | |
"unshared_at": null, | |
"download_count": 0, | |
"preview_count": 0, | |
"access": "open", | |
"permissions": { | |
"can_download": true, | |
"can_preview": true | |
} | |
}, | |
"folder_upload_email": { | |
"access": "open", | |
"email": "upload.Picture.k13sdz1@u.box.com" | |
}, | |
"parent": { | |
"type": "folder", | |
"id": "0", | |
"sequence_id": null, | |
"etag": null, | |
"name": "All Files" | |
}, | |
"item_status": "active", | |
"item_collection": { | |
"total_count": 1, | |
"entries": [ | |
{ | |
"type": "file", | |
"id": "5000948880", | |
"sequence_id": "3", | |
"etag": "3", | |
"sha1": "134b65991ed521fcfe4724b7d814ab8ded5185dc", | |
"name": "tigers.jpeg" | |
} | |
], | |
"offset": 0, | |
"limit": 100 | |
}, | |
"tags": [ | |
"approved", | |
"ready to publish" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
current_user.update_box_tokens | |
box_session = RubyBox::Session.new({ | |
client_id: BOX_KEYS[:client_id], | |
client_secret: BOX_KEYS[:client_secret], | |
access_token: current_user.oauth2_users.find_by_provider('box_oauth2').access_token | |
}) | |
box_client = RubyBox::Client.new(box_session) | |
folders = box_client.root_folder.folders | |
@tree = [] | |
folders.each do |folder| | |
children = [] | |
folder.items.each do |child| | |
if child.type == "folder" | |
children << { | |
title: child.name, | |
key: child.id, | |
isFolder: true | |
} | |
end | |
end | |
@tree << { | |
title: folder.name, | |
key: folder.id, | |
isFolder: true, | |
children: children | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment