Sha256: 66f0516cc095e76be0903c75639f8d213748746c853776b5348ac23aa72919be

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require 'json'
require 'prontoforms/resource'

module ProntoForms
  class FormSubmission < Resource
    def self.resource_name() 'data'; end

    property :id, key: 'identifier'
    property :reference_number, key: 'referenceNumber'
    property :state, key: 'state'
    property :data_state, key: 'dataState'
    property :data_persisted, key: 'dataPersisted'
    property :form_version_id, key: 'formVersionId'
    property :form_id, key: 'formId'
    property :user_id, key: 'userId'
    property :username, key: 'username'
    # Aliases
    property :data_persisted?, key: 'dataPersisted'
    property :submitter_id, key: 'userId'
    property :submitter_username, key: 'username'
    property :dispatcher, key: 'dispatcher'

    property :server_receive_date do
      str = data.fetch('serverReceiveDate')
      str.nil? ? nil : DateTime.strptime(str)
    end

    # Retrieve the pages containing the form questions and answers
    # @return [Hash] Hash of questions and answers for the FormSubmission
    def pages
      document.fetch('pages')
    end

    # Retrieve the dispatching User, if the form was dispatched
    # @return [User] The user that dispatched the form, or nil
    def dispatcher
      client.user(document.dig('dispatcher', 'identifier'))
    end

    private

    # Returns additional data about the submission. Uses cached data,
    # otherwise it loads and returns the data via #document!
    def document
      return @document if !@document.nil?
      document!
    end

    # Force loads the submission document
    def document!
      res = client.connection.get do |req|
        req.url "#{resource_name}/#{id}/document.json"
      end
      if res.success?
        @document = JSON.parse(res.body)
        @document
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prontoforms-0.3.1 lib/prontoforms/form_submission.rb