Skip to content

Commit

Permalink
Added ability to creates a request to download multiple files and fol…
Browse files Browse the repository at this point in the history
…ders as a zip file (#116)
  • Loading branch information
JewelSam authored Oct 31, 2022
1 parent 43b4e0c commit 98893d9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,15 @@ Boxr::WebhookValidator.new(

```

#### [Zip Downloads](https://developer.box.com/reference/resources/zip-download/)
```ruby
# Requests creation of zip archive
# Returns a download_url and a status_url that can be used to download the archive.
#
# Example: create_zip_download([{ id: '1', type: 'file' }, { id: '2', type: 'folder' }])
create_zip_download(items, download_file_name: nil)
```

## Contributing

1. Fork it ( https://github.com/cburnette/boxr/fork )
Expand Down
1 change: 1 addition & 0 deletions boxr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "awesome_print", "~> 1.8"
spec.add_development_dependency "lru_redux", "~> 0.8"
spec.add_development_dependency "parallel", "~> 1.0"
spec.add_development_dependency "rubyzip", "~> 2.3"

spec.add_runtime_dependency "httpclient", "~> 2.8"
spec.add_runtime_dependency "hashie", ">= 3.5", "< 6"
Expand Down
1 change: 1 addition & 0 deletions lib/boxr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'boxr/watermarking'
require 'boxr/webhooks'
require 'boxr/webhook_validator'
require 'boxr/zip_downloads'

class BoxrCollection < Array
def files
Expand Down
1 change: 1 addition & 0 deletions lib/boxr/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Client
EVENTS_URI = "#{API_URI}/events"
WEB_LINKS_URI = "#{API_URI}/web_links"
WEBHOOKS_URI = "#{API_URI}/webhooks"
ZIP_DOWNLOADS_URI = "#{API_URI}/zip_downloads"

DEFAULT_LIMIT = 100
FOLDER_ITEMS_LIMIT = 1000
Expand Down
14 changes: 14 additions & 0 deletions lib/boxr/zip_downloads.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Boxr
class Client
def create_zip_download(targets, download_file_name: nil)
attributes = {
download_file_name: download_file_name,
items: targets
}
zip_download, _response = post(ZIP_DOWNLOADS_URI, attributes, success_codes: [200, 202])
zip_download
end
end
end
37 changes: 37 additions & 0 deletions spec/boxr/zip_downloads_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'zip'

# rake spec SPEC_OPTS="-e \"invokes zip downloads operations"\"
describe 'zip downloads operations' do
it 'invokes zip downloads operations' do
test_file_path = "./spec/test_files/#{TEST_FILE_NAME}"
download_zip_path = "./spec/test_files/#{DOWNLOADED_ZIP_TEST_FILE_NAME}"
unzip_path = "./spec/test_files/#{DOWNLOADED_TEST_FILE_NAME}"

puts 'upload a file'
new_file = BOX_CLIENT.upload_file(test_file_path, @test_folder)
expect(new_file.name).to eq(TEST_FILE_NAME)

puts 'create zip downloads'
new_zip_download = BOX_CLIENT.create_zip_download(
[{ id: new_file.id, type: 'file' }],
download_file_name: 'test.zip'
)
expect(new_zip_download.download_url).to match(URI::regexp)
expect(new_zip_download.status_url).to match(URI::regexp)

# download and unzip
URI.open(new_zip_download.download_url) do |zip_content|
File.open(download_zip_path, 'w+') { |file| file.write(zip_content.read) }
end
Zip::File.open(download_zip_path) do |zip_file|
zip_file.each { |entry| entry.extract(unzip_path) }
end

expect(FileUtils.identical?(test_file_path, unzip_path)).to eq(true)
ensure
File.delete(unzip_path)
File.delete(download_zip_path)
end
end
2 changes: 2 additions & 0 deletions spec/boxr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
TEST_FILE_NAME_CUSTOM = 'test file custom.txt'
TEST_FILE_NAME_IO = 'test file io.txt'
DOWNLOADED_TEST_FILE_NAME = 'downloaded test file.txt'
ZIP_TEST_FILE_NAME = 'test.zip'
DOWNLOADED_ZIP_TEST_FILE_NAME = 'download_test.zip'
COMMENT_MESSAGE = 'this is a comment'
REPLY_MESSAGE = 'this is a comment reply'
CHANGED_COMMENT_MESSAGE = 'this comment has been changed'
Expand Down
Binary file added spec/test_files/test.zip
Binary file not shown.

0 comments on commit 98893d9

Please sign in to comment.