Skip to content

Commit 758d77f

Browse files
author
David Ashman
committed
Add webhook support. Add polling support
1 parent 8e5f374 commit 758d77f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ 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+
].freeze
35+
3136
def get(path, params = {}, options = {})
3237
handle_response do
3338
request = Net::HTTP::Get.new(request_uri(path, params), DEFAULT_HEADERS)
@@ -38,7 +43,7 @@ def get(path, params = {}, options = {})
3843

3944
def post(path, params, options = {})
4045
handle_response do
41-
request = Net::HTTP::Post.new(request_uri(path), DEFAULT_HEADERS)
46+
request = Net::HTTP::Post.new(request_uri(path, post_url_params(params)), DEFAULT_HEADERS)
4247
request.form_data = build_form_data(params)
4348

4449
http(options).request(request)
@@ -113,16 +118,22 @@ def build_form_data(params)
113118
data = {}
114119

115120
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
121+
unless POST_URL_PARAMS.include?(key)
122+
if value.is_a?(Array)
123+
value.each_with_index { |v, i| data["#{key}[#{i}]"] = v }
124+
else
125+
data[key] = value
126+
end
120127
end
121128
end
122129

123130
data
124131
end
125132

133+
def post_url_params(params)
134+
params.select { |k, v| POST_URL_PARAMS.include?(k) }
135+
end
136+
126137
def base_uri
127138
config.base_uri
128139
end

0 commit comments

Comments
 (0)