1: require 'httparty'
2: require 'certified'
3: require 'pp'
4: class SurveyMonkey
5: include HTTParty
6: attr_accessor :access_token, :base_uri, :api_key
7: attr_reader :result
8: def initialize(access_token, base_uri,api_key)
9: @access_token = access_token
10: @api_key = '?api_key=' + "#{api_key}"
11: @base_uri = base_uri
12: @result = {}
13: end
14: def get_survey_list(start_date, end_date, *fields)
15: current_page = 1
16: params_hash = {}
17: params_hash[:start_date] = "#{start_date}"
18: params_hash[:end_date] = "#{end_date}"
19: params_hash[:fields] = fields
20: params_hash[:page] = current_page
21: while true
22: @result = HTTParty.post("#{@base_uri}" + '/v2/surveys/get_survey_list' + "#{@api_key}",
23: #:debug_output => $stdout,
24: :headers => {'Authorization' => "bearer #{@access_token.to_s}", 'Content-type' => 'application/json'},
25: :body => params_hash.to_json
26: )
27: if @result["data"]["page"] == current_page
28: current_page += 1
29: else
30: break
31: end
32: end
33: end
34: def get_survey_details(survey_id)
35: @result = HTTParty.post("#{@base_uri}" + '/v2/surveys/get_survey_details' + "#{@api_key}",
36: #:debug_output => $stdout,
37: :headers => {'Authorization' => "bearer #{@access_token.to_s}", 'Content-type' => 'application/json'},
38: :body => {:survey_id => "#{survey_id}"
39: }.to_json
40: )
41: end
42: def get_respondent_list(survey_id, *fields)
43: params_hash = {}
44: params_hash[:survey_id] = "#{survey_id}"
45: params_hash[:fields] = fields
46: @result = HTTParty.post("#{@base_uri}" + '/v2/surveys/get_respondent_list' + "#{@api_key}",
47: #:debug_output => $stdout,
48: :headers => {'Authorization' => "bearer #{@access_token.to_s}", 'Content-type' => 'application/json'},
49: :body => params_hash.to_json
50: )
51: end
52: def get_responses(survey_id, respondent_ids, *fields)
53: params_hash = {}
54: params_hash[:survey_id] = "#{survey_id}"
55: params_hash[:respondent_ids] = respondent_ids
56: params_hash[:fields] = fields
57: @result = HTTParty.post("#{@base_uri}" + '/v2/surveys/get_responses' + "#{@api_key}",
58: #:debug_output => $stdout,
59: :headers => {'Authorization' => "bearer #{@access_token.to_s}", 'Content-type' => 'application/json'},
60: :body => params_hash.to_json
61: )
62: end
63: end
64: start_date = 'YYYY-MM-DD 00:00:00'
65: end_date = 'YYYY-MM-DD 00:00:00'
66: base_uri = 'https://api.surveymonkey.net'
67: access_token = 'XX register with developer API to obtain access token XX'
68: api_key = 'XX avaiable via Survey Monkey for your survey (not developer) account XX'
69: #create the object
70: xx = SurveyMonkey.new(access_token, base_uri, api_key)
71: #method call with parameters
72: xx.get_survey_list(start_date, end_date)
73: #print results to console
74: puts xx.result["data"]
If a user can skip a question you cannot guarantee that the json tree will contain the data elements to traverse tree so you will have to retrieve each users complete data tree and then load them into normalised database tables.
An example of Oauth using the Google API is here.
To follow;
- Error Handling
No comments:
Post a Comment