diff --git a/lib/cloud_convert/process.rb b/lib/cloud_convert/process.rb index 8bf7daa..cd246ca 100644 --- a/lib/cloud_convert/process.rb +++ b/lib/cloud_convert/process.rb @@ -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) @@ -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 @@ -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 @@ -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