missinglink 0.1.6 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b278ee350c825cbf5865a20c06323cc2760988a7
4
- data.tar.gz: 1d12e2f05a11a165e56d4903229020a20ce1952a
3
+ metadata.gz: d5ab020970fa34b7686f693901d08b96088f9089
4
+ data.tar.gz: f0a3ad6302bce18822d17faeddfee59bf775325d
5
5
  SHA512:
6
- metadata.gz: b286d50f06cecd6c25726fb54857d57005177d1cbd88e5733b0d22d18fe2e52d161d7018890a70ebb1e980280f5ddd346c5402f94ee78622316884d20c8eb6d4
7
- data.tar.gz: 1b89b1e30c1c85646b305a90820b5ff219309a74d0d0061e82025058b4ab899e8296b0c965ad48ac6341075c8a7e14557dfb2edfe2a5123469d7d8b9eb49539d
6
+ metadata.gz: 557e5969361e996f5b6f3206c2296c8904c51c1597f5759ac2dbd4350a2aeffe28c480f3ec33bd528e0203f1f47416191d95cd5d1426318a5826421c9e3ddc78
7
+ data.tar.gz: 3aa659a7708656e19dfd80c76864c83816905e80c629a7a25a8606606c10a4e05ef3e93824ac73dc38079859a6b427efa2aebd242f4a38a1c792cf2102eda085
@@ -14,5 +14,67 @@ module Missinglink
14
14
  return Survey.create(sm_survey_id: sm_id.to_i)
15
15
  end
16
16
  end
17
+
18
+ def load_survey_details
19
+ response = Connection.request('get_survey_details',
20
+ { survey_id: sm_survey_id.to_s })
21
+ (puts "Error loading survey details for survey #{ self.inspect }." && return) unless response
22
+
23
+ update_from_survey_details(response)
24
+
25
+ response['pages'].each do |page|
26
+ SurveyPage.parse(self, page)
27
+ end
28
+
29
+ return true
30
+ end
31
+
32
+ def load_respondents
33
+ response = Connection.request('get_respondent_list',
34
+ { survey_id: sm_survey_id.to_s })
35
+ (puts "Error loading responses for survey #{ self.inspect }" && return) unless response
36
+
37
+ response['respondents'].each do |respondent|
38
+ SurveyRespondentDetail.parse(self, respondent)
39
+ end
40
+
41
+ return true
42
+ end
43
+
44
+ def respondents_to_update
45
+ completed_respondents = survey_respondent_details.completed
46
+ completed_respondents.select { |r| r.survey_responses.empty? }
47
+ end
48
+
49
+ def load_response_details(respondents)
50
+ respondents = [respondents] unless respondents.is_a? Array
51
+
52
+ while (respondents.size > 0)
53
+ ids = respondents.slice!(0, 100).map { |x| x.sm_respondent_id.to_s }
54
+ response = Connection.request('get_responses',
55
+ { survey_id: sm_survey_id.to_s,
56
+ respondent_ids: ids })
57
+
58
+ (puts "Error fetching response answers" && return) unless response
59
+
60
+ response.each do |r|
61
+ SurveyResponse.parse(self, r) unless r.nil?
62
+ end
63
+ end
64
+
65
+ return true
66
+ end
67
+
68
+ def update_from_survey_details(response = {})
69
+ return nil if response.nil? || response.empty? || response['title'].nil?
70
+
71
+ self.update_attributes({date_created: DateTime.parse(response['date_created']),
72
+ date_modified: DateTime.parse(response['date_modified']),
73
+ title: response['title']['text'],
74
+ language_id: response['language_id'].to_i,
75
+ nickname: response['nickname'],
76
+ title_enabled: response['title']['enabled'],
77
+ title_text: response['title']['text']})
78
+ end
17
79
  end
18
80
  end
@@ -6,6 +6,8 @@ module Missinglink
6
6
  has_many :survey_responses
7
7
  has_many :survey_response_answers, through: :survey_responses
8
8
 
9
+ scope :completed, -> { where(status: "completed") }
10
+
9
11
  def self.parse(survey, hash)
10
12
  srd = SurveyRespondentDetail.first_or_create_by_survey_details(survey.id, hash['respondent_id'])
11
13
 
@@ -0,0 +1,39 @@
1
+ require 'typhoeus'
2
+
3
+ module Missinglink
4
+ module Connection
5
+ extend self
6
+
7
+ def request(request_type, body = { })
8
+ (puts "Please provide credentials before making a request." && return) unless credentials_provided?
9
+
10
+ JSON.parse(typh_request(request_type,
11
+ @credential_hash[:api_key],
12
+ @credential_hash[:token],
13
+ body.to_json).tap {|x| x.run}.response.body)['data']
14
+ end
15
+
16
+ def credential_hash=(key_pair)
17
+ @credential_hash = { api_key: nil, token: nil }.merge(key_pair)
18
+ end
19
+
20
+ def credentials_provided?
21
+ !!(@credential_hash && @credential_hash[:api_key] && @credential_hash[:token])
22
+ end
23
+
24
+ private
25
+ def typh_request(fragment, api_key, token, body = '{ }')
26
+ # survey monkey's API limits are obnoxious, this is hacky but an easy way
27
+ # to ensure that we're always under the 2 QPS limit
28
+ sleep 0.5 unless ENV["RAILS_ENV"] == 'test'
29
+
30
+ Typhoeus::Request.new(
31
+ "https://api.surveymonkey.net/v2/surveys/#{fragment}",
32
+ method: :post,
33
+ params: { api_key: api_key },
34
+ body: body,
35
+ headers: { Authorization: "bearer #{token}", :"Content-Type" => "application/json" }
36
+ )
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module Missinglink
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2"
3
3
  end
data/lib/missinglink.rb CHANGED
@@ -1,111 +1,33 @@
1
+ require 'pry'
1
2
  require "missinglink/engine"
2
- require 'typhoeus'
3
+ require "missinglink/connection"
3
4
 
4
5
  module Missinglink
6
+ include Connection
5
7
  extend self
6
8
 
7
- def poll_surveys(credential_hash = { api_key: nil, token: nil })
8
- unless (api_key = credential_hash[:api_key]) && (token = credential_hash[:token])
9
- puts "Please provide a hash with api_key and token to poll surveys."
10
- return
11
- end
9
+ def poll_surveys
10
+ response = Connection.request('get_survey_list')
11
+ (puts "Error polling surveys" && return) unless response
12
12
 
13
- response = JSON.parse(typh_request('get_survey_list', api_key, token).tap {|x| x.run}.response.body)['data']['surveys']
14
- response.each do |s|
13
+ response['surveys'].each do |s|
15
14
  survey = Survey.first_or_create_by_sm_survey_id(s['survey_id'].to_i)
16
15
  survey.update_attributes(analysis_url: s['analysis_url'])
17
- fetch_survey(survey, credential_hash)
18
- end
19
- end
20
-
21
- def fetch_survey(survey, credential_hash)
22
- unless (api_key = credential_hash[:api_key]) && (token = credential_hash[:token])
23
- puts "Please provide a hash with api_key and token to fetch surveys."
24
- return
25
- end
26
-
27
- request = typh_request('get_survey_details', api_key, token, {survey_id: survey.sm_survey_id.to_s}.to_json).tap { |x| x.run }
28
- response = JSON.parse(request.response.body)['data']
29
-
30
- survey.update_attributes({date_created: DateTime.parse(response['date_created']),
31
- date_modified: DateTime.parse(response['date_modified']),
32
- title: response['title']['text'],
33
- language_id: response['language_id'].to_i,
34
- nickname: response['nickname'],
35
- title_enabled: response['title']['enabled'],
36
- title_text: response['title']['text']})
37
-
38
- response['pages'].each do |page|
39
- SurveyPage.parse(survey, page)
40
- end
41
- end
42
-
43
- def fetch_respondents(survey, credential_hash = { api_key: nil, token: nil })
44
- unless (api_key = credential_hash[:api_key]) && (token = credential_hash[:token])
45
- puts "Please provide a hash with api_key and token to fetch survey respondents."
46
- return
16
+ survey.load_survey_details
47
17
  end
48
-
49
- request = typh_request('get_respondent_list', api_key, token, {survey_id: survey.sm_survey_id.to_s}.to_json).tap { |x| x.run }
50
- respondents = JSON.parse(request.response.body)['data']['respondents']
51
- respondents.each do |respondent|
52
- SurveyRespondentDetail.parse(survey, respondent)
53
- end
54
-
55
- fetch_responses(survey.reload, credential_hash)
56
18
  end
57
19
 
58
- def fetch_responses(survey, credential_hash)
59
- unless (api_key = credential_hash[:api_key]) && (token = credential_hash[:token])
60
- puts "Please provide a hash with api_key and token to fetch survey responses."
61
- return
62
- end
63
-
64
- # to start, we will only pull down completed surveys, and not re-pull these surveys
65
- # if a respondent is complete and has no response, then we need to pull it down
66
- # if a respodnent is complete and has a response, then we will not re-pull, it's done
67
-
68
- respondents = SurveyRespondentDetail.where(survey_id: survey.id,
69
- status: "completed")
20
+ def fetch_respondents(survey)
21
+ return if survey.load_respondents.nil?
70
22
 
71
- respondents_to_pull = respondents.select { |r| r.survey_responses.empty? }
72
-
73
- fetch_response_answers(survey, respondents_to_pull, credential_hash)
23
+ fetch_responses(survey.reload)
74
24
  end
75
25
 
76
- def fetch_response_answers(survey, respondents, credential_hash)
77
- unless (api_key = credential_hash[:api_key]) && (token = credential_hash[:token])
78
- puts "Please provide a hash with api_key and token to fetch survey response answers."
79
- return
80
- end
81
-
82
- while (respondents.size > 0)
83
- ids = respondents.slice!(0, 100).map { |x| x.sm_respondent_id.to_s }
84
- request = typh_request('get_responses', api_key, token,
85
- { survey_id: survey.sm_survey_id.to_s,
86
- respondent_ids: ids }.to_json).tap { |x| x.run }
87
- response = JSON.parse(request.response.body)['data']
88
-
89
- response.each do |r|
90
- begin
91
- SurveyResponse.parse(survey, r) unless r.nil?
92
- rescue Exception => e
93
- raise "Error processing Survey Response for survey #{ survey.inspect }\n\nResponse hash: #{ response.inspect }\n\nAttempting to parse: #{ r.inspect }\n\nException: #{ e.inspect }"
94
- end
95
- end
96
- end
26
+ def fetch_responses(survey)
27
+ fetch_response_answers(survey, survey.respondents_to_update)
97
28
  end
98
29
 
99
- private
100
- def typh_request(fragment, api_key, token, body = '{ }')
101
- sleep 0.5 # survey monkey's API limits are obnoxious, this is hacky but an easy way to ensure that we're always under
102
- # the 2 QPS limit
103
- Typhoeus::Request.new(
104
- "https://api.surveymonkey.net/v2/surveys/#{fragment}",
105
- method: :post,
106
- params: { api_key: api_key },
107
- body: body,
108
- headers: { Authorization: "bearer #{token}", :"Content-Type" => "application/json" }
109
- )
30
+ def fetch_response_answers(survey, respondents)
31
+ survey.load_response_details(respondents)
110
32
  end
111
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: missinglink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trey Springer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-12 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '2.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: typhoeus
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -120,6 +134,7 @@ files:
120
134
  - config/routes.rb
121
135
  - db/migrate/20140308005724_add_missinglink_survey_schema.rb
122
136
  - lib/missinglink.rb
137
+ - lib/missinglink/connection.rb
123
138
  - lib/missinglink/engine.rb
124
139
  - lib/missinglink/version.rb
125
140
  - lib/tasks/missinglink_tasks.rake