artirix_data_models 0.8.3 → 0.9.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: 2359922337aa2624392a98ae869724c2a3afa640
4
- data.tar.gz: ec9c7dfcc6fe58d603176940e0e0e4f39314fc41
3
+ metadata.gz: 713d558fd789e3af896c2621897a347ec1493f34
4
+ data.tar.gz: 95f5c0b37d134fb771f28442000849a9b605011c
5
5
  SHA512:
6
- metadata.gz: 08cce0d7e14055cc719a79a202266becd424dc9a53689e00c45d788531bd9928f6880a597f28fd9b8c91cf780f33daab2fafefb713c83edfe421b90355046eb9
7
- data.tar.gz: b68d7c20dc2741561c482917edd897abd97bb80d8bc77d0e9a9a27790a855ca3625657d38a271fa76a12dfa633ba53d0c17039e5f5f8195a4f24524dcde792e9
6
+ metadata.gz: 549184151747d15673c69d9798adce02d61f8afe80d850c34f33d4f14755105a0b51ce7f03bfd771c8ea3b9ea14ea4c4dd8f9022f42b18051711d90f1d02331a
7
+ data.tar.gz: eb5c9f12149a61af0db1218ca221da1a8816be668194e429511b4210abd4e1446967476f95534010870b477a6e176f8b1d7608502fc601ebea7d30b40c238e03
data/README.md CHANGED
@@ -250,6 +250,23 @@ end
250
250
 
251
251
  ## Changes
252
252
 
253
+ ### 0.9.0
254
+
255
+ - Fake Responses now can be a callable object (if it responds to `call` it will invoke it)
256
+ - refactor in `ArtirixDataModels::DataGateway` to add more info into the exceptions
257
+ - `ArtirixDataModels::DataGateway::Error` and subclasses have now `path`, `method`, `response_status`, `response_body` (when applicable) and also `json_response_body` method which will try to parse `response_body` as if it were json (nil if it is not present or if it is not a valid json)
258
+ - `ArtirixDataModels::DataGateway::Error` subclasses now for specific response status:
259
+ -- `NotFound`
260
+ -- `NotAcceptable`
261
+ -- `UnprocessableEntity`
262
+ -- `Unauthorized`
263
+ -- `Forbidden`
264
+ -- `RequestTimeout`
265
+ -- `TooManyRequests`
266
+ -- `ServerError`
267
+
268
+ note: `ParseError` will not have the `response_status`
269
+
253
270
  ### 0.8.3
254
271
 
255
272
  - `DataGateway` refactor, plus adding `put` and `delete` support.
@@ -24,13 +24,17 @@ class ArtirixDataModels::DataGateway
24
24
 
25
25
  def call(method, path, json_body: true, response_adaptor: nil, body: nil, fake: false, fake_response: nil, cache_adaptor: nil, **_ignored_options)
26
26
  if fake
27
- response_body = fake_response
27
+ result = fake_response.respond_to?(:call) ? fake_response.call : fake_response
28
28
  elsif cache_adaptor.present?
29
- response_body = cache_adaptor.call { perform(method, path, body, json_body) }
29
+ result = cache_adaptor.call { perform(method, path, body, json_body) }
30
30
  else
31
- response_body = perform(method, path, body, json_body)
31
+ result = perform(method, path, body, json_body)
32
32
  end
33
- parse_response(response_body, response_adaptor)
33
+
34
+ parse_response result: result,
35
+ response_adaptor: response_adaptor,
36
+ method: method,
37
+ path: path
34
38
  end
35
39
 
36
40
  private
@@ -70,17 +74,14 @@ class ArtirixDataModels::DataGateway
70
74
  end
71
75
  end
72
76
  rescue Faraday::ConnectionFailed => e
73
- raise ConnectionError, "method: #{method}, path: #{path}, error: #{e}"
77
+ raise ConnectionError.new(path: path, method: method), "method: #{method}, path: #{path}, error: #{e}"
74
78
  end
75
79
 
76
80
  def treat_response(response, method, path)
77
- if response.success?
78
- response.body
79
- elsif response.status.to_i == 404
80
- raise NotFound, path
81
- else
82
- raise GatewayError, "method: #{method}, path: #{path}, status: #{response.status}, body: #{response.body}"
83
- end
81
+ return response.body if response.success?
82
+
83
+ klass = exception_for_status(response.status)
84
+ raise klass.new(path: path, method: method, response_body: response.body, response_status: response.status)
84
85
  end
85
86
 
86
87
  def body_to_json(body)
@@ -92,7 +93,7 @@ class ArtirixDataModels::DataGateway
92
93
  end
93
94
  end
94
95
 
95
- def parse_response(result, response_adaptor)
96
+ def parse_response(result:, response_adaptor:, path:, method:)
96
97
  if result.present?
97
98
  parsed_response = Oj.load result, symbol_keys: true
98
99
  else
@@ -106,7 +107,30 @@ class ArtirixDataModels::DataGateway
106
107
  end
107
108
 
108
109
  rescue Oj::ParseError => e
109
- raise ParseError, "response: #{result}, #{e}"
110
+ raise ParseError.new(path: path, method: method, response_body: result), e.message
111
+ end
112
+
113
+ def exception_for_status(response_status)
114
+ case response_status.to_i
115
+ when 404
116
+ NotFound
117
+ when 406
118
+ NotAcceptable
119
+ when 422
120
+ UnprocessableEntity
121
+ when 401
122
+ Unauthorized
123
+ when 403
124
+ Forbidden
125
+ when 408
126
+ RequestTimeout
127
+ when 429
128
+ TooManyRequests
129
+ when 500
130
+ ServerError
131
+ else
132
+ GatewayError
133
+ end
110
134
  end
111
135
 
112
136
  module DefaultConnectionLoader
@@ -143,9 +167,38 @@ class ArtirixDataModels::DataGateway
143
167
  end
144
168
 
145
169
  class Error < StandardError
146
- end
170
+ attr_reader :path, :method, :response_status, :response_body
147
171
 
148
- class NotFound < Error
172
+ def initialize(path: nil, method: nil, response_status: nil, response_body: nil)
173
+ @path = path
174
+ @method = method
175
+ @response_status = response_status
176
+ @response_body = response_body
177
+ end
178
+
179
+ def json_response_body
180
+ return nil unless response_body.present?
181
+
182
+ Oj.load response_body, symbol_keys: true
183
+
184
+ rescue Oj::Error # in case it's not json
185
+ nil
186
+ end
187
+
188
+ def to_s
189
+ msg = super
190
+ msg = nil if msg == self.class.to_s
191
+
192
+ parts = {
193
+ path: path,
194
+ method: method,
195
+ response_status: response_status,
196
+ response_body: response_body,
197
+ message: msg,
198
+ }.select { |_, v| v.present? }.map { |k, v| "#{k}: #{v.inspect}" }
199
+
200
+ "#{self.class}: #{parts.join ', '}"
201
+ end
149
202
  end
150
203
 
151
204
  class ParseError < Error
@@ -154,6 +207,47 @@ class ArtirixDataModels::DataGateway
154
207
  class GatewayError < Error
155
208
  end
156
209
 
210
+ ###########################################
211
+ # SPECIAL, not subclasses of GatewayError #
212
+ ###########################################
213
+
214
+ # 404
215
+ class NotFound < Error
216
+ end
217
+
218
+ # 406
219
+ class NotAcceptable < Error
220
+ end
221
+
222
+ # 422
223
+ class UnprocessableEntity < Error
224
+ end
225
+
226
+ ##############################
227
+ # subclasses of GatewayError #
228
+ ##############################
229
+
230
+ # 401
231
+ class Unauthorized < GatewayError
232
+ end
233
+
234
+ # 403
235
+ class Forbidden < GatewayError
236
+ end
237
+
238
+ # 408
239
+ class RequestTimeout < GatewayError
240
+ end
241
+
242
+ # 429
243
+ class TooManyRequests < GatewayError
244
+ end
245
+
246
+ # 500
247
+ class ServerError < GatewayError
248
+ end
249
+
250
+ # generic error
157
251
  class ConnectionError < GatewayError
158
252
  end
159
253
  end
@@ -1,3 +1,3 @@
1
1
  module ArtirixDataModels
2
- VERSION = '0.8.3'
2
+ VERSION = '0.9.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artirix_data_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-22 00:00:00.000000000 Z
11
+ date: 2016-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport