mastercard_api_core 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 944203e87ee923b74bf1ff75a6bdfc7197cd4b32
4
- data.tar.gz: 6fbf644b3b92a6a58165da9fe1f14ce654c211f9
3
+ metadata.gz: eaace8e42ca4512e5c646aeb38a80e618f24ac15
4
+ data.tar.gz: 2103ad5fd5f01b2c32463eac4f2cebc8e1619bfd
5
5
  SHA512:
6
- metadata.gz: e873d63a12ca97afe302b75f12182509d882650717e0a8eb216807acbab89046d3c8c11add4c50bb87a520f849ec9424b4b863274101e86008744ffe3f29e1ed
7
- data.tar.gz: 9f98769a65e6682c31c468e4d25eef3b80fe5c9d1f9f546fda3478a9b1699f0e9b0ba9ccc16689b82eb1cb22c0e10b134246efa6dbf4ce7ddae89b5aea95049c
6
+ metadata.gz: bbfecc3d34189d931472d46032f186305aea93451d966f8095e1a30876438902c61141eab82239613de84b2fa1255e1357a9188d64f341b9005ce3b9f5a53bf3
7
+ data.tar.gz: 1b29b096869b4b6048db37068c1d851b183b4fb1fe546bbf3385bd744db0906765e87bda82bd18362e7a6e6c859365de0750be9467cae4936d8ca8a8c11293a9
@@ -0,0 +1,87 @@
1
+ require "mastercard/core/controller"
2
+ require "mastercard/core/model"
3
+
4
+ module MasterCard
5
+ module Core
6
+ module Model
7
+ ################################################################################
8
+ # BaseObject
9
+ ################################################################################
10
+
11
+ class BaseObject < RequestMap
12
+ include MasterCard::Core::Controller
13
+ include MasterCard::Core::Model
14
+
15
+ def initialize(requestMap = nil)
16
+
17
+ #call the base class constructor
18
+ super()
19
+
20
+ unless requestMap.nil?
21
+ setAll(requestMap.getObject())
22
+ end
23
+
24
+ end
25
+
26
+
27
+ protected
28
+
29
+ def self.getOperationConfig(uuid)
30
+ raise NotImplementedError.new("Child class must define getOperationConfig method to use this class")
31
+ end
32
+
33
+ def self.getOperationMetadata()
34
+ raise NotImplementedError.new("Child class must define getOperationMetadata method to use this class")
35
+ end
36
+
37
+ def self.execute(operationUUID,inputObject)
38
+
39
+
40
+ config = inputObject.class.getOperationConfig(operationUUID)
41
+ metadata = inputObject.class.getOperationMetadata()
42
+
43
+ response = APIController.new.execute(config,metadata,inputObject.getObject())
44
+ returnObjClass = inputObject.class
45
+
46
+ if config.getAction().upcase == APIController::ACTION_LIST
47
+ returnObj = []
48
+
49
+ if response.is_a?(Hash) && response.key?(RequestMap::KEY_LIST)
50
+ response = response[RequestMap::KEY_LIST]
51
+ end
52
+
53
+ if response.is_a?(Hash)
54
+
55
+ response.each do |key,value|
56
+
57
+ requestMap = RequestMap.new
58
+ requestMap.setAll(value)
59
+ returnObj.push(returnObjClass.new(requestMap))
60
+
61
+ end
62
+
63
+ elsif response.is_a?(Array)
64
+
65
+ response.each do |value|
66
+ requestMap = RequestMap.new
67
+ requestMap.setAll(value)
68
+ returnObj.push(returnObjClass.new(requestMap))
69
+
70
+ end
71
+
72
+ end
73
+ return returnObj
74
+ else
75
+
76
+ requestMap = RequestMap.new
77
+ requestMap.setAll(response)
78
+ return returnObjClass.new(requestMap)
79
+
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+ end
86
+ end
87
+ end
@@ -34,6 +34,8 @@ module MasterCard
34
34
  module Environment
35
35
  PRODUCTION = "production"
36
36
  SANDBOX = "sandbox"
37
+ SANDBOX_MTF = "sandbox_mtf"
38
+ SANDBOX_ITF = "sandbox_itf"
37
39
  STAGE = "stage"
38
40
  DEV = "dev"
39
41
  PRODUCTION_MTF = "production_mtf"
@@ -45,6 +47,8 @@ module MasterCard
45
47
  MAPPING = {
46
48
  "production" => ["https://api.mastercard.com", nil],
47
49
  "sandbox" => ["https://sandbox.api.mastercard.com", nil],
50
+ "sandbox_mtf" => ["https://sandbox.api.mastercard.com", "mtf"],
51
+ "sandbox_itf" => ["https://sandbox.api.mastercard.com", "itf"],
48
52
  "stage" => ["https://stage.api.mastercard.com", nil],
49
53
  "dev" => ["https://dev.api.mastercard.com", nil],
50
54
  "production_mtf" => ["https://api.mastercard.com", "mtf"],
@@ -24,6 +24,9 @@
24
24
  # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
25
  # SUCH DAMAGE.
26
26
  #
27
+
28
+ require 'mastercard/core/model'
29
+
27
30
  module MasterCard
28
31
  module Core
29
32
  module Exceptions
@@ -32,69 +35,80 @@ module MasterCard
32
35
  # APIException
33
36
  ################################################################################
34
37
  class APIException < StandardError
38
+ include MasterCard::Core::Model
35
39
  #
36
40
  # Base Class for all the API exceptions
37
- def initialize(message,status=nil, error_data=nil)
41
+ def initialize(message,http_status=nil, error_data=nil)
38
42
  #Call the base class constructor
39
43
  super(message)
40
44
 
41
45
  @message = message
42
- @status = status
43
- @error_data = error_data
44
- @error_code = nil
46
+ @http_status = http_status
47
+ @raw_error_data = error_data
48
+ @reason_code = nil
49
+ @source = nil
50
+ @description = nil
45
51
 
46
52
  #If error_data is not nil set the appropriate message
47
53
  unless error_data.nil?
48
54
 
49
- error_hash = Hash.new
50
- # If error_data is of type hash and has Key 'Errors' which has a key 'Error'
51
- if error_data.is_a?(Hash) && error_data.key?("Errors") && error_data["Errors"].key?("Error")
55
+ @raw_error_data = SmartMap.new
56
+ @raw_error_data.setAll(error_data);
52
57
 
53
- error_hash = error_data["Errors"]["Error"]
58
+ error_data_case_insensitive = parseMap(error_data)
59
+
60
+ # If error_data is of type hash and has Key 'Errors' which has a key 'Error'
61
+ if error_data_case_insensitive.key?("errors") && error_data_case_insensitive["errors"].key?("error")
62
+ error_hash = error_data_case_insensitive["errors"]["error"]
54
63
 
55
64
  #Case of multiple errors take the first one
56
- if error_hash.is_a?(Array)
65
+ if error_hash.is_a?(Hash)
66
+ initDataFromHash(error_hash)
67
+
68
+ #If error Data is of Type Array
69
+ elsif error_hash.is_a?(Array)
70
+ #Take the first error
57
71
  error_hash = error_hash[0]
72
+ initDataFromHash(error_hash)
58
73
  end
59
-
60
- initDataFromHash(error_hash)
61
-
62
- #If error Data is of Type Array
63
- elsif error_data.is_a?(Array)
64
- #Take the first error
65
- error_hash = error_data[0]
66
- initDataFromHash(error_hash)
67
74
  end
68
75
  end
69
-
70
76
  end
71
77
 
72
78
  def getMessage()
73
- return @message
79
+ if @description.nil?
80
+ return @message
81
+ else
82
+ return @description
83
+ end
84
+ end
85
+
86
+ def getHttpStatus()
87
+ return @http_status
74
88
  end
75
89
 
76
- def getStatus()
77
- return @status
90
+ def getReasonCode()
91
+ return @reason_code
78
92
  end
79
93
 
80
- def getErrorCode()
81
- return @error_code
94
+ def getSource()
95
+ return @source
82
96
  end
83
97
 
84
- def getErrorData()
85
- return @error_data
98
+ def getRawErrorData()
99
+ return @raw_error_data
86
100
  end
87
101
 
88
102
  def describe()
89
103
  exception = self.class.name
90
104
  exception << ": \""
91
105
  exception << getMessage()
92
- exception << "\" (status: "
93
- exception << "%s" % getStatus()
94
- errorCode = getErrorCode()
106
+ exception << "\" (http_status: "
107
+ exception << "%s" % getHttpStatus()
108
+ errorCode = getReasonCode()
95
109
  unless errorCode.nil?
96
- exception << ", error code: "
97
- exception << "%s" % getErrorCode()
110
+ exception << ", reason_code: "
111
+ exception << "%s" % getReasonCode()
98
112
  end
99
113
  exception << ")"
100
114
  return exception
@@ -107,219 +121,40 @@ module MasterCard
107
121
  private
108
122
 
109
123
  def initDataFromHash(error_hash)
110
- @error_code = error_hash.fetch("ReasonCode","")
111
- @message = error_hash.fetch("Message",@message)
112
- end
113
-
114
- end
115
-
116
- ################################################################################
117
- # ApiConnectionException
118
- ################################################################################
119
-
120
- class APIConnectionException < APIException
121
- #
122
- #Exception raised when there are communication problems contacting the API.
123
- def initialize(message=nil,status=nil,error_data=nil)
124
-
125
-
126
- if status.nil?
127
- status = 500
128
- end
129
-
130
- #Call the base class constructor
131
- super(message,status,error_data)
132
- end
133
- end
134
-
135
-
136
- ################################################################################
137
- # ObjectNotFoundException
138
- ################################################################################
139
-
140
- class ObjectNotFoundException < APIException
141
- #
142
- #Exception raised when the endpoint does not exist.
143
- def initialize(message=nil,status=nil,error_data=nil)
144
-
145
-
146
- if status.nil?
147
- status = 404
148
- end
149
-
150
- #Call the base class constructor
151
- super(message,status,error_data)
152
- end
153
- end
154
-
155
- ################################################################################
156
- # AuthenticationException
157
- ################################################################################
158
-
159
- class AuthenticationException < APIException
160
- #
161
- #Exception raised where there are problems authenticating a request.
162
- def initialize(message=nil,status=nil,error_data=nil)
163
-
164
-
165
- if status.nil?
166
- status = 401
167
- end
168
-
169
- #Call the base class constructor
170
- super(message,status,error_data)
171
- end
172
- end
173
-
174
- ################################################################################
175
- # InvalidRequestException
176
- ################################################################################
177
-
178
- class InvalidRequestException < APIException
179
- #
180
- #Exception raised when the API request contains errors.
181
-
182
- def initialize(message=nil,status=nil,error_data=nil)
183
-
184
- if status.nil?
185
- status = 400
186
- end
187
-
188
- #Call the base class constructor
189
- super(message,status,error_data)
190
-
191
- @fieldErrors = []
192
-
193
- #If error_data is not nil set the appropriate message
194
- unless error_data.nil?
195
-
196
- error_hash = Hash.new
197
- # If error_data is of type hash and has Key 'Errors' which has a key 'Error'
198
- if error_data.is_a?(Hash) && error_data.key?("Errors") && error_data["Errors"].key?("Error")
199
-
200
- error_hash = error_data["Errors"]["Error"]
201
-
202
- #Case of multiple errors take the first one
203
- if error_hash.is_a?(Array)
204
- error_hash = error_hash[0]
205
- end
206
-
207
- initFieldDataFromHash(error_hash)
208
-
209
- #If error Data is of Type Array
210
- elsif error_data.is_a?(Array)
211
- #Take the first error
212
- error_hash = error_data[0]
213
- initFieldDataFromHash(error_hash)
124
+ @reason_code = error_hash.fetch("reasoncode",nil)
125
+ @description = error_hash.fetch("description",@message)
126
+ @source = error_hash.fetch("source",nil)
127
+ end
128
+
129
+ def parseMap(map)
130
+ parsedMap = {}
131
+ map.each do |k, v|
132
+ if v.is_a?(Array)
133
+ parsedMap[k.to_s.downcase] = parseList(v)
134
+ elsif v.is_a?(Hash)
135
+ parsedMap[k.to_s.downcase] = parseMap(v)
136
+ else
137
+ parsedMap[k.to_s.downcase] = v
214
138
  end
215
139
  end
216
- end
217
-
218
-
219
- def hasFieldErrors
220
- return @fieldErrors.length != 0
221
- end
222
-
223
- def getFieldErrors
224
- return @fieldErrors
225
- end
226
-
227
- def describe
228
- des = super()
229
- @fieldErrors.each do |field_error|
230
- des << "\n #{field_error}"
231
- end
232
- return des
233
- end
234
-
235
- def to_s
236
- return "%s"%describe()
237
- end
238
-
239
- private
240
-
241
- def initFieldDataFromHash(error_hash)
242
- if error_hash.key?("FieldErrors")
243
- error_hash.fetch("FieldErrors").each do |field_error|
244
- @fieldErrors << "%s"%FieldError.new(field_error)
140
+ return parsedMap
141
+ end
142
+
143
+ def parseList(l)
144
+ parsedList = []
145
+ l.each do |v|
146
+ if v.is_a?(Array)
147
+ parsedList << parseList(v)
148
+ elsif v.is_a?(Hash)
149
+ parsedList << parseMap(v)
150
+ else
151
+ parsedList << v
245
152
  end
246
153
  end
154
+ return parsedList
247
155
  end
248
156
 
249
-
250
-
251
157
  end
252
-
253
- ################################################################################
254
- # NotAllowedException
255
- ################################################################################
256
-
257
- class NotAllowedException < APIException
258
- #
259
- #Exception when a request was not allowed.
260
- def initialize(message=nil,status=nil,error_data=nil)
261
-
262
-
263
- if status.nil?
264
- status = 403
265
- end
266
-
267
- #Call the base class constructor
268
- super(message,status,error_data)
269
-
270
- end
271
- end
272
-
273
-
274
- ################################################################################
275
- # SystemException
276
- ################################################################################
277
-
278
- class SystemException < APIException
279
- #
280
- #Exception when there was a system error processing a request.
281
- def initialize(message=nil,status=nil,error_data=nil)
282
-
283
-
284
- if status.nil?
285
- status = 500
286
- end
287
-
288
- #Call the base class constructor
289
- super(message,status,error_data)
290
- end
291
- end
292
-
293
- ################################################################################
294
- # FieldError
295
- ################################################################################
296
-
297
- class FieldError
298
-
299
- def initialize(params)
300
-
301
- @name = params.fetch("field","")
302
- @message = params.fetch("message","")
303
- @code = params.fetch("code")
304
- end
305
-
306
- def getFieldName
307
- return @name
308
- end
309
-
310
- def getErrorMessage
311
- return @message
312
- end
313
-
314
- def getErrorCode
315
- return @code
316
- end
317
-
318
- def to_s
319
- return "Field Error: #{@name} \"#{@message}\" (#{@code})"
320
- end
321
- end
322
-
323
158
  end
324
159
  end
325
160
  end
@@ -24,16 +24,14 @@
24
24
  # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
25
  # SUCH DAMAGE.
26
26
  #
27
- require "mastercard/core/controller"
28
-
29
27
  module MasterCard
30
28
  module Core
31
29
  module Model
32
30
  ################################################################################
33
- # RequestMap
31
+ # SmartMap
34
32
  ################################################################################
35
33
 
36
- class RequestMap
34
+ class SmartMap
37
35
 
38
36
  KEY_LIST = "list"
39
37
 
@@ -400,83 +398,18 @@ module MasterCard
400
398
  end
401
399
 
402
400
  ################################################################################
403
- # BaseObject
401
+ # RequestMap
404
402
  ################################################################################
405
403
 
406
- class BaseObject < RequestMap
407
- include MasterCard::Core::Controller
408
-
409
- def initialize(requestMap = nil)
410
-
404
+ class RequestMap < SmartMap
405
+ def initialize
411
406
  #call the base class constructor
412
407
  super()
413
-
414
- unless requestMap.nil?
415
- setAll(requestMap.getObject())
416
- end
417
-
418
- end
419
-
420
-
421
- protected
422
-
423
- def self.getOperationConfig(uuid)
424
- raise NotImplementedError.new("Child class must define getOperationConfig method to use this class")
425
- end
426
-
427
- def self.getOperationMetadata()
428
- raise NotImplementedError.new("Child class must define getOperationMetadata method to use this class")
429
- end
430
-
431
- def self.execute(operationUUID,inputObject)
432
-
433
-
434
- config = inputObject.class.getOperationConfig(operationUUID)
435
- metadata = inputObject.class.getOperationMetadata()
436
-
437
- response = APIController.new.execute(config,metadata,inputObject.getObject())
438
- returnObjClass = inputObject.class
439
-
440
- if config.getAction().upcase == APIController::ACTION_LIST
441
- returnObj = []
442
-
443
- if response.is_a?(Hash) && response.key?(RequestMap::KEY_LIST)
444
- response = response[RequestMap::KEY_LIST]
445
- end
446
-
447
- if response.is_a?(Hash)
448
-
449
- response.each do |key,value|
450
-
451
- requestMap = RequestMap.new
452
- requestMap.setAll(value)
453
- returnObj.push(returnObjClass.new(requestMap))
454
-
455
- end
456
-
457
- elsif response.is_a?(Array)
458
-
459
- response.each do |value|
460
- requestMap = RequestMap.new
461
- requestMap.setAll(value)
462
- returnObj.push(returnObjClass.new(requestMap))
463
-
464
- end
465
-
466
- end
467
- return returnObj
468
- else
469
-
470
- requestMap = RequestMap.new
471
- requestMap.setAll(response)
472
- return returnObjClass.new(requestMap)
473
-
474
- end
475
-
476
408
  end
477
-
478
409
  end
479
410
 
411
+
412
+
480
413
 
481
414
  end
482
415
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mastercard_api_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - MasterCard Worldwide
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-04 00:00:00.000000000 Z
11
+ date: 2017-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simplecov
@@ -46,6 +46,7 @@ executables: []
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - lib/mastercard/core/baseobject.rb
49
50
  - lib/mastercard/core/config.rb
50
51
  - lib/mastercard/core/constants.rb
51
52
  - lib/mastercard/core/controller.rb