sfrest 0.0.14 → 0.0.15

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
  SHA256:
3
- metadata.gz: 08936ab1f2d5f8780cba9b3c74884806896886bd6c26210e3d3f69de44f6f510
4
- data.tar.gz: 299199ecf27db665e2dacd3300a0a033251baf26a0964852592fb22a24d5dbf9
3
+ metadata.gz: 728d8a47e5b6c93f0dd88576f75b24d2e8cdd7a6a5aed22d89517d6835d5501d
4
+ data.tar.gz: c738283f50fe09d31b714791b44a3ec9130578e61f93b21687382a988254c6a6
5
5
  SHA512:
6
- metadata.gz: f76a2ec444911abdaa7c0eaac3d3c2797822f6e79c9b75096d7d7565ddd27151e3be0907779b78f1fc27fe3f0428dd48342f77aa690fbd2deb79cf064003494c
7
- data.tar.gz: 5283104b05712b69a454d5a5b5c8e181b90f4168c53c8ce35a436283d61d9fd2b1e2dd35c9ad1e00f1493b90be5f414f57294d5d49573d08c7a89314e4348f3b
6
+ metadata.gz: a6663190218e40d64e6f0e6c327fe459a148d9eb8439e4d11590b44afa817d37f5ef1c91f7badfd346ac7451394bf3ea74caa3fe9fa7ab6dd4055c5b21b9ea7a
7
+ data.tar.gz: b1966f5b32571d9838106c4023dffaaec226683dccbba70375f8d921fe223643185bd028670ed616d2048fa23b4ecca5cf3605c5997eb1f88f30118a30d7e7dd
@@ -16,8 +16,8 @@ module SFRest
16
16
  # http request via get
17
17
  # @param [string] uri
18
18
  # @return [Object] ruby representation of the json response
19
- # if the reponsebody does not parse, returns
20
- # the non-parsed body
19
+ # if the reponse body does not parse, raises
20
+ # a SFRest::InvalidResponse
21
21
  def get(uri)
22
22
  headers = { 'Content-Type' => 'application/json' }
23
23
  res = Excon.get(@base_url + uri.to_s,
@@ -25,18 +25,14 @@ module SFRest
25
25
  user: username,
26
26
  password: password,
27
27
  ssl_verify_peer: false)
28
- begin
29
- access_check JSON(res.body), res.status
30
- rescue JSON::ParserError
31
- res.body
32
- end
28
+ api_response res
33
29
  end
34
30
 
35
31
  # http request via get
36
32
  # @param [string] uri
37
33
  # @return [Integer, Object] http status and the ruby representation
38
- # of the json response if the reponse body
39
- # does not parse, returns the non-parsed body
34
+ # if the reponse body does not parse, raises
35
+ # a SFRest::InvalidResponse
40
36
  def get_with_status(uri)
41
37
  headers = { 'Content-Type' => 'application/json' }
42
38
  res = Excon.get(@base_url + uri.to_s,
@@ -44,19 +40,14 @@ module SFRest
44
40
  user: username,
45
41
  password: password,
46
42
  ssl_verify_peer: false)
47
- begin
48
- data = access_check JSON(res.body), res.status
49
- return res.status, data
50
- rescue JSON::ParserError
51
- return res.status, res.body
52
- end
43
+ api_response res, true
53
44
  end
54
45
 
55
46
  # http request via post
56
47
  # @param [string] uri
57
48
  # @return [Object] ruby representation of the json response
58
- # if the reponsebody does not parse, returns
59
- # the non-parsed body
49
+ # if the reponse body does not parse, raises
50
+ # a SFRest::InvalidResponse
60
51
  def post(uri, payload)
61
52
  headers = { 'Content-Type' => 'application/json' }
62
53
  res = Excon.post(@base_url + uri.to_s,
@@ -65,18 +56,14 @@ module SFRest
65
56
  password: password,
66
57
  ssl_verify_peer: false,
67
58
  body: payload)
68
- begin
69
- access_check JSON(res.body), res.status
70
- rescue JSON::ParserError
71
- res.body
72
- end
59
+ api_response res
73
60
  end
74
61
 
75
62
  # http request via put
76
63
  # @param [string] uri
77
64
  # @return [Object] ruby representation of the json response
78
- # if the reponsebody does not parse, returns
79
- # the non-parsed body
65
+ # if the reponse body does not parse, raises
66
+ # a SFRest::InvalidResponse
80
67
  def put(uri, payload)
81
68
  headers = { 'Content-Type' => 'application/json' }
82
69
  res = Excon.put(@base_url + uri.to_s,
@@ -85,18 +72,14 @@ module SFRest
85
72
  password: password,
86
73
  ssl_verify_peer: false,
87
74
  body: payload)
88
- begin
89
- access_check JSON(res.body), res.status
90
- rescue JSON::ParserError
91
- res.body
92
- end
75
+ api_response res
93
76
  end
94
77
 
95
78
  # http request via delete
96
79
  # @param [string] uri
97
80
  # @return [Object] ruby representation of the json response
98
- # if the reponsebody does not parse, returns
99
- # the non-parsed body
81
+ # if the reponse body does not parse, raises
82
+ # a SFRest::InvalidResponse
100
83
  def delete(uri)
101
84
  headers = { 'Content-Type' => 'application/json' }
102
85
  res = Excon.delete(@base_url + uri.to_s,
@@ -104,11 +87,22 @@ module SFRest
104
87
  user: username,
105
88
  password: password,
106
89
  ssl_verify_peer: false)
107
- begin
108
- access_check JSON(res.body), res.status
109
- rescue JSON::ParserError
110
- res.body
111
- end
90
+ api_response res
91
+ end
92
+
93
+ # Confirm that the result looks adequate
94
+ # @param [Excon::Response] res
95
+ # @param [Boolean] return_status If true returns the integer status
96
+ # with the reponse
97
+ # @return [Array|Object] if return_status then [int, Object]
98
+ # else Object
99
+ def api_response(res, return_status = false)
100
+ data = access_check JSON(res.body), res.status
101
+ ret_data = return_status ? [res.status, data] : data
102
+ return ret_data
103
+ rescue JSON::ParserError
104
+ message = "Invalid data, status #{res.status}, body: #{res.body}"
105
+ raise SFRest::InvalidResponse, message
112
106
  end
113
107
 
114
108
  # Throws an SFRest exception for requests that have problems
@@ -123,7 +117,9 @@ module SFRest
123
117
  #
124
118
  # The cyclomatic complexity check is being ignored here because we are
125
119
  # collecting all the possible exception raising cases.
126
- def access_check(data, http_status) # rubocop:disable Metrics/CyclomaticComplexity
120
+ # rubocop: disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
121
+ def access_check(data, http_status)
122
+ raise SFRest::EmptyResult if data.size.zero?
127
123
  if data.is_a?(Hash) && !data['message'].nil?
128
124
  case data['message']
129
125
  when /Access denied|Access Denied/
@@ -142,6 +138,7 @@ module SFRest
142
138
  end
143
139
  data
144
140
  end
141
+ # rubocop: enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
145
142
 
146
143
  # pings the SF api as an authenticated user
147
144
  # responds with a pong
data/lib/sfrest/error.rb CHANGED
@@ -22,4 +22,10 @@ module SFRest
22
22
 
23
23
  # if you get in valid data
24
24
  class InvalidDataError < SFRest::SFError; end
25
+
26
+ # If the return seems to contain no data
27
+ class EmptyResult < SFRest::SFError; end
28
+
29
+ # If the return cannot be parsed into something useful
30
+ class InvalidResponse < SFRest::SFError; end
25
31
  end
@@ -1,4 +1,4 @@
1
1
  module SFRest
2
2
  # Just tracks the version of sfrest.
3
- VERSION = '0.0.14'.freeze
3
+ VERSION = '0.0.15'.freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sfrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - ACSF Engineering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-13 00:00:00.000000000 Z
11
+ date: 2018-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon