Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Commit e4b1e7b

Browse files
committed
Implement create_transfer_and_upload_files
1 parent 963ea55 commit e4b1e7b

File tree

4 files changed

+63
-21
lines changed

4 files changed

+63
-21
lines changed

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,26 @@ client = WeTransfer::Client.new(api_key: ENV.fetch('WT_API_KEY'))
7575
Now that you've got the client set up you can use `create_transfer_and_upload_files` to, well, create a transfer, and upload all files!
7676

7777
```ruby
78-
transfer = client.create_transfer_and_upload_files(message: 'All the Things') do |upload|
79-
upload.add_file_at(path: '/path/to/local/file.jpg')
80-
upload.add_file_at(path: '/path/to/another/local/file.jpg')
81-
upload.add_file(name: 'README.txt', io: StringIO.new("You should read All the Things!"))
78+
client.create_transfer_and_upload_files(message: 'All the Things') do |transfer|
79+
transfer.add_file(io: File.open('Gemfile'))
80+
transfer.add_file(
81+
name: 'hello_world.rb',
82+
io: File.open('path/to/different_name.rb')
83+
)
84+
transfer.add_file(
85+
name: 'README.txt',
86+
size: 31,
87+
io: StringIO.new("You should read All the Things!")
88+
)
8289
end
8390

91+
transfer = client.transfer
92+
8493
# To get a link to your transfer, call `url` on your transfer object:
85-
transfer.url => "https://we.tl/t-123234="
94+
transfer.url => "https://we.tl/t-1232346"
95+
96+
# Or inspect the whole transfer:
97+
puts transfer.to_json
8698
```
8799

88100
The upload will be performed at the end of the block. Depending on your file sizes and network connection speed, this might take some time.

lib/we_transfer/client.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# frozen_string_literal: true
2-
31
module WeTransfer
42
class Client
53
class Error < StandardError; end
@@ -29,10 +27,13 @@ def create_transfer(**args, &block)
2927
self
3028
end
3129

32-
def create_transfer_and_upload_files(message:, &block)
33-
transfer = WeTransfer::Transfer.new(args)
34-
transfer.persist(&block)
30+
def create_transfer_and_upload_files(**args, &block)
31+
create_transfer(args, &block)
32+
@transfer.upload_files
33+
@transfer.complete_files
34+
@transfer.finalize
3535

36+
self
3637
end
3738

3839
def find_transfer(transfer_id)

lib/we_transfer/transfer.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def add_file(**args)
4646
self
4747
end
4848

49-
def upload_file(name:, io: nil)
50-
file = find_file_by_name(name)
49+
def upload_file(name:, io: nil, file: nil)
50+
file ||= find_file_by_name(name)
5151
put_io = io || file.io
5252

5353
raise(
@@ -64,16 +64,35 @@ def upload_file(name:, io: nil)
6464
end
6565
end
6666

67+
#
68+
def upload_files
69+
files.each do |file|
70+
upload_file(
71+
name: file.name,
72+
file: file
73+
)
74+
end
75+
end
76+
6777
def upload_url_for_chunk(name:, chunk:)
6878
file_id = find_file_by_name(name).id
6979
Communication.upload_url_for_chunk(id, file_id, chunk)
7080
end
7181

72-
def complete_file(name:)
73-
file = find_file_by_name(name)
82+
def complete_file(name:, file: nil)
83+
file ||= find_file_by_name(name)
7484
Communication.complete_file(id, file.id, file.multipart.chunks)
7585
end
7686

87+
def complete_files
88+
files.each do |file|
89+
complete_file(
90+
name: file.name,
91+
file: file
92+
)
93+
end
94+
end
95+
7796
def finalize
7897
Communication.finalize_transfer(self)
7998
end
@@ -106,7 +125,5 @@ def find_file_by_name(name)
106125
raise FileMismatchError unless @found_files[name]
107126
@found_files[name]
108127
end
109-
110-
def_delegator self, :remote_transfer_params
111128
end
112129
end

spec/features/client_creates_a_transfer_spec.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
require "spec_helper"
22

33
describe "transfer integration" do
4-
around do |example|
4+
around(:each) do |example|
55
WebMock.allow_net_connect!
66
example.run
77
WebMock.disable_net_connect!
88
end
99

10-
it "Create a client, a transfer, it uploads files and finalizes the transfer" do
11-
client = WeTransfer::Client.new(api_key: ENV.fetch("WT_API_KEY"))
10+
let(:client) { WeTransfer::Client.new(api_key: ENV.fetch("WT_API_KEY")) }
11+
12+
it "Convenience method create_transfer_and_upload_files does it all!" do
13+
client.create_transfer_and_upload_files(message: "test transfer") do |transfer|
14+
transfer.add_file(name: "small_file_with_io", size: 10, io: StringIO.new("#" * 10))
15+
end
16+
transfer = client.transfer
1217

18+
expect(transfer.files.map(&:name))
19+
.to include("small_file_with_io")
20+
expect(transfer.state)
21+
.to eq "processing"
22+
expect(transfer.url)
23+
.to match %r|https://we.tl/t-|
24+
end
25+
26+
it "Create a client, a transfer, it uploads files and finalizes the transfer" do
1327
client.create_transfer(message: "test transfer") do |transfer|
1428
transfer.add_file(name: "small_file", size: 80)
1529
transfer.add_file(name: "small_file_with_io", size: 10, io: StringIO.new("#" * 10))
@@ -18,8 +32,6 @@
1832

1933
transfer = client.transfer
2034

21-
transfer.to_json
22-
2335
expect(transfer.url)
2436
.to be_nil
2537

0 commit comments

Comments
 (0)