class ProntoForms::FormSubmission

A FormSubmission represents submitted form data in ProntoForms. It includes various metadata about the submission as well.

Public Class Methods

resource_name() click to toggle source
# File lib/prontoforms/form_submission.rb, line 10
def self.resource_name
  'data'
end

Public Instance Methods

dispatched?() click to toggle source

Check if the form was dispatched @return [Boolean] True if the form was dispatched; false otherwise

# File lib/prontoforms/form_submission.rb, line 60
def dispatched?
  !document.dig('dispatcher', 'identifier').nil?
end
dispatcher() click to toggle source

Retrieve the dispatching User, if the form was dispatched @return [User] The user that dispatched the form, or nil

# File lib/prontoforms/form_submission.rb, line 52
def dispatcher
  return nil unless dispatched?

  client.user(document.dig('dispatcher', 'identifier'))
end
documents(populate: false) click to toggle source

Retrieve all documents attached to this form submission @return [Array] Documents attached to the form submission

# File lib/prontoforms/form_submission.rb, line 84
def documents(populate: false)
  ids = form_version.document_ids
  if populate
    ids.map { |id| form_space.document(id) }
  else
    ids
  end
end
download_document(document) click to toggle source

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

# File lib/prontoforms/form_submission.rb, line 96
def download_document(document)
  io = StringIO.new
  client.connection.get do |req|
    req.url "#{url}/documents/#{document.id}"
    req.options.on_data = proc { |chunk| io << chunk }
  end
  io.rewind
  io
end
form() click to toggle source

Retrieve the form for the form submission @return [Form] Form for the submission

# File lib/prontoforms/form_submission.rb, line 72
def form
  form_space.form(full_data.dig('form', 'formId'))
end
form_space() click to toggle source

Retrieve the form space for the form submission @return [FormSpace] Form space for the submission's form

# File lib/prontoforms/form_submission.rb, line 66
def form_space
  client.form_space(full_data.dig('form', 'formSpaceId'))
end
form_version() click to toggle source

Retrieve the current version of the form @return [FormIteration] The form iteration

# File lib/prontoforms/form_submission.rb, line 78
def form_version
  form.current_version
end
pages() click to toggle source

Retrieve the pages containing the form questions and answers @return [Hash] Hash of questions and answers for the FormSubmission

# File lib/prontoforms/form_submission.rb, line 46
def pages
  document.fetch('pages')
end

Private Instance Methods

document() click to toggle source

Returns additional data about the submission. Uses cached data, otherwise it loads and returns the data via document!

# File lib/prontoforms/form_submission.rb, line 121
def document
  return @document unless @document.nil?

  document!
end
document!() click to toggle source

Force loads the submission document

# File lib/prontoforms/form_submission.rb, line 128
def document!
  res = client.connection.get do |req|
    req.url "#{resource_name}/#{id}/document.json"
  end

  @document = JSON.parse(res.body)
  @document
end
full_data() click to toggle source
# File lib/prontoforms/form_submission.rb, line 112
def full_data
  return @full_data unless @full_data.nil?

  @full_data = client.form_submission(id).data
  @full_data
end
url() click to toggle source
# File lib/prontoforms/form_submission.rb, line 108
def url
  "#{resource_name}/#{id}"
end