Skip to content

Commit 8e5f374

Browse files
committed
Add support for async conversions
- Bump Ruby version to latest 2.6 patch - Request path method extraction with async prefix support - Add async result class - Refactor Task param handling
1 parent 88ed026 commit 8e5f374

File tree

6 files changed

+122
-21
lines changed

6 files changed

+122
-21
lines changed

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.6.3
1+
2.6.10

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: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,45 @@ 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+
@converter = detect_converter
13+
end
1514

16-
from_format = @from_format || detect_format(params)
15+
attr_reader :converter
16+
17+
def run
1718
read_timeout = @conversion_timeout + config.conversion_timeout_delta if @conversion_timeout
18-
converter = detect_converter(params)
19-
converter_path = converter ? "/converter/#{converter}" : ''
2019

2120
response = ConvertApi.client.post(
22-
"convert/#{from_format}/to/#{@to_format}#{converter_path}",
23-
params,
21+
request_path,
22+
@params,
2423
read_timeout: read_timeout,
2524
)
2625

26+
return AsyncResult.new(response) if async?
27+
2728
Result.new(response)
2829
end
2930

3031
private
3132

33+
def async?
34+
@async.to_s.downcase == 'true'
35+
end
36+
37+
def request_path
38+
from_format = @from_format || detect_format
39+
converter_path = converter ? "/converter/#{converter}" : ''
40+
async = async? ? 'async/' : ''
41+
42+
"#{async}convert/#{from_format}/to/#{@to_format}#{converter_path}"
43+
end
44+
3245
def normalize_params(params)
3346
result = {}
3447

@@ -62,16 +75,16 @@ def files_batch(values)
6275
files
6376
end
6477

65-
def detect_format(params)
66-
return DEFAULT_URL_FORMAT if params[:Url]
78+
def detect_format
79+
return DEFAULT_URL_FORMAT if @params[:Url]
6780

68-
resource = params[:File] || Array(params[:Files]).first
81+
resource = @params[:File] || Array(@params[:Files]).first
6982

7083
FormatDetector.new(resource, @to_format).run
7184
end
7285

73-
def detect_converter(params)
74-
params.each do |key, value|
86+
def detect_converter
87+
@params.each do |key, value|
7588
return value if key.to_s.downcase == 'converter'
7689
end
7790

spec/convert_api/task_spec.rb

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
let(:file) { 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' }
99
let(:result) { double }
1010

11-
it 'executes task and returns result' do
12-
expect(ConvertApi.client).to(
13-
receive(:post).with('convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result)
14-
)
11+
shared_examples 'successful task' do
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+
)
1516

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

21+
it_behaves_like 'successful task'
22+
1923
context 'with converter' do
2024
let(:params) { { File: file, Converter: 'openoffice' } }
2125

@@ -53,5 +57,50 @@
5357

5458
expect(subject).to be_instance_of(ConvertApi::Result)
5559
end
60+
61+
it 'executes task and returns result' do
62+
expect(ConvertApi.client).to(
63+
receive(:post).with('convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result)
64+
)
65+
66+
expect(subject).to be_instance_of(ConvertApi::Result)
67+
end
68+
end
69+
70+
71+
describe 'async' do
72+
shared_examples 'successful async task' do
73+
it 'submits an async task and returns result' do
74+
expect(ConvertApi.client).to(
75+
receive(:post).with('async/convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result)
76+
)
77+
78+
expect(subject).to be_instance_of(ConvertApi::AsyncResult)
79+
end
80+
end
81+
82+
context 'Async: false' do
83+
let(:params) { { Async: false, File: file } }
84+
85+
it_behaves_like 'successful task'
86+
end
87+
88+
context 'Async: "false"' do
89+
let(:params) { { Async: 'false', File: file } }
90+
91+
it_behaves_like 'successful task'
92+
end
93+
94+
context 'Async: true' do
95+
let(:params) { { Async: true, File: file } }
96+
97+
it_behaves_like 'successful async task'
98+
end
99+
100+
context 'Async: "true"' do
101+
let(:params) { { Async: "true", File: file } }
102+
103+
it_behaves_like 'successful async task'
104+
end
56105
end
57106
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)