class EasyPdfCloud::Workflow
Public Class Methods
new(client, workflow_id, options = {})
click to toggle source
# File lib/easy_pdf_cloud.rb, line 114 def initialize(client, workflow_id, options = {}) @client = client @access_token = client.access_token @workflow_id = workflow_id @workflow_url = "#{client.workflow_url}/#{@workflow_id}" @jobs_url = "#{client.api_url}/jobs" @event_id = nil @debug = options[:debug] end
Public Instance Methods
convert(filepath, source_extension, dest_extension)
click to toggle source
# File lib/easy_pdf_cloud.rb, line 124 def convert(filepath, source_extension, dest_extension) raise "Invalid file: #{filepath}" if !File.file?(filepath) file_data = File.open(filepath, 'rb') {|f| f.read} filename = File.basename(filepath) out_data = convert_data(filename, file_data, source_extension, dest_extension) out_filepath = filepath.sub(".#{source_extension}", ".#{dest_extension}") File.open(out_filepath, "wb") {|f| f.write(out_data)} return out_filepath end
convert_data(filename, data, source_extension, dest_extension)
click to toggle source
# File lib/easy_pdf_cloud.rb, line 137 def convert_data(filename, data, source_extension, dest_extension) job_id = create_job_from_file(filename, data) wait_for_completion(job_id) output_file = filename.sub(".#{source_extension}", ".#{dest_extension}") response = retrieve_file(job_id, output_file) # The job stays around after execution. We can either delete or keep a history some where. # delete_job(job_id) # Delete the output file so it doesn't take up storage space. delete_output_file(job_id, output_file) response.body end
create_job_from_file(filename, file_data)
click to toggle source
# File lib/easy_pdf_cloud.rb, line 149 def create_job_from_file(filename, file_data) create_job_url = "#{@workflow_url}/jobs?file=#{filename}" response = @access_token.put(create_job_url, {:body => file_data, :headers => {"Content-Type" => "application/pdf"}}) return response.parsed["jobID"] end
delete_job(id)
click to toggle source
# File lib/easy_pdf_cloud.rb, line 183 def delete_job(id) response = @access_token.delete("#{@jobs_url}/#{id}") response.status end
delete_output_file(job_id, filename)
click to toggle source
Delete File from output folder.
# File lib/easy_pdf_cloud.rb, line 171 def delete_output_file(job_id, filename) file_url = "#{@jobs_url}/#{job_id}/output/#{filename}" response = @access_token.delete(file_url) return response.parsed.is_a?(Hash) end
download(job_id, filename, destination_path = nil)
click to toggle source
Download Output File
# File lib/easy_pdf_cloud.rb, line 156 def download(job_id, filename, destination_path = nil) response = retrieve_file(job_id, filename) filepath = (destination_path ? File.join(destination_path, filename) : filename) File.open(filepath, "wb") {|f| f.write(response.body)} delete_output_file(job_id, filename) true end
job_event(job_id)
click to toggle source
www.easypdfcloud.com/developer/reference#jobs_event This API waits for an event for up to 30 seconds. If the job execution does not complete within this duration, returns HTTP status code 202 (Accepted).
# File lib/easy_pdf_cloud.rb, line 207 def job_event(job_id) response = @access_token.post("#{@jobs_url}/#{job_id}/event") hash = response.parsed return hash["status"] == "completed" end
job_status(job_id)
click to toggle source
# File lib/easy_pdf_cloud.rb, line 199 def job_status(job_id) response = @access_token.get("#{@jobs_url}/#{job_id}") response.parsed end
retrieve_file(job_id, filename)
click to toggle source
# File lib/easy_pdf_cloud.rb, line 165 def retrieve_file(job_id, filename) file_url = "#{@jobs_url}/#{job_id}/output/#{filename}" response = @access_token.get(file_url) end
start_job(id)
click to toggle source
There is no response from this command.
# File lib/easy_pdf_cloud.rb, line 178 def start_job(id) response = @access_token.post("#{@jobs_url}/#{id}") response.status end
wait_for_completion(job_id)
click to toggle source
# File lib/easy_pdf_cloud.rb, line 188 def wait_for_completion(job_id) count = 0 while (job_event(job_id) == false) if count == 6 delete_job(job_id) raise "Failed to convert after 180 seconds." end count += 1 end end