Method: FormAPI::Client#generate_pdf

Defined in:
lib/form_api/api/client.rb

#generate_pdf(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/form_api/api/client.rb', line 10

def generate_pdf(options)
  unless options[:data].is_a?(::Hash)
    raise InvalidDataError, "data is required, and must be a Hash."
  end

  # Wait for job to finish by default.
  wait = options.key?(:wait) ? options[:wait] : true
  options.delete :wait

  template_id = options.delete :template_id
  create_submission_data = options
  response = super(template_id, create_submission_data)

  return response unless wait

  submission = response.submission
  timeout = options[:timeout] || 60
  start_time = Time.now

  # Wait for submission to be ready
  while submission.state == 'pending'
    sleep 1
    submission = get_submission(submission.id)
    if Time.now - start_time > timeout
      raise PollTimeoutError, "PDF was not processed after #{timeout} seconds!"
    end
  end

  CreateSubmissionResponse.new(
    status: submission.state == 'processed' ? 'success' : 'error',
    submission: submission
  )
end