lib/groupdocs/questionnaire.rb in groupdocs-1.1.0 vs lib/groupdocs/questionnaire.rb in groupdocs-1.2.0

- old
+ new

@@ -1,29 +1,43 @@ module GroupDocs class Questionnaire < Api::Entity + require 'groupdocs/questionnaire/collector' require 'groupdocs/questionnaire/execution' require 'groupdocs/questionnaire/page' require 'groupdocs/questionnaire/question' + include Api::Helpers::Status + # # Returns an array of all questionnaires. # + # @param [Hash] options Hash of options + # @option options [Symbol] :status Filter questionnaires by status + # @option options [Integer] :page_number Page to start with + # @option options [Integer] :page_size How many items to list # @param [Hash] access Access credentials # @option access [String] :client_id # @option access [String] :private_key # @return [Array<GroupDocs::Questionnaire>] # - def self.all!(access = {}) - json = Api::Request.new do |request| + def self.all!(options = {}, access = {}) + if options[:status] + # TODO find better way to parse status + options[:status] = new.send(:parse_status, options[:status]) + end + + api = Api::Request.new do |request| request[:access] = access request[:method] = :GET request[:path] = '/merge/{{client_id}}/questionnaires' - end.execute! + end + api.add_params(options) + json = api.execute! json[:questionnaires].map do |questionnaire| - Questionnaire.new(questionnaire) + new(questionnaire) end end # # Returns questionnaire by identifier. @@ -39,47 +53,42 @@ request[:access] = access request[:method] = :GET request[:path] = "/merge/{{client_id}}/questionnaires/#{id}" end.execute! - Questionnaire.new(json[:questionnaire]) + new(json[:questionnaire]) rescue RestClient::ResourceNotFound nil end - # - # Returns an array of all executions. - # - # @param [Hash] access Access credentials - # @option access [String] :client_id - # @option access [String] :private_key - # @return [Array<GroupDocs::Questionnaire::Execution>] - # - def self.executions!(access = {}) - json = Api::Request.new do |request| - request[:access] = access - request[:method] = :GET - request[:path] = '/merge/{{client_id}}/questionnaires/executions' - end.execute! - - json[:executions].map do |execution| - Questionnaire::Execution.new(execution) - end - end - # @attr [Integer] id attr_accessor :id + # @attr [String] guid + attr_accessor :guid # @attr [String] name attr_accessor :name + # @attr [Symbol] status + attr_accessor :status # @attr [String] descr attr_accessor :descr # @attr [Array<GroupDocs::Questionnaire::Page>] pages attr_accessor :pages + # @attr [Integer] resolved_executions + attr_accessor :resolved_executions + # @attr [Integer] assigned_questions + attr_accessor :assigned_questions + # @attr [Integer] total_questions + attr_accessor :total_questions + # @attr [Integer] modified + attr_accessor :modified + # @attr [Integer] expires + attr_accessor :expires + # @attr [Array<String>] document_ids + attr_accessor :document_ids # Human-readable accessors - alias_method :description, :descr - alias_method :description=, :descr= + alias_accessor :description, :descr # # Converts each page to GroupDocs::Questionnaire::Page object. # # @param [Array<GroupDocs::Questionnaire::Page, Hash>] pages @@ -95,10 +104,19 @@ end end end # + # Converts status to human-readable format. + # + # @return [Symbol] + # + def status + parse_status(@status) + end + + # # Adds page to questionnaire. # # @param [GroupDocs::Questionnaire::Page] page # @raise [ArgumentError] if page is not GroupDocs::Questionnaire::Page object # @@ -124,10 +142,12 @@ request[:path] = '/merge/{{client_id}}/questionnaires' request[:request_body] = to_hash end.execute! self.id = json[:questionnaire_id] + self.guid = json[:questionnaire_guid] + self.name = json[:adjusted_name] end # # Updates questionnaire. # @@ -181,39 +201,113 @@ DataSource.new(datasource) end end # - # Creates new questionnaire execution. + # Returns an array of questionnaire executions. # + # @param [Hash] access Access credentials + # @option access [String] :client_id + # @option access [String] :private_key + # @return [Array<GroupDocs::Questionnaire::Execution>] + # + def executions!(access = {}) + json = Api::Request.new do |request| + request[:access] = access + request[:method] = :GET + request[:path] = "/merge/{{client_id}}/questionnaires/#{guid}/executions" + end.execute! + + json[:executions].map do |execution| + Execution.new(execution) + end + end + + # + # Returns an array of questionnaire collectors. + # + # @param [Hash] access Access credentials + # @option access [String] :client_id + # @option access [String] :private_key + # @return [Array<GroupDocs::Questionnaire::Collector>] + # + def collectors!(access = {}) + json = Api::Request.new do |request| + request[:access] = access + request[:method] = :GET + request[:path] = "/merge/{{client_id}}/questionnaires/#{guid}/collectors" + end.execute! + + json[:collectors].map do |collector| + collector.merge!(questionnaire: self) + Collector.new(collector) + end + end + + # + # Returns questionnaire metadata. + # + # @param [Hash] access Access credentials + # @option access [String] :client_id + # @option access [String] :private_key + # @return [GroupDocs::Questionnaire] + # + def metadata!(access = {}) + json = Api::Request.new do |request| + request[:access] = access + request[:method] = :GET + request[:path] = "/merge/{{client_id}}/questionnaires/#{guid}/metadata" + end.execute! + + Questionnaire.new(json[:questionnaire]) + end + + # + # Updates questionnaire metadata. + # # @example - # execution = GroupDocs::Questionnaire::Execution.new # questionnaire = GroupDocs::Questionnaire.get!(1) - # execution = questionnaire.create_execution!(execution, 'user@email.com') - # #=> #<GroupDocs::Questionnaire::Execution @id=1, @questionnaire_id=1> + # metadata = questionnaire.metadata! + # metadata.name = 'New questionnaire name' + # questionnaire.update_metadata! metadata # - # @param [GroupDocs::Questionnaire::Execution] execution - # @param [String] email # @param [Hash] access Access credentials # @option access [String] :client_id # @option access [String] :private_key - # @return [GroupDocs::Questionnaire::Execution] updated execution + # @raise [ArgumentError] if metadata is not GroupDocs::Questionnaire # - def create_execution!(execution, email, access = {}) - execution.is_a?(GroupDocs::Questionnaire::Execution) or raise ArgumentError, - "Execution should be GroupDocs::Questionnaire::Execution object, received: #{execution.inspect}" + def update_metadata!(metadata, access = {}) + metadata.is_a?(GroupDocs::Questionnaire) or raise ArgumentError, + "Metadata should be GroupDocs::Questionnaire object, received: #{metadata.inspect}" - json = Api::Request.new do |request| + Api::Request.new do |request| request[:access] = access - request[:method] = :POST - request[:path] = "/merge/{{client_id}}/questionnaires/#{id}/executions" - request[:request_body] = execution.to_hash.merge(executive: { primary_email: email }) + request[:method] = :PUT + request[:path] = "/merge/{{client_id}}/questionnaires/#{guid}/metadata" + request[:request_body] = metadata.to_hash end.execute! + end - execution.id = json[:execution_id] - execution.questionnaire_id = json[:questionnaire_id] + # + # Returns an array of document fields for questionnaire. + # + # @param [Hash] access Access credentials + # @option access [String] :client_id + # @option access [String] :private_key + # @return [Array<GroupDocs::Document::Field>] + # + def fields!(access = {}) + api = Api::Request.new do |request| + request[:access] = access + request[:method] = :GET + request[:path] = "/merge/{{client_id}}/questionnaires/#{guid}/fields" + end + api.add_params(include_geometry: true) + json = api.execute! - execution + json[:fields].map do |field| + Document::Field.new(field) + end end end # Questionnaire end # GroupDocs