class CloudConvert::Process

Attributes

client[R]
conversion_response[R]
download_url[R]
input_format[R]
output_format[R]
process_response[R]
status_response[R]
step[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/cloud_convert/process.rb, line 18
def initialize(args = {})
  @input_format = args[:input_format]
  @output_format = args[:output_format]
  @step = :awaiting_creation
  @client = args[:client]
end

Public Instance Methods

convert(opts) click to toggle source
# File lib/cloud_convert/process.rb, line 42
def convert(opts)
    raise CloudConvert::InvalidStep if @step == :awaiting_creation
    url = process_url()
    multi = opts[:file].respond_to?("read")
    response = send_request(http_method: :post, 
                            url: url, 
                            params: opts,
                            multi: multi) do |response|
        response.parsed_response[:success] = true
        create_parsed_response(:conversion_response, response.parsed_response)
        @step = @conversion_response[:step].to_sym
    end
    return convert_response response
end
create() click to toggle source
# File lib/cloud_convert/process.rb, line 25
def create
    raise CloudConvert::InvalidStep unless @step == :awaiting_creation
    url = construct_url("process")
    response = send_request(http_method: :post, 
                            url: url, 
                            params: {
                                "apikey" => @client.api_key,
                                "inputformat" => @input_format,
                                "outputformat" => @output_format
                            }) do | response|
        @step = :awaiting_conversion
        response.parsed_response[:success] = true
        create_parsed_response(:process_response, response.parsed_response)
    end
    return convert_response response
end
delete() click to toggle source
# File lib/cloud_convert/process.rb, line 81
def delete
    raise CloudConvert::InvalidStep if @step == :awaiting_creation
    url = construct_url(process_response[:subdomain], "process", process_response[:id])
    response = HTTMultiParty.delete(url)
    @step = :deleted if response.response.code == "200"
    return convert_response response
end
download(path, file_name="") click to toggle source
# File lib/cloud_convert/process.rb, line 68
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| 
        f.binmode
        f.write response.parsed_response
        full_path.to_s
    end
end
status() click to toggle source
# File lib/cloud_convert/process.rb, line 57
def status
    raise CloudConvert::InvalidStep if @step == :awaiting_creation
    url = process_url()
    response = send_request(http_method: :get,
                            url: url) do |response|
        create_parsed_response(:status_response, response.parsed_response)
        @step = @status_response[:step].to_sym
    end
    return convert_response response
end

Private Instance Methods

construct_url(action, id="") click to toggle source
# File lib/cloud_convert/process.rb, line 107
def construct_url(action, id="")
  id = "/#{id}" if id.length > 0
  return "#{CloudConvert::PROTOCOL}://#{CloudConvert::API_DOMAIN}/#{action}#{id}"
end
convert_response(response) click to toggle source
# File lib/cloud_convert/process.rb, line 123
def convert_response(response)
    case @client.return_type
    when :response
        return response.response
    else
        parsed_response = response.parsed_response.deep_symbolize
        return parsed_response
    end
end
create_parsed_response(variable_symbol, parsed_response) click to toggle source
# File lib/cloud_convert/process.rb, line 117
def create_parsed_response(variable_symbol, parsed_response)
    symbolized_response = parsed_response.deep_symbolize
    return self.instance_variable_set("@#{variable_symbol.to_s}", symbolized_response)
end
full_path(dir, file_name) click to toggle source
# File lib/cloud_convert/process.rb, line 133
def full_path(dir, file_name)
    return Pathname(dir).join(file_name)
end
process_url() click to toggle source
# File lib/cloud_convert/process.rb, line 112
def process_url()
    return "#{CloudConvert::PROTOCOL}:#{@process_response[:url]}"
end
send_request(opts) { |response| ... } click to toggle source
# File lib/cloud_convert/process.rb, line 98
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" || 
        (response.parsed_response.kind_of?(Hash) and response.parsed_response.key?("step")))
    return response
end
update_download_progress(response) click to toggle source
# File lib/cloud_convert/process.rb, line 137
def update_download_progress(response)
    if response.parsed_response["step"]
        create_parsed_response(:status_response, response.parsed_response)
        @step = @status_response[:step].to_sym
    end
    return convert_response response
end