surveymonkey 0.1.3 → 0.2.0

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: bcc8b046283c6458cefb32ba467347378219a96b
4
- data.tar.gz: 5c193620b02c368f19bf86c2030d1fc8da6e03bc
3
+ metadata.gz: c0c1572387413d53da2a0a44985440a8ea47270f
4
+ data.tar.gz: 692049f25149f4e8794f099be218fa76fc75b130
5
5
  SHA512:
6
- metadata.gz: da13f7310d7db4eb8eefcd2c4c37dd89a8afd7aeaf67b94e1c9b45cdffe3a7894db6322bd2b33d334b47b6a467fc2973fd364c962c3f2ecc35fd4c4c9c6b0fe9
7
- data.tar.gz: 2715e724f7aaaffbae8b85731b2d2a300df2af2329d2b6dac58f0994865cb913a84498d6f1654f93b91330f9ec7ae6e1d33a90a1b8ff6a15bb13e3001fb5011e
6
+ metadata.gz: cd15fc54ca2937595c44f09a12c96f2fbf1ed5c14c3842f1a8bf4563267cb7d502e5cc74172628920fb93e68eb672c08c0bf28f2ac82d8ca324b6e6cff6068b2
7
+ data.tar.gz: 7f05a0f14e09ff41e7c68b57b038141ffffba22ffe30c7832e1fe327c71ec140623692e50dd85e9351930226bc8fb78865b541c778e53d98dedd3a28402ee13a
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Surveymonkey [![Build Status](https://travis-ci.org/hakamadare/rubygem-surveymonkey.svg?branch=master)](https://travis-ci.org/hakamadare/rubygem-surveymonkey)
1
+ # Surveymonkey [![Build Status](https://travis-ci.org/hakamadare/rubygem-surveymonkey.svg?branch=master)](https://travis-ci.org/hakamadare/rubygem-surveymonkey) [![Gem Version](https://badge.fury.io/rb/surveymonkey.svg)](http://badge.fury.io/rb/surveymonkey)
2
2
 
3
3
  This is a client for the SurveyMonkey [RESTful API](http://developer.surveymonkey.com).
4
4
 
@@ -50,9 +50,9 @@ The SurveyMonkey API methods are documented [here](https://developer.surveymonke
50
50
  {"username"=>"XXXXXX", "is_paid_account"=>true, "is_enterprise_user"=>false}}}
51
51
  ```
52
52
 
53
- To pass parameters to an API method, pass a hash like so:
53
+ To pass parameters to an API method, pass a hash as the `method_params` parameter like so:
54
54
  ```ruby
55
- [2] pry(main)> Surveymonkey.get_survey_list({"page_size" => 5, "order_asc" => true})
55
+ [2] pry(main)> Surveymonkey.get_survey_list('method_params' => {"page_size" => 5, "order_asc" => true})
56
56
  => {"status"=>0,
57
57
  "data"=>
58
58
  {"surveys"=>
@@ -0,0 +1,30 @@
1
+ require 'surveymonkey/logging'
2
+
3
+ class Surveymonkey::API::Method
4
+ attr_reader :path, :http_method
5
+
6
+ def initialize(path, http_method = 'post')
7
+ begin
8
+ $log.debug(sprintf("%s: enter", __method__))
9
+
10
+ # FIXME validate the path
11
+ @path = path
12
+
13
+ # validate the method
14
+ $log.debug(sprintf("%s:http_method: '%s'\n", __method__, http_method))
15
+ the_method = http_method.to_s.downcase
16
+ $log.debug(sprintf("%s:the_method: '%s'\n", __method__, the_method))
17
+
18
+ if the_method =~ /^(get|post|patch|put|delete|move|copy|head|options)$/
19
+ @http_method = the_method
20
+ $log.debug(sprintf("%s: method: %s", __method__, the_method))
21
+ else
22
+ raise StandardError, "'#{the_method}' is not a valid HTTP method", caller
23
+ end
24
+
25
+ rescue StandardError => e
26
+ $log.error(sprintf("%s: unable to initialize API method: %s\n", __method__, e.message))
27
+ raise
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,96 @@
1
+ require 'surveymonkey/logging'
2
+ require 'json'
3
+
4
+ class Surveymonkey::API
5
+ autoload :Method, 'surveymonkey/api/method'
6
+
7
+ # constants
8
+
9
+ Api_methods = {
10
+ 'create_flow' => {
11
+ 'path' => '/v2/batch/create_flow',
12
+ },
13
+ 'send_flow' => {
14
+ 'path' => '/v2/batch/send_flow',
15
+ },
16
+ 'create_collector' => {
17
+ 'path' => '/v2/collectors/create_collector',
18
+ },
19
+ 'get_survey_list' => {
20
+ 'path' => '/v2/surveys/get_survey_list',
21
+ },
22
+ 'get_survey_details' => {
23
+ 'path' => '/v2/surveys/get_survey_details',
24
+ },
25
+ 'get_collector_list' => {
26
+ 'path' => '/v2/surveys/get_collector_list',
27
+ },
28
+ 'get_respondent_list' => {
29
+ 'path' => '/v2/surveys/get_respondent_list',
30
+ },
31
+ 'get_responses' => {
32
+ 'path' => '/v2/surveys/get_responses',
33
+ },
34
+ 'get_response_counts' => {
35
+ 'path' => '/v2/surveys/get_response_counts',
36
+ },
37
+ 'get_template_list' => {
38
+ 'path' => '/v2/templates/get_template_list',
39
+ },
40
+ 'get_user_details' => {
41
+ 'path' => '/v2/user/get_user_details',
42
+ },
43
+ }
44
+
45
+ # public methods
46
+ attr_reader :api_methods
47
+
48
+ def api_method(key, api_methods = self.api_methods)
49
+ begin
50
+ $log.debug(sprintf("%s: api methods: %s\n", __method__, api_methods.inspect))
51
+ $log.debug(sprintf("%s: fetching '%s' from API methods\n", __method__, key))
52
+ value = api_methods.fetch(key)
53
+ $log.debug(sprintf("%s: retrieved '%s'\n", __method__, value.inspect))
54
+
55
+ path = value['path']
56
+ $log.debug(sprintf("%s: path '%s'\n", __method__, path))
57
+ method = (value['method'] || 'post')
58
+ $log.debug(sprintf("%s: method '%s'\n", __method__, method))
59
+
60
+ # return
61
+ Surveymonkey::API::Method.new(path, method)
62
+
63
+ rescue KeyError => e
64
+ $log.error(sprintf("%s: '%s' not found in api methods\n", __method__, key))
65
+ raise e
66
+ rescue StandardError => e
67
+ $log.error(sprintf("%s: %s\n", __method__, e.message))
68
+ raise
69
+ end
70
+ end
71
+
72
+ def api_method_params(method_params)
73
+ begin
74
+ # TODO validate params against API spec
75
+ $log.debug(sprintf("%s: parsing api method params from '%s'\n", __method__, method_params))
76
+ the_params = (method_params.kind_of?(String) ? method_params : JSON.generate(method_params || {}))
77
+ $log.debug(sprintf("%s: parsed method params '%s'\n", __method__, the_params))
78
+
79
+ # return
80
+ the_params
81
+
82
+ rescue StandardError => e
83
+ $log.error(sprintf("%s: %s\n", __method__, e.message))
84
+ raise
85
+ end
86
+ end
87
+
88
+ def initialize
89
+ begin
90
+ @api_methods = Api_methods
91
+ rescue StandardError => e
92
+ $log.error(sprintf("%s: %s\n", __method__, e.message))
93
+ raise
94
+ end
95
+ end
96
+ end
@@ -9,91 +9,15 @@ class Surveymonkey::Client
9
9
  $log.debug("Defined Surveymonkey::Client.")
10
10
 
11
11
  # constants
12
- Baseuri = 'https://api.surveymonkey.net'
13
-
14
- Api_methods = {
15
- 'create_flow' => {
16
- 'path' => '/v2/batch/create_flow',
17
- },
18
- 'send_flow' => {
19
- 'path' => '/v2/batch/send_flow',
20
- },
21
- 'create_collector' => {
22
- 'path' => '/v2/collectors/create_collector',
23
- },
24
- 'get_survey_list' => {
25
- 'path' => '/v2/surveys/get_survey_list',
26
- },
27
- 'get_survey_details' => {
28
- 'path' => '/v2/surveys/get_survey_details',
29
- },
30
- 'get_collector_list' => {
31
- 'path' => '/v2/surveys/get_collector_list',
32
- },
33
- 'get_respondent_list' => {
34
- 'path' => '/v2/surveys/get_respondent_list',
35
- },
36
- 'get_responses' => {
37
- 'path' => '/v2/surveys/get_responses',
38
- },
39
- 'get_response_counts' => {
40
- 'path' => '/v2/surveys/get_response_counts',
41
- },
42
- 'get_template_list' => {
43
- 'path' => '/v2/templates/get_template_list',
44
- },
45
- 'get_user_details' => {
46
- 'path' => '/v2/user/get_user_details',
47
- },
48
- }
49
12
 
50
13
  # public methods
51
- attr_reader :baseuri, :api_key
52
- attr_accessor :access_token
53
-
54
- def api_call(api_method, method_params = {}, api_key = self.api_key, access_token = self.access_token)
55
- begin
56
- $log.debug(sprintf("%s: calling '%s'\n", __method__, api_method))
57
-
58
- the_api_method = _api_method(api_method)
59
-
60
- body = _api_method_params(method_params)
61
-
62
- path = the_api_method.fetch('path')
63
-
64
- http_method = the_api_method.fetch('method', 'post')
65
-
66
- $log.debug(sprintf("%s: %s '%s' '%s'\n", __method__, http_method, path, body))
67
-
68
- http_headers = _http_headers(access_token)
69
- $log.debug(sprintf("%s: http_headers: '%s'\n", __method__, http_headers.inspect))
70
-
71
- request_uri = _request_uri(path, api_key)
72
-
73
- $log.debug(sprintf("%s: ready to make request for '%s'\n", __method__, api_method))
74
- response = self.class.send(http_method.to_sym, request_uri, body: body, headers: http_headers)
75
-
76
- $log.debug(sprintf("%s: response class %s\n", __method__, response.class))
77
- $log.debug(sprintf("%s: response code %i\n", __method__, response.code))
78
- $log.debug(sprintf("%s: response headers '%s'\n", __method__, response.headers.inspect))
14
+ attr_reader :baseuri, :api_key, :access_token
79
15
 
80
- response.parsed_response
81
-
82
- rescue KeyError => e
83
- $log.error(sprintf("%s: no such method '%s': %s\n", __method__, http_method, e.message))
84
- raise e
85
- rescue Exception => e
86
- $log.error(sprintf("%s: %s\n", __method__, e.message))
87
- raise
88
- end
89
- end
90
-
91
- def initialize(*args)
16
+ def initialize(baseuri, access_token, api_key)
92
17
  begin
93
- param_hash = args.shift || {}
94
- @baseuri = param_hash.fetch('baseuri', Baseuri)
95
- @access_token = param_hash.fetch('access_token', _from_env('SURVEYMONKEY_ACCESSTOKEN'))
96
- @api_key = param_hash.fetch('api_key', _from_env('SURVEYMONKEY_APIKEY'))
18
+ @baseuri = baseuri
19
+ @access_token = access_token
20
+ @api_key = api_key
97
21
 
98
22
  self.class.logger $log, :debug
99
23
 
@@ -101,12 +25,10 @@ class Surveymonkey::Client
101
25
  self.class.base_uri @baseuri
102
26
 
103
27
  $log.debug(sprintf("%s: setting headers'\n", __method__))
104
- the_headers = {
105
- "Content-Type" => "application/json",
106
- "Authorization" => sprintf("bearer %s", @access_token),
107
- }
108
- self.class.headers the_headers
109
- rescue Exception => e
28
+ http_headers = _http_headers(@access_token)
29
+ self.class.headers http_headers
30
+
31
+ rescue StandardError => e
110
32
  $log.error(sprintf("%s: %s\n", __method__, e.message))
111
33
  raise
112
34
  end
@@ -115,22 +37,6 @@ class Surveymonkey::Client
115
37
  # private methods
116
38
  private
117
39
 
118
- def _api_method(key, api_methods = Api_methods)
119
- begin
120
- $log.debug(sprintf("%s: fetching '%s' from api methods\n", __method__, key))
121
- value = api_methods.fetch(key)
122
- $log.debug(sprintf("%s: retrieved '%s'\n", __method__, value.inspect))
123
- value
124
-
125
- rescue KeyError => e
126
- $log.error(sprintf("%s: '%s' not found in api methods\n", __method__, key))
127
- raise e
128
- rescue Exception => e
129
- $log.error(sprintf("%s: %s\n", __method__, e.message))
130
- raise
131
- end
132
- end
133
-
134
40
  def _http_headers(token)
135
41
  begin
136
42
  $log.debug(sprintf("%s: constructing http headers with token '%s'\n", __method__, token))
@@ -141,49 +47,7 @@ class Surveymonkey::Client
141
47
  $log.debug(sprintf("%s: http headers: '%s'\n", __method__, http_headers))
142
48
  http_headers
143
49
 
144
- rescue Exception => e
145
- $log.error(sprintf("%s: %s\n", __method__, e.message))
146
- raise
147
- end
148
- end
149
-
150
- def _api_method_params(method_params)
151
- begin
152
- # TODO validate params against API spec
153
- $log.debug(sprintf("%s: parsing api method params from '%s'\n", __method__, method_params))
154
- the_params = JSON.generate(method_params || {}).to_s
155
- $log.debug(sprintf("%s: parsed method params '%s'\n", __method__, the_params))
156
- the_params
157
-
158
- rescue Exception => e
159
- $log.error(sprintf("%s: %s\n", __method__, e.message))
160
- raise
161
- end
162
- end
163
-
164
- def _request_uri(path, api_key)
165
- begin
166
- $log.debug(sprintf("%s: generating request uri fragment from '%s' and '%s'\n", __method__, path, api_key))
167
- request_uri = sprintf("%s?api_key=%s", path, api_key)
168
- $log.debug(sprintf("%s: generated '%s'\n", __method__, request_uri))
169
- request_uri
170
-
171
- rescue Exception => e
172
- $log.error(sprintf("%s: %s\n", __method__, e.message))
173
- raise
174
- end
175
- end
176
-
177
- def _from_env(key)
178
- begin
179
- $log.debug(sprintf("%s: fetching '%s' from environment\n", __method__, key))
180
- value = ENV.fetch(key)
181
- $log.debug(sprintf("%s: retrieved '%s'\n", __method__, value))
182
- value
183
-
184
- rescue KeyError => e
185
- $log.info(sprintf("%s: '%s' not found in environment\n", __method__, key))
186
- rescue Exception => e
50
+ rescue StandardError => e
187
51
  $log.error(sprintf("%s: %s\n", __method__, e.message))
188
52
  raise
189
53
  end
@@ -0,0 +1,142 @@
1
+ require 'surveymonkey/logging'
2
+ require 'surveymonkey/api'
3
+ require 'surveymonkey/client'
4
+
5
+ class Surveymonkey::Request
6
+
7
+ begin
8
+ attr_reader :api, :api_method, :client, :path, :api_key, :baseuri
9
+ attr_accessor :access_token, :method_params
10
+
11
+ # constants
12
+
13
+ Baseuri = 'https://api.surveymonkey.net'
14
+
15
+ # public methods
16
+ def execute(method_params = self.method_params, api_method = self.api_method, api_key = self.api_key, access_token = self.access_token)
17
+ begin
18
+ $log.debug(sprintf("%s: enter\n", __method__))
19
+
20
+ self.method_params=(self.api.api_method_params(method_params))
21
+ method_params = self.method_params
22
+ $log.debug(sprintf("%s: method_params: %s\n", __method__, method_params))
23
+
24
+ path = api_method.path
25
+ $log.debug(sprintf("%s: path: %s\n", __method__, path))
26
+
27
+ http_method = api_method.http_method
28
+ $log.debug(sprintf("%s: http_method: %s\n", __method__, http_method))
29
+
30
+ request_uri = _request_uri(path, api_key)
31
+ $log.debug(sprintf("%s: ready to make request for '%s'\n", __method__, api_method))
32
+
33
+ response = self.client.class.send(http_method.to_sym, request_uri, body: self.method_params)
34
+
35
+ $log.debug(sprintf("%s: response class %s\n", __method__, response.class))
36
+ $log.debug(sprintf("%s: response code %i\n", __method__, response.code))
37
+ $log.debug(sprintf("%s: response headers '%s'\n", __method__, response.headers.inspect))
38
+
39
+ response.parsed_response
40
+
41
+ rescue StandardError => e
42
+ $log.error(sprintf("%s: unable to execute API request: %s\n", __method__, e.message))
43
+ raise
44
+ end
45
+ end
46
+
47
+ def initialize(api_method, *args)
48
+ begin
49
+ $log.debug(sprintf("%s: enter\n", __method__))
50
+ $log.debug(sprintf("%s: api_method: %s\n", __method__, api_method))
51
+ $log.debug(sprintf("%s: args: %s\n", __method__, args))
52
+
53
+ api = Surveymonkey::API.new
54
+ @api_method = api.api_method(api_method)
55
+
56
+ # extract optional params
57
+ param_hash = Hash.try_convert(args.shift) || {}
58
+ @baseuri = param_hash.fetch('baseuri', Baseuri)
59
+ @method_params = api.api_method_params(param_hash.fetch('method_params', {}))
60
+ @access_token = param_hash.fetch('access_token', _from_env('SURVEYMONKEY_ACCESSTOKEN'))
61
+ @api_key = param_hash.fetch('api_key', _from_env('SURVEYMONKEY_APIKEY'))
62
+
63
+ # configure the client
64
+ @client = Surveymonkey::Client.new(baseuri = @baseuri, access_token = @access_token, api_key = @api_key)
65
+
66
+ # configure the API
67
+ @api = Surveymonkey::API.new
68
+
69
+ rescue StandardError => e
70
+ $log.error(sprintf("%s: unable to initialize API request: %s\n", __method__, e.message))
71
+ raise
72
+ end
73
+ end
74
+
75
+ # private methods
76
+ private
77
+
78
+ def _client
79
+ begin
80
+ @client = Surveymonkey::Client.new()
81
+ rescue StandardError => e
82
+ $log.fatal(sprintf("%s: %s\n", "Unable to initialize REST client", e.message))
83
+ $log.debug(sprintf("%s: %s\n", __method__, e.message))
84
+ raise
85
+ end
86
+ end
87
+
88
+ def _api
89
+ begin
90
+ @api = Surveymonkey::API.new()
91
+ rescue StandardError => e
92
+ $log.fatal(sprintf("%s: %s\n", "Unable to initialize SurveyMonkey API", e.message))
93
+ $log.debug(sprintf("%s: %s\n", __method__, e.message))
94
+ raise
95
+ end
96
+ end
97
+
98
+ def _http_headers(token)
99
+ begin
100
+ $log.debug(sprintf("%s: constructing http headers with token '%s'\n", __method__, token))
101
+ http_headers = {
102
+ "Content-Type" => "application/json",
103
+ "Authorization" => sprintf("bearer %s", token),
104
+ }
105
+ $log.debug(sprintf("%s: http headers: '%s'\n", __method__, http_headers))
106
+ http_headers
107
+
108
+ rescue Exception => e
109
+ $log.error(sprintf("%s: %s\n", __method__, e.message))
110
+ raise
111
+ end
112
+ end
113
+
114
+ def _from_env(key)
115
+ begin
116
+ $log.debug(sprintf("%s: fetching '%s' from environment\n", __method__, key))
117
+ value = ENV.fetch(key)
118
+ $log.debug(sprintf("%s: retrieved '%s'\n", __method__, value))
119
+ value
120
+
121
+ rescue KeyError => e
122
+ $log.info(sprintf("%s: '%s' not found in environment\n", __method__, key))
123
+ rescue Exception => e
124
+ $log.error(sprintf("%s: %s\n", __method__, e.message))
125
+ raise
126
+ end
127
+ end
128
+
129
+ def _request_uri(path, api_key)
130
+ begin
131
+ $log.debug(sprintf("%s: generating request uri fragment from '%s' and '%s'\n", __method__, path, api_key))
132
+ request_uri = sprintf("%s?api_key=%s", path, api_key)
133
+ $log.debug(sprintf("%s: generated '%s'\n", __method__, request_uri))
134
+ request_uri
135
+
136
+ rescue StandardError => e
137
+ $log.error(sprintf("%s: %s\n", __method__, e.message))
138
+ raise
139
+ end
140
+ end
141
+ end
142
+ end
@@ -1,3 +1,3 @@
1
1
  module Surveymonkey
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/surveymonkey.rb CHANGED
@@ -2,9 +2,9 @@ require "logging"
2
2
 
3
3
  require "surveymonkey/version"
4
4
  require "surveymonkey/logging"
5
+ require "surveymonkey/request"
5
6
 
6
7
  module Surveymonkey
7
- autoload :Client, "surveymonkey/client"
8
8
 
9
9
  class << self
10
10
  # Constants
@@ -15,17 +15,19 @@ module Surveymonkey
15
15
  $log.debug(sprintf("%s: %s\n", __method__, 'enter'))
16
16
 
17
17
  method_params = Hash(Array(args).shift) || {}
18
+ $log.debug(sprintf("%s: method_params: %s\n", __method__, method_params.inspect))
18
19
 
19
- response = _client.send(:api_call, method_name.to_s, method_params)
20
+ request = Surveymonkey::Request.new(method_name.to_s, method_params)
21
+ response = request.execute
20
22
  response
21
23
 
22
24
  rescue TypeError => e
23
- $log.fatal(sprintf("%s: method parameters must be a hash\n"))
25
+ $log.fatal(sprintf("%s: method parameters must be a hash\n", __method__))
24
26
  exit 1
25
27
  rescue KeyError => e
26
- $log.fatal(sprintf("%s: method '%s' not implemented\n"))
28
+ $log.fatal(sprintf("%s: method '%s' not implemented\n", __method__, method_name.to_s))
27
29
  exit 1
28
- rescue Exception => e
30
+ rescue StandardError => e
29
31
  $log.error(sprintf("%s: %s\n", __method__, e.message))
30
32
  raise
31
33
  end
@@ -34,15 +36,5 @@ module Surveymonkey
34
36
  # Private methods
35
37
  private
36
38
 
37
- def _client
38
- begin
39
- @client = Surveymonkey::Client.new()
40
- rescue Exception => e
41
- $log.fatal(sprintf("%s: %s\n", "Unable to initialize REST client", e.message))
42
- $log.debug(sprintf("%s: %s\n", __method__, e.message))
43
- raise
44
- end
45
- end
46
39
  end
47
-
48
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surveymonkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Huff
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-01 00:00:00.000000000 Z
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -141,9 +141,12 @@ files:
141
141
  - bin/console
142
142
  - bin/setup
143
143
  - lib/surveymonkey.rb
144
+ - lib/surveymonkey/api.rb
145
+ - lib/surveymonkey/api/method.rb
144
146
  - lib/surveymonkey/client.rb
145
147
  - lib/surveymonkey/config.rb
146
148
  - lib/surveymonkey/logging.rb
149
+ - lib/surveymonkey/request.rb
147
150
  - lib/surveymonkey/version.rb
148
151
  - surveymonkey.gemspec
149
152
  homepage: http://developer.surveymonkey.com/