Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions lib/cloud_convert/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ def initialize(args = {})
@client = args[:client]
end

def create
def create mode = 'convert'
raise CloudConvert::InvalidStep unless @step == :awaiting_creation
url = construct_url("api", "process")
response = send_request(http_method: :post,
url: url,
params: {
"apikey" => @client.api_key,
"inputformat" => @input_format,
"outputformat" => @output_format
}) do | response|

params = { "apikey" => @client.api_key,
"inputformat" => @input_format,
"outputformat" => @output_format,
"mode" => mode }

if mode == 'info'
params.delete(:outputformat)
params.merge!(wait: true, download: false)
end

response = send_request(http_method: :post,
url: url,
params: params) do | response|
@step = :awaiting_conversion
response.parsed_response[:success] = true
create_parsed_response(:process_response, response.parsed_response)
Expand All @@ -44,8 +51,8 @@ def convert(opts)
raise CloudConvert::InvalidStep if @step == :awaiting_creation
url = process_url(include_process_id: true)
multi = opts[:file].respond_to?("read")
response = send_request(http_method: :post,
url: url,
response = send_request(http_method: :post,
url: url,
params: opts,
multi: multi) do |response|
response.parsed_response[:success] = true
Expand All @@ -66,13 +73,13 @@ def status
return convert_response response
end

def download(path, file_name="")
def download(path, file_name="")
raise CloudConvert::InvalidStep if @step == :awaiting_creation
response = HTTMultiParty.get(download_url(file_name))
return update_download_progress response unless response.response.code == "200"
file_name = response.response.header['content-disposition'][/filename=(\"?)(.+)\1/, 2] if file_name.strip.empty?
full_path = full_path(path, file_name)
return full_path.open("w") do |f|
return full_path.open("w") do |f|
f.binmode
f.write response.parsed_response
full_path.to_s
Expand All @@ -93,14 +100,14 @@ def download_url(file = "")
return "https://#{@process_response[:subdomain]}.cloudconvert.com/download/#{@process_response[:id]}#{file}"
end


private

def send_request(opts)
request = opts[:params] || {}
args = [opts[:http_method], opts[:url], {query: request, detect_mime_type: (true if opts[:multi])}]
response = CloudConvert::Client.send(*args)
yield(response) if block_given? and (response.response.code == "200" ||
yield(response) if block_given? and (response.response.code == "200" ||
(response.parsed_response.kind_of?(Hash) and response.parsed_response.key?("step")))
return response
end
Expand Down