Class: DocSpring::Client

Inherits:
PDFApi
  • Object
show all
Defined in:
lib/docspring/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

#add_fields_to_template, #add_fields_to_template_with_http_info, #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, #copy_template, #copy_template_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_html_template, #create_html_template_with_http_info, #create_pdf_template, #create_pdf_template_from_upload, #create_pdf_template_from_upload_with_http_info, #create_pdf_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_full_template, #get_full_template_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_submissions, #list_submissions_0, #list_submissions_0_with_http_info, #list_submissions_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, #update_template, #update_template_with_http_info

Constructor Details

This class inherits a constructor from DocSpring::PDFApi

Instance Method Details

#batch_generate_and_combine_pdfs(options) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/docspring/api/client.rb', line 161

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



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
107
108
109
110
111
# File 'lib/docspring/api/client.rb', line 46

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

  # Wait for submission to be ready
  while batch.state == 'pending'
    sleep 1
    batch = get_submission_batch(batch.id)
    if Time.now.to_i - 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&.any?
    updated_submissions_hash =
      batch_with_submissions.submissions.each_with_object({}) do |sub, h|
        next h unless 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



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
141
142
143
144
145
# File 'lib/docspring/api/client.rb', line 113

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

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

    if Time.now.to_i - 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



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/docspring/api/client.rb', line 148

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



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
43
44
# File 'lib/docspring/api/client.rb', line 12

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

  # Wait for submission to be ready
  while submission.state == 'pending'
    sleep 1
    submission = get_submission(submission.id)
    if Time.now.to_i - 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