surveymonkey 0.2.2 → 0.3.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: 6b64132d5d1febf1ea23a672a432bda1c3f87db7
4
- data.tar.gz: f956f7dfba34dd84c1d2ebd858ffd1c336301063
3
+ metadata.gz: 6e1ba04b72f387956b57379112fcc3d0b2af52bc
4
+ data.tar.gz: 646d1fbe102a8c982f8bf6d360e602dd790577bb
5
5
  SHA512:
6
- metadata.gz: a708772e0e48a25ac487c2fdae8904d91a97b3e77893459a9b69b8a8066f96f33123d0001351942c4b7e2f222dc59c71c58a6ad2e74d5329888d237a7cace4f4
7
- data.tar.gz: 0ba7c59b97a3595a275965bc9c320e5766ee2a1aacd7d38faa9bf8673a81179d3bb12d0ba97fe5858335710b30d3b0fedba7a353c61022ab5d346c61a71cae8b
6
+ metadata.gz: 84eb3f064ff7f5538fb07c0d542616a88e60230360d6f56bdfcd6f7716adbae616bc035e1472fb693dcc9959372047e617f58b17a21e7f7807fcc5aaf915b29c
7
+ data.tar.gz: 93123b3ba30cf464513bd35f603fe1b93bcf3d19f5add1e7db5f8475cdf0637d51757ece7bdf774f91197aae3237590aa3a6c0a70618b6e7b1c68f7b12b9cc08
data/README.md CHANGED
@@ -67,6 +67,8 @@ To pass parameters to an API method, pass a hash as the `method_params` paramete
67
67
 
68
68
  API method responses are parsed into Ruby data structures; do with them as you think best.
69
69
 
70
+ SurveyMonkey API responses include a `status` attribute. The possible values of this attribute are documented upstream; `Surveymonkey::Client` inspects each API response and raises a `Surveymonkey::Error` if the status code is other than 0.
71
+
70
72
  ## Development
71
73
 
72
74
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,54 @@
1
+ require 'surveymonkey/logging'
2
+
3
+ class Surveymonkey::Error < StandardError
4
+ # constants
5
+
6
+ ##
7
+ # API status codes, documented upstream.
8
+
9
+ Status_codes = {
10
+ 0 => 'Success',
11
+ 1 => 'Not Authenticated',
12
+ 2 => 'Invalid User Credentials',
13
+ 3 => 'Invalid Request',
14
+ 4 => 'Unknown User',
15
+ 5 => 'System Error'
16
+ }
17
+
18
+ attr_reader :status, :status_name, :status_codes, :errmsg
19
+
20
+ ##
21
+ # Create a new Surveymonkey::Error object. Pass in the hash parsed from the
22
+ # JSON object returned by the API.
23
+
24
+ def initialize(error = {}, status_codes = Status_codes)
25
+ begin
26
+ @status_codes = status_codes
27
+ @status = error.fetch('status', 0)
28
+ @errmsg = error.fetch('errmsg', '')
29
+ @status_name = _status_name(@status)
30
+
31
+ rescue StandardError => e
32
+ $log.error(sprintf("%s: unable to parse '%s' as error\n", __method__, error.inspect))
33
+ raise
34
+ end
35
+ end
36
+
37
+ ##
38
+ # Stringify a Surveymonkey::Error object.
39
+
40
+ def to_s
41
+ sprintf("Error %i (%s): %s", self.status, self.status_name, self.errmsg)
42
+ end
43
+
44
+ private
45
+
46
+ def _status_name(error) # :nodoc:
47
+ begin
48
+ self.status_codes.fetch(error)
49
+ rescue StandardError => e
50
+ $log.error(sprintf("%s: %i is not a valid error code\n", __method__, error))
51
+ raise
52
+ end
53
+ end
54
+ end
@@ -1,6 +1,7 @@
1
1
  require 'surveymonkey/logging'
2
2
  require 'surveymonkey/api'
3
3
  require 'surveymonkey/client'
4
+ require 'surveymonkey/error'
4
5
 
5
6
  ##
6
7
  # Object representing a request to the SurveyMonkey API. Parameters should all be populated automatically.
@@ -46,7 +47,15 @@ class Surveymonkey::Request
46
47
  $log.debug(sprintf("%s: response code %i\n", __method__, response.code))
47
48
  $log.debug(sprintf("%s: response headers '%s'\n", __method__, response.headers.inspect))
48
49
 
49
- response.parsed_response
50
+ parsed = response.parsed_response
51
+ status = parsed.fetch('status')
52
+
53
+ if status == 0
54
+ parsed
55
+ else
56
+ $log.debug(sprintf("%s: API returned status %i\n", __method__, status))
57
+ raise Surveymonkey::Error.new(parsed)
58
+ end
50
59
 
51
60
  rescue StandardError => e
52
61
  $log.error(sprintf("%s: unable to execute API request: %s\n", __method__, e.message))
@@ -2,5 +2,5 @@
2
2
  # Specify the version of the surveymonkey gem.
3
3
 
4
4
  module Surveymonkey
5
- VERSION = "0.2.2"
5
+ VERSION = "0.3.0"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surveymonkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Huff
@@ -158,6 +158,7 @@ files:
158
158
  - lib/surveymonkey/api.rb
159
159
  - lib/surveymonkey/api/method.rb
160
160
  - lib/surveymonkey/client.rb
161
+ - lib/surveymonkey/error.rb
161
162
  - lib/surveymonkey/logging.rb
162
163
  - lib/surveymonkey/request.rb
163
164
  - lib/surveymonkey/version.rb