wetransfer 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +0,0 @@
1
- module WeTransfer
2
- VERSION = '0.1.0'.freeze
3
- end
data/spec/client_spec.rb DELETED
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe WeTransfer::Client do
4
- describe 'Client#new' do
5
- it 'returns a error when no api_key is given' do
6
- expect {
7
- described_class.new
8
- }.to raise_error(ArgumentError, /missing keyword: api_key/)
9
- end
10
-
11
- it 'on initialization a active connection object is created' do
12
- client = described_class.new(api_key: 'api-key')
13
- expect(client.api_connection).to be_kind_of WeTransfer::Connection
14
- end
15
- end
16
-
17
- describe 'Client#create_transfer' do
18
- let(:client) { described_class.new(api_key: 'api-key') }
19
-
20
- it 'create transfer should return a transfer object' do
21
- transfer = client.create_transfer
22
- expect(transfer.shortened_url).to start_with('http://we.tl/s-')
23
- expect(transfer).to be_kind_of WeTransfer::Transfer
24
- end
25
-
26
- it 'when no name/description is send, a default name/description is generated' do
27
- transfer = client.create_transfer
28
- expect(transfer.name).to eq("File Transfer: #{Time.now.strftime('%d-%m-%Y')}")
29
- expect(transfer.description).to eq('Transfer generated with WeTransfer Ruby SDK')
30
- end
31
-
32
- it 'when a name/description is send, transfer has that name/description' do
33
- transfer = client.create_transfer(
34
- name: 'WeTransfer Test Transfer',
35
- description: "Moving along… Good news, everyone! I've
36
- taught the toaster to feel love! Humans dating robots is
37
- sick. You people wonder why I'm still single? It's 'cause
38
- all the fine robot sisters are dating humans!")
39
- expect(transfer.name).to eq('WeTransfer Test Transfer')
40
- expect(transfer.description).to start_with('Moving along… Good news, everyone!')
41
- end
42
-
43
- it 'when no items are send, a itemless transfer is created' do
44
- transfer = client.create_transfer
45
- expect(transfer.items).to be_empty
46
- end
47
-
48
- it 'when items are sended, the transfer has items' do
49
- transfer = client.create_transfer(items: ["#{__dir__}/fixtures/war-and-peace.txt"])
50
- expect(transfer).to be_kind_of WeTransfer::Transfer
51
- expect(transfer.items.count).to eq(1)
52
- end
53
-
54
- it 'returns an error when items are not sended inside an array' do
55
- expect {
56
- client.create_transfer(items: "#{__dir__}/war-end-peace.txt")
57
- }.to raise_error(StandardError, 'The items field must be an array')
58
- end
59
-
60
- it 'completes a item after item upload' do
61
- transfer = client.create_transfer(items: ["#{__dir__}/fixtures/war-and-peace.txt"])
62
- expect(transfer).to be_kind_of WeTransfer::Transfer
63
- end
64
- end
65
-
66
- describe 'Client#add_item' do
67
- let(:client) { described_class.new(api_key: 'api-key') }
68
-
69
- it 'add items to an already created transfer' do
70
- transfer = client.create_transfer
71
- expect(transfer.items.count).to eq(0)
72
- transfer = client.add_items(transfer: transfer, items: ["#{__dir__}/fixtures/war-and-peace.txt"])
73
- expect(transfer.items.count).to eq(1)
74
- end
75
-
76
- it 'raises an error when no transfer is being send to add_items_to_transfer method' do
77
- expect {
78
- client.add_items(items: ["#{__dir__}/fixtures/war-and-peace.txt"])
79
- }.to raise_error(ArgumentError, 'missing keyword: transfer')
80
- end
81
- end
82
- end
@@ -1,74 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe WeTransfer::Connection do
4
- describe 'Connection#new' do
5
- it 'creates a new connection when the client is passed' do
6
- client = OpenStruct.new(api_key: 'api-key-12345')
7
- connection = described_class.new(client: client)
8
- expect(connection.api_connection).to be_kind_of Faraday::Connection
9
- end
10
-
11
- it 'contains the api_key inside the connection' do
12
- client = OpenStruct.new(api_key: 'api-key-12345')
13
- connection = described_class.new(client: client)
14
- expect(connection.api_key).to eq(client.api_key)
15
- end
16
-
17
- it 'correctly handles the connection path variable' do
18
- ENV['WT_API_CONNECTION_PATH'] = '/this_path_does_not_exist'
19
- client = OpenStruct.new(api_key: 'api-key-12345')
20
- connection = described_class.new(client: client)
21
- expect(connection.api_path).to eq('/this_path_does_not_exist')
22
- ENV['WT_API_CONNECTION_PATH'] = '/v1'
23
- connection = described_class.new(client: client)
24
- expect(connection.api_path).to eq('/v1')
25
- end
26
- end
27
-
28
- describe 'Connection#post_request' do
29
- it 'returns with a response body when a post request is made' do
30
- client = OpenStruct.new(api_key: 'api-key-12345')
31
- connection = described_class.new(client: client)
32
- response = connection.post_request(path: '/authorize')
33
- expect(response['status']).to eq('success')
34
- end
35
-
36
- it 'returns with a response body when a post request is made' do
37
- client = OpenStruct.new(api_key: 'api-key-12345')
38
- connection = described_class.new(client: client)
39
- response = connection.post_request(path: '/transfers', body: {name: 'test_transfer', description: 'this is a test transfer', items: []})
40
- expect(response['shortened_url']).to start_with('http://we.tl/s-')
41
- expect(response['name']).to eq('test_transfer')
42
- expect(response['description']).to eq('this is a test transfer')
43
- expect(response['items'].count).to eq(0)
44
- end
45
-
46
- it 'returns with a ApiRequestError when request is forbidden' do
47
- client = OpenStruct.new(api_key: 'api-key-12345')
48
- connection = described_class.new(client: client)
49
- expect {
50
- connection.post_request(path: '/forbidden')
51
- }.to raise_error(WeTransfer::Connection::ApiRequestError, 'Forbidden')
52
- end
53
- end
54
-
55
- describe 'Connection#get_request' do
56
- it 'returns with a response body when a get request is made for upload urls' do
57
- client = OpenStruct.new(api_key: 'api-key-12345')
58
- connection = described_class.new(client: client)
59
- response = connection.get_request(path: '/files/1337/uploads/1/7331')
60
- expect(response['upload_url']).to include('upload')
61
- expect(response['part_number']).to eq(1)
62
- expect(response['upload_id']).to_not be_nil
63
- expect(response['upload_expires_at']).to_not be_nil
64
- end
65
-
66
- it 'returns with a ApiRequestError when request is forbidden' do
67
- client = OpenStruct.new(api_key: 'api-key-12345')
68
- connection = described_class.new(client: client)
69
- expect {
70
- connection.get_request(path: '/forbidden')
71
- }.to raise_error(WeTransfer::Connection::ApiRequestError, 'Forbidden')
72
- end
73
- end
74
- end
@@ -1,104 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe WeTransfer::ItemBuilder do
4
- describe 'ItemBuilder process' do
5
- let(:item_builder) { described_class.new }
6
-
7
- it 'makes a new Item if no item is given on initialze' do
8
- expect(item_builder).to be_an_instance_of(WeTransfer::ItemBuilder)
9
- expect(item_builder.item).to be_an_instance_of(WeTransfer::Item)
10
- end
11
-
12
- it 'uses the item, when a item is given to the ItemBuilder' do
13
- builder_item = described_class.new(item: item_builder.item)
14
- expect(builder_item).to be_an_instance_of(WeTransfer::ItemBuilder)
15
- expect(builder_item.item).to be_an_instance_of(WeTransfer::Item)
16
- end
17
-
18
- it 'sets the path when path method is called' do
19
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
20
- expect(item_builder.item.path).to eq("#{__dir__}/fixtures/war-and-peace.txt")
21
- end
22
-
23
- it 'sets content always to file' do
24
- item_builder.content_identifier
25
- expect(item_builder.item.content_identifier).to eq('file')
26
- end
27
-
28
- it 'sets the local local_identifier to the first 36 characters of the file name' do
29
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
30
- item_builder.local_identifier
31
- expect(item_builder.item.local_identifier).to eq('war-and-peace.txt')
32
- expect(item_builder.item.local_identifier.length).to be <= 36
33
- end
34
-
35
- it 'sets the local local_identifier to the first 36 characters of the file name' do
36
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace-and-peace-and-war-and-war-and-peace.txt")
37
- item_builder.local_identifier
38
- expect(item_builder.item.local_identifier).to eq('war-and-peace-and-peace-and-war-and-')
39
- expect(item_builder.item.local_identifier.length).to be <= 36
40
- end
41
-
42
- it 'sets the item name according to the given path' do
43
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
44
- item_builder.name
45
- expect(item_builder.item.name).to eq('war-and-peace.txt')
46
- end
47
-
48
- it 'sets the filesize by reading the file' do
49
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
50
- item_builder.size
51
- expect(item_builder.item.size).to_not be_nil
52
- expect(item_builder.item.size).to eq(485192)
53
- end
54
-
55
- it 'sets the id according to the api response' do
56
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
57
- item_builder.id(item: item_builder.item, id: 1234)
58
- expect(item_builder.item.id).to eq(1234)
59
- end
60
-
61
- it 'sets the upload url according to the api response' do
62
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
63
- item_builder.upload_url(item: item_builder.item, url: "#{ENV.fetch('WT_API_URL')}/upload/#{SecureRandom.hex(9)}")
64
- expect(item_builder.item.upload_url).to_not be_nil
65
- end
66
-
67
- it 'sets the multipart_parts according to the api response' do
68
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
69
- item_builder.multipart_parts(item: item_builder.item, part_count: 3)
70
- expect(item_builder.item.multipart_parts).to eq(3)
71
- end
72
-
73
- it 'sets the multipart_id according to the api response' do
74
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
75
- item_builder.multipart_id(item: item_builder.item, multi_id: 1234567890)
76
- expect(item_builder.item.multipart_id).to eq(1234567890)
77
- end
78
-
79
- it 'sets the upload_id according to the api response' do
80
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
81
- item_builder.upload_id(item: item_builder.item, upload_id: 1234567890)
82
- expect(item_builder.item.upload_id).to eq(1234567890)
83
- end
84
-
85
- it 'returns the item object when ItemBuilder item method is calles' do
86
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
87
- expect(item_builder.item).to be_an_instance_of(WeTransfer::Item)
88
- end
89
-
90
- it 'validates the file if it exists on the path given' do
91
- expect {
92
- item_builder.path(path: "#{__dir__}/fixtures/war-and-peace.txt")
93
- item_builder.validate_file
94
- }.to_not raise_error
95
- end
96
-
97
- it 'validates the file if it exists on the path given and returns with FileDoesNotExistError' do
98
- expect {
99
- item_builder.path(path: "#{__dir__}/peace-and-war.txt")
100
- item_builder.validate_file
101
- }.to raise_error(WeTransfer::ItemBuilder::FileDoesNotExistError, "#{item_builder.item} does not exist")
102
- end
103
- end
104
- end
data/spec/spec_helper.rb DELETED
@@ -1,24 +0,0 @@
1
- require 'simplecov'
2
- SimpleCov.start do
3
- add_filter '/spec/'
4
- end
5
-
6
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
7
- $LOAD_PATH.unshift(File.dirname(__FILE__))
8
-
9
- require 'wetransfer'
10
- require 'pry'
11
- require 'rspec'
12
- require_relative 'test_server'
13
-
14
- RSpec.configure do |config|
15
- config.before :suite do
16
- TestServer.start(nil)
17
- ENV['WT_API_URL'] = 'http://localhost:9001'
18
- ENV['WT_API_CONNECTION_PATH'] = '/v1'
19
- end
20
-
21
- config.expect_with :rspec do |c|
22
- c.syntax = :expect
23
- end
24
- end
data/spec/test_server.rb DELETED
@@ -1,208 +0,0 @@
1
- require 'webrick'
2
- require 'securerandom'
3
- include WEBrick
4
-
5
- class ForbiddenServlet < HTTPServlet::AbstractServlet
6
- def do_GET(_req, res)
7
- res['Content-Type'] = 'application/json'
8
- res.status = 403
9
- end
10
- def do_POST(_req, res)
11
- res['Content-Type'] = 'application/json'
12
- res.status = 403
13
- end
14
- end
15
-
16
- class AuthServlet < HTTPServlet::AbstractServlet
17
- def do_POST(req, res)
18
- if req.header["x-api-key"].empty?
19
- res['Content-Type'] = 'application/json'
20
- res.status = 401
21
- else
22
- res['Content-Type'] = 'application/json'
23
- res.status = 200
24
- res.body = {status: 'success', token: SecureRandom.hex(4)}.to_json
25
- end
26
- end
27
- end
28
-
29
- class TransfersServlet < HTTPServlet::AbstractServlet
30
- def self.do_POST(req, res)
31
- content = JSON.parse(req.body)
32
- items_count = content["items"].count || 0
33
- res['Content-Type'] = 'application/json'
34
- res.status = 200
35
- res.body = {shortened_url: "http://we.tl/s-#{SecureRandom.hex(5)}",
36
- id: SecureRandom.hex(9),
37
- name: content["name"],
38
- description: content["description"],
39
- size: totalsize_calc(items: content["items"]),
40
- total_items: content["items"].count,
41
- version_identifier: nil,
42
- state: 'uploading',
43
- items: item_params(items: content["items"])}.to_json
44
- end
45
-
46
- private
47
-
48
- def self.item_params(items:)
49
- items_array = []
50
- items.each do |item|
51
- items_array << {id: SecureRandom.hex(9),
52
- local_identifier: item['filename'][0..36],
53
- name: item['filename'],
54
- size: item['filesize'],
55
- meta:{
56
- multipart_parts: multipart_calc(item: item),
57
- multipart_id: SecureRandom.hex(9),
58
- },
59
- upload_url:"#{ENV.fetch('WT_API_URL')}/upload/#{SecureRandom.hex(9)}",
60
- upload_id: SecureRandom.hex(9),
61
- upload_expires_at: (Time.now + 5).to_i
62
- }
63
- end
64
- return items_array
65
- end
66
-
67
- def self.totalsize_calc(items:)
68
- total_size = []
69
- items.each do |item|
70
- total_size << item['filesize']
71
- end
72
- total_size.reduce(0, :+)
73
- end
74
-
75
- def self.multipart_calc(item:)
76
- parts = item['filesize'] / 6291456
77
- parts == 0 ? 1 : parts
78
- end
79
- end
80
-
81
- class UploadUrlServlet < HTTPServlet::AbstractServlet
82
- def self.do_GET(_req, res)
83
- part_number = res.request_uri.to_s.split('/')[-2]
84
- res['Content-Type'] = 'application/json'
85
- res.status = 200
86
- res.body = { upload_url: "#{ENV.fetch('WT_API_URL')}/upload/#{SecureRandom.hex(9)}",
87
- part_number: part_number.to_i,
88
- upload_id: SecureRandom.hex(9),
89
- upload_expires_at: (Time.now + 5).to_i
90
- }.to_json
91
- end
92
- end
93
-
94
- class UploadPartServlet < HTTPServlet::AbstractServlet
95
- def self.do_PUT(_req, res)
96
- res['Content-Type'] = 'application/json'
97
- res.status = 200
98
- end
99
- end
100
-
101
- class TransferItemServlet < HTTPServlet::AbstractServlet
102
- # this servlet is used for add_items_to_transfer functionality
103
- def self.do_POST(req, res)
104
- content = JSON.parse(req.body)
105
- res['Content-Type'] = 'application/json'
106
- res.status = 200
107
- res.body = item_params(items: content["items"]).to_json
108
- end
109
-
110
- def self.item_params(items:)
111
- items_array = []
112
- items.each do |item|
113
- items_array << {id: SecureRandom.hex(9),
114
- local_identifier: item['filename'][0..36],
115
- name: item['filename'],
116
- size: item['filesize'],
117
- meta:{
118
- multipart_parts: multipart_calc(item: item),
119
- multipart_id: SecureRandom.hex(9),
120
- },
121
- upload_url:"#{ENV.fetch('WT_API_URL')}/upload/#{SecureRandom.hex(9)}",
122
- upload_id: SecureRandom.hex(9),
123
- upload_expires_at: (Time.now + 5).to_i
124
- }
125
- end
126
- return items_array
127
- end
128
- def self.multipart_calc(item:)
129
- parts = item['filesize'] / 6291456
130
- parts == 0 ? 1 : parts
131
- end
132
- end
133
-
134
- class UploadServlet < HTTPServlet::AbstractServlet
135
- def do_PUT(req, res)
136
- res['Content-Type'] = 'application/json'
137
- res.status = 200
138
- end
139
- end
140
-
141
- class CompleteItemServlet < HTTPServlet::AbstractServlet
142
- def self.do_POST(_req, res)
143
- res['Content-Type'] = 'application/json'
144
- res.status = 202
145
- res.body = {ok: true, message: 'File is marked as complete.'}.to_json
146
- end
147
- end
148
-
149
- class TestServer
150
- def self.start(log_file = nil, port = 9001)
151
- new(log_file, port).start
152
- end
153
-
154
- def initialize(log_file = nil, port = 9001)
155
- log_file ||= StringIO.new
156
- log = WEBrick::BasicLog.new(log_file, WEBrick::BasicLog::WARN)
157
-
158
- options = {
159
- Port: port,
160
- Logger: log,
161
- AccessLog: [
162
- [log, WEBrick::AccessLog::COMMON_LOG_FORMAT],
163
- [log, WEBrick::AccessLog::REFERER_LOG_FORMAT]
164
- ],
165
- DocumentRoot: File.expand_path(__dir__),
166
- }
167
-
168
- @server = WEBrick::HTTPServer.new(options)
169
- @server.mount('/v1/forbidden', ForbiddenServlet)
170
- @server.mount('/v1/authorize', AuthServlet)
171
- @server.mount_proc('/v1/transfers') do |req, res|
172
- if req.path =~ /^(?=.*\bv1\b)(?=.*\btransfers\b)(?=.*\bitems\b).+/
173
- TransferItemServlet.do_POST(req, res)
174
- else
175
- TransfersServlet.do_POST(req, res)
176
- end
177
- end
178
- @server.mount_proc('/v1/files/') do |req, res|
179
- if req.request_method == "PUT"
180
- UploadPartServlet.do_PUT(req, res)
181
- elsif req.request_method == "GET"
182
- UploadUrlServlet.do_GET(req, res)
183
- else
184
- CompleteItemServlet.do_POST(req,res)
185
- end
186
- end
187
- @server.mount('/upload', UploadServlet)
188
- end
189
-
190
- def start
191
- trap('INT') {
192
- begin
193
- @server.shutdown unless @server.nil?
194
- rescue Object => e
195
- warn "Error #{__FILE__}:#{__LINE__}\n#{e.message}"
196
- end
197
- }
198
-
199
- @thread = Thread.new { @server.start }
200
- Thread.pass
201
- self
202
- end
203
-
204
- def join
205
- @thread.join if defined? @thread and @thread
206
- self
207
- end
208
- end