lib/prontoforms/form_submission.rb in prontoforms-1.0.1 vs lib/prontoforms/form_submission.rb in prontoforms-2.0.0

- old
+ new

@@ -62,46 +62,59 @@ end # Retrieve the form space for the form submission # @return [FormSpace] Form space for the submission's form def form_space - client.form_space(data.dig('form', 'formSpaceId')) + client.form_space(full_data.dig('form', 'formSpaceId')) end # Retrieve the form for the form submission # @return [Form] Form for the submission def form - client.form(data.dig('form', 'formId')) + form_space.form(full_data.dig('form', 'formId')) end - # Retrieve the standard PDF document for the form submission - # @return [IO] Readable stream containing the PDF contents - def pdf + # Retrieve the current version of the form + # @return [FormIteration] The form iteration + def form_version + form.current_version + end + + # Retrieve all documents attached to this form submission + # @return [Array] Documents attached to the form submission + def documents(populate: false) + ids = form_version.document_ids + if populate + ids.map { |id| form.iteration(id) } + else + ids + end + end + + # Download a specific document. The Document must have been attached to + # the form's current version at the time of submission. + # @return [IO] Data stream for the document + def download_document(document) io = StringIO.new client.connection.get do |req| - req.url "#{resource_name}/#{id}/documents/#{pdf_document.id}" - req.options.on_data = proc do |chunk| - io << chunk - end + req.url "#{url}/documents/#{document.id}" + req.options.on_data = proc { |chunk| io << chunk } end - io.rewind io end private - # Retrieve the standard PDF document for the form submission - # @return [Document] PDF document for the form submission - # @raises RuntimeError - def pdf_document - document = form_space.documents.items.find do |doc| - doc.type == 'Pdf' && doc.standard? - end + def url + "#{resource_name}/#{id}" + end - raise "No PDF document found for form space #{fs.id}" if document.nil? + def full_data + return @full_data unless @full_data.nil? - document + @full_data = client.form_submission(id).data + @full_data end # Returns additional data about the submission. Uses cached data, # otherwise it loads and returns the data via #document! def document