-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMapi.rb
59 lines (48 loc) · 2.21 KB
/
SMapi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'httparty'
require 'json'
class SMapi
include HTTParty
base_uri 'https://api.surveymonkey.net'
format :plain #this keeps HTTParty from automatically parsing the response as JSON - I'll do that myself
# debug_output $stderr
@@get_survey_list_url = '/v2/surveys/get_survey_list'
@@get_respondent_list_url = '/v2/surveys/get_respondent_list'
@@get_collector_list_url = '/v2/surveys/get_get_collector_list'
@@get_survey_details_url = '/v2/surveys/get_survey_details'
@@get_responses_url = '/v2/surveys/get_responses'
@@get_response_counts_url = '/v2/surveys/get_response_counts'
@@get_user_details_url = '/v2/user/get_user_details'
@@oauth_authorize_url = '/oauth/authorize'
@@oauth_token_url = '/oauth/token'
def initialize(api_key, access_token)
# ensure api_key and access_token quack like a string
return nil unless (api_key.respond_to?(:to_str) && access_token.respond_to?(:to_str))
@api_key = api_key.to_str
@access_token = access_token.to_str
@headers = {
'Authorization' => "bearer #{@access_token}",
'Content-type' => 'application/json'
}
end
def self.build_authorize_url(api_key,client_id,redirect_uri)
return default_options[:base_uri] + @@oauth_authorize_url + "?response=code&api_key=#{api_key}&client_id=#{client_id}&redirect_uri=#{URI::encode(redirect_uri)}"
end
def self.exchange_code_for_token(api_key, client_id, redirect_uri, client_secret, code)
response = self.class.post(@@oauth_token_url, :query => { :api_key => api_key}, :body => {:grant_type => 'authorization_code', :client_id => client_id, :redirect_uri => redirect_uri, :client_secret => client_secret})
unless response.success?
return nil
else
return JSON.parse(response, {:symbolize_names => true})[:access_token]
end
end
def get_user_details
response = self.class.post(@@get_user_details_url, :body => {}.to_json, :query => {:api_key => @api_key}, :headers => @headers)
unless response.success?
return nil
else
return JSON.parse(response, {:symbolize_names => true})
end
end
end
my_account = SMapi.new( api_key = 'YOUR_API_KEY', access_token = 'YOUR_ACCESS_TOKEN' )
print my_account.get_user_details.to_json