Skip to content

Commit 7155c1e

Browse files
authored
Merge pull request #1 from Interfolio/webhooks_and_polling
Add webhook support. Add polling support
2 parents 8e5f374 + e9e8ce1 commit 7155c1e

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

lib/convert_api.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ def convert(to_format, params, from_format: nil, conversion_timeout: nil)
2828
Task.new(from_format, to_format, params, conversion_timeout: conversion_timeout).run
2929
end
3030

31+
# Poll ConvertAPI for job status
32+
# Raises ClientError with status code 202 if the job is not complete yet
33+
# Raises ClientError with status code 404 if the job is not found
34+
def poll(job_id)
35+
Result.new(client.get("async/job/#{job_id}"))
36+
end
37+
3138
def user
3239
client.get('user')
3340
end

lib/convert_api/client.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class Client
2828
'Accept' => 'application/json'
2929
}
3030

31+
# Parameters that are always on the URL, even for POST
32+
POST_URL_PARAMS = [
33+
:WebHook,
34+
:JobId
35+
].freeze
36+
3137
def get(path, params = {}, options = {})
3238
handle_response do
3339
request = Net::HTTP::Get.new(request_uri(path, params), DEFAULT_HEADERS)
@@ -38,7 +44,7 @@ def get(path, params = {}, options = {})
3844

3945
def post(path, params, options = {})
4046
handle_response do
41-
request = Net::HTTP::Post.new(request_uri(path), DEFAULT_HEADERS)
47+
request = Net::HTTP::Post.new(request_uri(path, post_url_params(params)), DEFAULT_HEADERS)
4248
request.form_data = build_form_data(params)
4349

4450
http(options).request(request)
@@ -113,16 +119,22 @@ def build_form_data(params)
113119
data = {}
114120

115121
params.each do |key, value|
116-
if value.is_a?(Array)
117-
value.each_with_index { |v, i| data["#{key}[#{i}]"] = v }
118-
else
119-
data[key] = value
122+
unless POST_URL_PARAMS.include?(key)
123+
if value.is_a?(Array)
124+
value.each_with_index { |v, i| data["#{key}[#{i}]"] = v }
125+
else
126+
data[key] = value
127+
end
120128
end
121129
end
122130

123131
data
124132
end
125133

134+
def post_url_params(params)
135+
params.select { |k, v| POST_URL_PARAMS.include?(k) }
136+
end
137+
126138
def base_uri
127139
config.base_uri
128140
end

spec/convert_api/client_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,36 @@
1010
expect(subject['FileId']).to be_instance_of(String)
1111
end
1212
end
13+
14+
describe '#post' do
15+
let(:file) { 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' }
16+
let(:path) { 'convert/txt/to/pdf/converter/openoffice' }
17+
let(:options) { {} }
18+
let(:mock_response) { OpenStruct.new(code: 200, body: '{}') }
19+
20+
subject{ client.post(path, params, options) }
21+
22+
context 'with normal parameters' do
23+
let(:params) { { File: file } }
24+
let(:uri_with_secret) { "/#{path}?Secret=#{ConvertApi.config.api_secret}" }
25+
26+
it 'makes a post request with no extra URL parameters' do
27+
expect(Net::HTTP::Post).to(receive(:new).with(uri_with_secret, described_class::DEFAULT_HEADERS).and_call_original)
28+
expect_any_instance_of(Net::HTTP).to(receive(:request).and_return(mock_response))
29+
expect(subject).to be_an_instance_of(Hash)
30+
end
31+
end
32+
33+
context 'with parameters that MUST be passed via URL' do
34+
let(:webhook) { 'https://www.convertapi.com/fake-webhook' }
35+
let(:params) { { File: file, WebHook: webhook } }
36+
let(:uri_with_selected_params) { "/#{path}?#{URI.encode_www_form({ WebHook: webhook, Secret: ConvertApi.config.api_secret})}" }
37+
38+
it 'makes a post request that passes the required parameters via URL' do
39+
expect(Net::HTTP::Post).to(receive(:new).with(uri_with_selected_params, described_class::DEFAULT_HEADERS).and_call_original)
40+
expect_any_instance_of(Net::HTTP).to(receive(:request).and_return(mock_response))
41+
expect(subject).to be_an_instance_of(Hash)
42+
end
43+
end
44+
end
1345
end

0 commit comments

Comments
 (0)