Skip to content

Commit e199969

Browse files
author
Laurynas Butkus
committed
Add file upload
1 parent f535cd5 commit e199969

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

.rspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
--format documentation
21
--color
32
--require spec_helper

lib/convert_api/client.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ def post(path, params)
99
end
1010
end
1111

12+
def upload(io, filename)
13+
handle_exceptions do
14+
result = connection.post('upload', io.read) do |request|
15+
request.headers['Content-Type'] = 'application/octet-stream'
16+
request.headers['Content-Disposition'] = "attachment; filename='#{filename}'"
17+
end
18+
19+
result.body
20+
end
21+
end
22+
1223
private
1324

1425
def handle_exceptions
@@ -28,7 +39,6 @@ def connection
2839
builder.params['TimeOut'] = config.conversion_timeout
2940
builder.params['StoreFile'] = true
3041

31-
builder.request :multipart
3242
builder.request :url_encoded
3343

3444
builder.response :json, content_type: /\bjson$/

spec/convert_api/client_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
RSpec.describe ConvertApi::Client do
22
before { ConvertApi.config.api_secret = ENV['CONVERT_API_SECRET'] }
33

4+
let(:client) { described_class.new }
5+
46
describe '#post' do
5-
subject { described_class.new.post(path, params) }
7+
subject { client.post(path, params) }
68

79
let(:path) { 'txt/to/pdf' }
810
let(:params) { { File: file } }
11+
let(:file) { 'https://www.w3.org/TR/PNG/iso_8859-1.txt' }
912

10-
shared_examples 'converting file' do
11-
it 'sends request and returns result' do
12-
expect(subject['ConversionCost']).to be_instance_of(Integer)
13-
expect(subject['Files']).to be_instance_of(Array)
14-
end
13+
it 'sends request and returns result' do
14+
expect(subject['ConversionCost']).to be_instance_of(Integer)
15+
expect(subject['Files']).to be_instance_of(Array)
1516
end
17+
end
1618

17-
context 'with local file' do
18-
let(:file) { Faraday::UploadIO.new('LICENSE.txt', 'application/octet-stream') }
19+
describe '#upload' do
20+
subject { client.upload(io, File.basename(io.path)) }
1921

20-
it_behaves_like 'converting file'
21-
end
22+
let(:io) { File.open('LICENSE.txt') }
2223

23-
context 'with url as file' do
24-
let(:file) { 'https://www.w3.org/TR/PNG/iso_8859-1.txt' }
25-
it_behaves_like 'converting file'
24+
it 'uploads file and results result' do
25+
expect(subject['FileId']).to be_instance_of(String)
2626
end
2727
end
2828
end

spec/convert_api_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
end
55

66
it 'has configuration defaults' do
7-
expect(described_class.config.api_secret).to be_nil
87
expect(described_class.config.api_base_uri).not_to be_nil
98
expect(described_class.config.connect_timeout).not_to be_nil
109
expect(described_class.config.conversion_timeout).not_to be_nil

0 commit comments

Comments
 (0)