Class: FormAPI::Client

Inherits:
PDFApi
  • Object
show all
Defined in:
lib/form_api/api/client.rb

Defined Under Namespace

Classes: FailedBatchError, InvalidDataError, InvalidResponseError, PollTimeoutError

Instance Attribute Summary

Attributes inherited from PDFApi

#api_client

Instance Method Summary collapse

Methods inherited from PDFApi

#batch_generate_pdf_v1, #batch_generate_pdf_v1_with_http_info, #batch_generate_pdfs_with_http_info, #combine_pdfs_with_http_info, #combine_submissions_with_http_info, #create_custom_file_from_upload, #create_custom_file_from_upload_with_http_info, #create_data_request_token, #create_data_request_token_with_http_info, #create_folder, #create_folder_with_http_info, #create_template, #create_template_from_upload, #create_template_from_upload_with_http_info, #create_template_with_http_info, #delete_folder, #delete_folder_with_http_info, #expire_combined_submission, #expire_combined_submission_with_http_info, #expire_submission, #expire_submission_with_http_info, #generate_pdf_with_http_info, #get_combined_submission, #get_combined_submission_with_http_info, #get_data_request, #get_data_request_with_http_info, #get_presign_url, #get_presign_url_with_http_info, #get_submission, #get_submission_batch, #get_submission_batch_with_http_info, #get_submission_with_http_info, #get_template, #get_template_schema, #get_template_schema_with_http_info, #get_template_with_http_info, #initialize, #list_folders, #list_folders_with_http_info, #list_templates, #list_templates_with_http_info, #move_folder_to_folder, #move_folder_to_folder_with_http_info, #move_template_to_folder, #move_template_to_folder_with_http_info, #rename_folder, #rename_folder_with_http_info, #test_authentication, #test_authentication_with_http_info, #update_data_request, #update_data_request_with_http_info

Constructor Details

This class inherits a constructor from FormAPI::PDFApi

Instance Method Details

#batch_generate_and_combine_pdfs(options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/form_api/api/client.rb', line 155

def batch_generate_and_combine_pdfs(options)
  # We pass the wait option to the combine_submissions method
  wait = options.key?(:wait) ? options.delete(:wait) : true

  response = batch_generate_pdfs(options)
  if response.status == 'error'
    raise FailedBatchError, 'Batch job failed, cannot combine PDFs!'
  end
  submission_ids = response.submissions.map { |s| s.submission.id }
  combine_submissions(submission_ids: submission_ids, wait: wait)
end

#batch_generate_pdfs(options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/form_api/api/client.rb', line 44

def batch_generate_pdfs(options)
  unless options[:submissions].is_a?(::Array)
    raise InvalidDataError, ":submissions is required, and must be an Array."
  end
  options[:submissions].each do |submission|
    unless submission[:data].is_a?(::Hash)
      raise InvalidDataError,
        ":data is required for each submission, and must be a Hash."
    end
  end

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

  submission_batch_data = options
  response = super(submission_batch_data)

  return response unless wait

  batch = response.submission_batch
  submission_responses = response.submissions
  timeout = options[:timeout] || 600
  start_time = Time.now

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

  # While polling the batch status, we exclude any info about the submissions.
  # We need to make one more request to fetch the updated submissions and
  # prepare the correct responses for CreateSubmissionBatchResponse
  batch_with_submissions = get_submission_batch(batch.id, include_submissions: true)
  if batch_with_submissions.submissions && batch_with_submissions.submissions.any?
    updated_submissions_hash =
      batch_with_submissions.submissions.each_with_object({}) do |sub, h|
        next h unless sub && sub.id
        h[sub.id] = sub
      end
    submission_responses.each do |sub|
      # If there's no submission it's already an error.
      next unless sub.submission
      updated_sub = updated_submissions_hash[sub.submission.id]
      if updated_sub
        sub.submission = updated_sub
        sub.status = updated_sub.state == 'processed' ? 'success' : 'error'
      else
        sub.status = 'error'
      end
    end
  end

  CreateSubmissionBatchResponse.new(
    status: batch.state == 'processed' ? 'success' : 'error',
    submission_batch: batch,
    submissions: submission_responses
  )
end

#combine_pdfs(options) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/form_api/api/client.rb', line 108

def combine_pdfs(options)
  unless options[:source_pdfs].is_a?(::Array)
    raise InvalidDataError, "source_pdfs is required, and must be an Array."
  end

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

  # PdfAPI requires a :combined_submission_data option.
  response = super(options)

  return response unless wait

  combined_submission = response.combined_submission
  timeout = options[:timeout] || 600
  start_time = Time.now

  # Wait for submission to be ready
  while combined_submission.state == 'pending'
    sleep 1
    combined_submission = get_combined_submission(combined_submission.id)

    if Time.now - start_time > timeout
      raise PollTimeoutError, "Merged PDF was not ready after #{timeout} seconds!"
    end
  end

  CreateCombinedSubmissionResponse.new(
    status: combined_submission.state == 'processed' ? 'success' : 'error',
    combined_submission: combined_submission
  )
end

#combine_submissions(options) ⇒ Object

Alias for combine_pdfs, for backwards compatibility



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/form_api/api/client.rb', line 143

def combine_submissions(options)
  unless options[:submission_ids].is_a?(::Array)
    raise InvalidDataError, "submission_ids is required, and must be an Array."
  end
  options[:source_pdfs] = options[:submission_ids].map do |id|
    { type: 'submission', id: id }
  end
  options.delete :submission_ids

  combine_pdfs(options)
end

#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