Skip to content

Commit d86eec2

Browse files
committed
Add async result; refactor Task param handling
1 parent 5bb10b1 commit d86eec2

File tree

5 files changed

+97
-25
lines changed

5 files changed

+97
-25
lines changed

lib/convert_api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'convert_api/errors'
66
require 'convert_api/result'
77
require 'convert_api/result_file'
8+
require 'convert_api/async_result'
89
require 'convert_api/upload_io'
910
require 'convert_api/file_param'
1011
require 'convert_api/format_detector'

lib/convert_api/async_result.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ConvertApi
2+
class AsyncResult
3+
attr_reader :response
4+
5+
def initialize(response)
6+
@response = response
7+
end
8+
9+
def job_id
10+
response['JobId']
11+
end
12+
end
13+
end

lib/convert_api/task.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,38 @@ class Task
33
def initialize(from_format, to_format, params, conversion_timeout: nil)
44
@from_format = from_format
55
@to_format = to_format
6-
@params = params
76
@conversion_timeout = conversion_timeout || config.conversion_timeout
8-
end
9-
10-
def run
11-
params = normalize_params(@params).merge(
7+
@params = normalize_params(params).merge(
128
Timeout: @conversion_timeout,
139
StoreFile: true,
1410
)
11+
@async = @params.delete(:Async)
12+
end
1513

14+
def run
1615
read_timeout = @conversion_timeout + config.conversion_timeout_delta if @conversion_timeout
1716

1817
response = ConvertApi.client.post(
19-
request_path(params),
20-
params,
18+
request_path,
19+
@params,
2120
read_timeout: read_timeout,
2221
)
2322

23+
return AsyncResult.new(response) if async?
24+
2425
Result.new(response)
2526
end
2627

2728
private
2829

29-
def request_path(params)
30-
from_format = @from_format || detect_format(params)
31-
converter = params[:converter] ? "/converter/#{params[:converter]}" : ''
32-
async = params[:Async] ? 'async/' : ''
30+
def async?
31+
@async.to_s.downcase == 'true'
32+
end
33+
34+
def request_path
35+
from_format = @from_format || detect_format
36+
converter = @params[:converter] ? "/converter/#{@params[:converter]}" : ''
37+
async = async? ? 'async/' : ''
3338

3439
"#{async}convert/#{from_format}/to/#{@to_format}#{converter}"
3540
end
@@ -67,10 +72,10 @@ def files_batch(values)
6772
files
6873
end
6974

70-
def detect_format(params)
71-
return DEFAULT_URL_FORMAT if params[:Url]
75+
def detect_format
76+
return DEFAULT_URL_FORMAT if @params[:Url]
7277

73-
resource = params[:File] || Array(params[:Files]).first
78+
resource = @params[:File] || Array(@params[:Files]).first
7479

7580
FormatDetector.new(resource, @to_format).run
7681
end

spec/convert_api/task_spec.rb

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,51 @@
99

1010
let(:result) { double }
1111

12-
it 'executes task and returns result' do
13-
expect(ConvertApi.client).to(
14-
receive(:post).with('convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result)
15-
)
12+
shared_examples 'successful task' do
13+
it 'executes task and returns result' do
14+
expect(ConvertApi.client).to(
15+
receive(:post).with('convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result)
16+
)
1617

17-
expect(subject).to be_instance_of(ConvertApi::Result)
18+
expect(subject).to be_instance_of(ConvertApi::Result)
19+
end
1820
end
1921

22+
it_behaves_like 'successful task'
23+
2024
describe 'async' do
21-
let(:params) { { Async: true, File: 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' } }
25+
shared_examples 'successful async task' do
26+
it 'submits an async task and returns result' do
27+
expect(ConvertApi.client).to(
28+
receive(:post).with('async/convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result)
29+
)
2230

23-
it 'submits an async task and returns result' do
24-
expect(ConvertApi.client).to(
25-
receive(:post).with('async/convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result)
26-
)
31+
expect(subject).to be_instance_of(ConvertApi::AsyncResult)
32+
end
33+
end
2734

28-
expect(subject).to be_instance_of(ConvertApi::Result)
35+
context 'Async: false' do
36+
let(:params) { { Async: false, File: 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' } }
37+
38+
it_behaves_like 'successful task'
39+
end
40+
41+
context 'Async: "false"' do
42+
let(:params) { { Async: 'false', File: 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' } }
43+
44+
it_behaves_like 'successful task'
45+
end
46+
47+
context 'Async: true' do
48+
let(:params) { { Async: true, File: 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' } }
49+
50+
it_behaves_like 'successful async task'
51+
end
52+
53+
context 'Async: "true"' do
54+
let(:params) { { Async: "true", File: 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' } }
55+
56+
it_behaves_like 'successful async task'
2957
end
3058
end
3159
end

spec/convert_api_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@
112112
expect { subject }.to raise_error(ConvertApi::FormatError)
113113
end
114114
end
115+
116+
context 'async' do
117+
shared_examples 'successful async conversion' do
118+
it 'returns result' do
119+
expect(subject).to be_instance_of(ConvertApi::AsyncResult)
120+
expect(subject.job_id).to be_a_kind_of(String)
121+
end
122+
end
123+
124+
context 'with web resource' do
125+
let(:from_format) { 'web' }
126+
let(:params) { {Async: true, Url: 'http://convertapi.com' } }
127+
128+
it_behaves_like 'successful async conversion'
129+
end
130+
131+
context 'with multiple files' do
132+
let(:to_format) { 'zip' }
133+
let(:params) { { Async: true, Files: [file1, file2] } }
134+
let(:file1) { 'examples/files/test.pdf' }
135+
let(:file2) { ConvertApi::UploadIO.new('examples/files/test.pdf', 'test2.pdf') }
136+
137+
it_behaves_like 'successful async conversion'
138+
end
139+
end
115140
end
116141

117142
describe '.user' do

0 commit comments

Comments
 (0)