-
Notifications
You must be signed in to change notification settings - Fork 5
Example File Upload
Edward Chen edited this page Sep 10, 2018
·
6 revisions
The sample code below assumes you have an Organization and an administrator. It starts by initializing the caplinked client and gives an example of pulling your user. Following that it uses the file upload flow. This includes creating a team, workspace and folder for the files to be uploaded. Finally it provides a One Time download load link to the files uploaded.
- Ruby 2.0 or greater
gem install caplinked-apirequire 'caplinked-api'
client = Caplinked::Client.new(api_host: 'sandbox.caplinked.com', api_scheme: 'https')
client.api_key = 'YOUR_PUBLIC_API_KEY'
client.api_secret_key = 'YOUR_SECRET_API_KEY'
client.api_user_token = 'USER_RESOURCE_IDENTIFIER_TOKEN'
response = client.get_user_info # response as ruby hash
response_body = response.to_json # response as json
team = client.create_team(
team: {
name: 'Powerpuff Girls',
allowed_workspaces: 1,
allowed_admins: 1,
drm_enabled: false,
watermarking: false,
suppress_email: false
}
)
# The creating user is automatically added to the team. Trying to add this user
# again will throw a Caplinked::Error
workspace = client.create_workspace(
team_id: team[:id], workspace: {name: "Mojo jojo's lab"}
)
folder = client.create_a_new_folder(
workspace_id: workspace[:id], parent_id: nil, name: 'Chemical X ingredients'
)
file = client.upload_file(
workspace_id: workspace[:id],
folder_id: folder[:id],
file_name: 'sugar_and_spice.jpg',
file: File.read('sugar_and_spice.jpg')
)
single_file = client.single_file_download(
file_id: file[:id], workspace_id: workspace[:id]
)
puts single_file[:expiring_url] # One time use of url to download file.
file = client.upload_file(
workspace_id: workspace[:id],
folder_id: folder[:id],
file_name: 'everything_nice.pdf',
file: File.read('everything_nice.pdf')
)
zip_file = client.create_zip_file(
workspace_id: workspace[:id], download: {folder_ids: [folder[:id]]}
)
# function to check if file is ready from downloads response
def file_ready? downloads, zip_file
downloads.each do |down|
return true if down[:id] == zip_file[:id] && down[:status] == 'done'
end
false
end
downloads = client.download_status(workspace_id: workspace[:id])[:downloads]
if file_ready? downloads, zip_file
begin
client.get_zip id: zip_file[:id], workspace_id: workspace[:id]
rescue Caplinked::Error => e
puts e.inspect
end
end
# File may take a few seconds to process
sleep 5
get_zip = client.get_zip id: zip_file[:id], workspace_id: workspace[:id]
puts get_zip[:expiring_url]