Skip to content

Instantly share code, notes, and snippets.

@travisdmathis
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travisdmathis/66b3895120f1c9c7a7f6 to your computer and use it in GitHub Desktop.
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
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
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
{
"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"
]
}
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