Class: Missinglink::Survey

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/missinglink/survey.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_or_create_by_sm_survey_id(sm_id) ⇒ Object



9
10
11
12
13
14
15
16
# File 'app/models/missinglink/survey.rb', line 9

def self.first_or_create_by_sm_survey_id(sm_id)
  # spelling this one out more explicitly so it's easier to test
  if new_survey = Survey.find_by_sm_survey_id(sm_id.to_i)
    return new_survey
  else
    return Survey.create(sm_survey_id: sm_id.to_i)
  end
end

Instance Method Details

#load_respondentsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/missinglink/survey.rb', line 38

def load_respondents
  begin
    response = Connection.request('get_respondent_list',
                                  { survey_id: sm_survey_id.to_s })
  rescue Exception => e
    puts "Exception raised when loading survey respondents for #{ self.inspect }.\n#{ e.inspect }"
    return
  end

  (puts "Error loading responses for survey #{ self.inspect }" && return) unless response

  response['respondents'].each do |respondent|
    SurveyRespondentDetail.parse(self, respondent)
  end

  return true
end

#load_response_details(respondents) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/missinglink/survey.rb', line 61

def load_response_details(respondents)
  respondents = [respondents] unless respondents.is_a? Array

  while (respondents.size > 0)
    ids = respondents.slice!(0, 100).map { |x| x.sm_respondent_id.to_s }
    begin
      response = Connection.request('get_responses',
                                    { survey_id: sm_survey_id.to_s,
                                      respondent_ids: ids })
    rescue Exception => e
      puts "Exception raised when loading response details for #{ self.inspect }.\n#{ e.inspect }"
      return
    end

    (puts "Error fetching response answers" && return) unless response

    response.each do |r|
      SurveyResponse.parse(self, r) unless r.nil?
    end
  end

  return true
end

#load_survey_detailsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/missinglink/survey.rb', line 18

def load_survey_details
  begin
    response = Connection.request('get_survey_details',
                                       { survey_id: sm_survey_id.to_s })
  rescue Exception => e
    puts "Exception raised when loading survey details for #{ self.inspect }.\n#{ e.inspect }"
    return
  end

  (puts "Error loading survey details for survey #{ self.inspect }." && return) unless response

  update_from_survey_details(response)

  response['pages'].each do |page|
    SurveyPage.parse(self, page)
  end

  return true
end

#respondents_to_updateObject



56
57
58
59
# File 'app/models/missinglink/survey.rb', line 56

def respondents_to_update
  completed_respondents = survey_respondent_details.completed
  completed_respondents.select { |r| r.survey_responses.empty? }
end

#update_from_survey_details(response = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/missinglink/survey.rb', line 85

def update_from_survey_details(response = {})
  return nil if response.nil? || response.empty? || response['title'].nil?

  self.update_attributes({date_created: DateTime.parse(response['date_created']),
                          date_modified: DateTime.parse(response['date_modified']),
                          title: response['title']['text'],
                          language_id: response['language_id'].to_i,
                          nickname: response['nickname'],
                          title_enabled: response['title']['enabled'],
                          title_text: response['title']['text']})
end