Class: Rapidfire::SurveyResults

Inherits:
BaseService show all
Defined in:
app/services/rapidfire/survey_results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#surveyObject

Returns the value of attribute survey.



3
4
5
# File 'app/services/rapidfire/survey_results.rb', line 3

def survey
  @survey
end

Instance Method Details

#extractObject

extracts question along with results each entry will have the following:

  1. question type and question id

  2. question text

  3. if aggregatable, return each option with value

  4. else return an array of all the answers given



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/rapidfire/survey_results.rb', line 11

def extract
  @survey.questions.collect do |question|
    results =
      case question
      when Rapidfire::Questions::Select, Rapidfire::Questions::Radio,
        Rapidfire::Questions::Checkbox
        answers = question.answers.map(&:answer_text).map do |text|
          text.to_s.split(Rapidfire.answers_delimiter)
        end.flatten

        answers.inject(Hash.new(0)) { |total, e| total[e] += 1; total }
      when Rapidfire::Questions::Short, Rapidfire::Questions::Date,
        Rapidfire::Questions::Long, Rapidfire::Questions::Numeric
        question.answers.pluck(:answer_text)
      end

    QuestionResult.new(question: question, results: results)
  end
end