Class: EasyPdfCloud::Workflow

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_pdf_cloud.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, workflow_id, options = {}) ⇒ Workflow

Returns a new instance of Workflow.



114
115
116
117
118
119
120
121
122
# 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

Instance Method Details

#convert(filepath, source_extension, dest_extension) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# 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) ⇒ Object



149
150
151
152
153
# 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) ⇒ Object



183
184
185
186
# 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) ⇒ Object

Delete File from output folder.



171
172
173
174
175
# 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) ⇒ Object

Download Output File



156
157
158
159
160
161
162
163
# 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) ⇒ Object

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).



207
208
209
210
211
# 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) ⇒ Object



199
200
201
202
# 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) ⇒ Object



165
166
167
168
# 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) ⇒ Object

There is no response from this command.



178
179
180
181
# 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) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# 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