Skip to content

Commit 7e7ce60

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

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
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
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
receive(:post).with('async/convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result)
2626
)
2727

28-
expect(subject).to be_instance_of(ConvertApi::Result)
28+
expect(subject).to be_instance_of(ConvertApi::AsyncResult)
2929
end
3030
end
3131
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)