deepsecurity 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,93 +2,80 @@
2
2
 
3
3
  module SavonHelper
4
4
 
5
+ # @abstract MappingObject is an abstract class providing methods to automatically convert from and to savon data.
5
6
  class MappingObject
6
- @@mappings = Hash.new()
7
7
 
8
- # @!group Request Helper
8
+ attr_reader :interface
9
9
 
10
- # Send a Request to the SOAP API for method +method_name+ with the data in +soap_body+ and unwrap the response
11
- def send_soap(method, message = {})
12
- retryable(:tries => 5, :on => Errno::ECONNRESET) do
13
- logger.debug { "#{self.class}\##{__method__}(#{method.inspect}, #{message.inspect})" }
14
- response = @client.call method, :message => message
15
- return response.to_hash[(method.to_s+"_response").to_sym][(method.to_s+"_return").to_sym]
16
- end
17
- end
10
+ extend SavonHelper::DSL
18
11
 
19
- # Helper Method deserializing the SOAP response into an object
20
- def request_object(method_name, object_class, arguments={})
21
- object_class.from_savon_data(send_soap(method_name, arguments))
22
- end
12
+ @@type_mappings = Hash.new { |hash, key| hash[key] = Hash.new }
23
13
 
24
- # Helper Method deserializing the SOAP response into an object
25
- def request_array(method_name, object_class, collection_name = nil, arguments={})
26
- data = send_soap(method_name, arguments)
27
- data = data[collection_name] unless collection_name.blank?
28
- SavonHelper::ArrayMapping.new(SavonHelper::ObjectMapping.new(object_class)).from_savon_data(data)
14
+ def initialize(interface=nil)
15
+ @interface = interface
29
16
  end
30
17
 
31
- # @!endgroup
18
+ BLACK_LIST = [:@interface]
32
19
 
33
- # @group Mapping
20
+ def to_s
21
+ public_vars = self.instance_variables.reject { |var|
22
+ BLACK_LIST.include? var
23
+ }.map { |var|
24
+ "#{var}=\"#{instance_variable_get(var)}\""
25
+ }.join(" ")
26
+
27
+ "<##{self.class}:#{self.object_id.to_s(8)} #{public_vars}>"
28
+ end
34
29
 
35
- # Return an initialized instance with the values from the (type-converted) hash. Store the instance in cache
36
- # if cacheable.
37
- # @see #store_in_cache
30
+ # @!group Mapping
31
+
32
+ # Return an initialized instance with the values from the (type-converted) hash.
38
33
  #
39
34
  # @param data [Hash] A hash of simple types as provided by Savon
40
35
  # @return [MappingObject] The initialized instance.
41
- def self.from_savon_data(data)
42
- instance = self.new()
43
- instance.fill_from_savon_data(data)
44
- instance.store_in_cache if instance.cachable?
36
+ def self.from_savon(data, interface)
37
+ instance = self.new(interface)
38
+ data.each do |key, value|
39
+ instance.instance_variable_set("@#{key}", self.map_to_native(key, value, interface))
40
+ end
45
41
  instance
46
42
  end
47
43
 
48
44
  # Return the instance as a hash of simple (type-converted) values suitable for Savon.
49
45
  #
50
- # @return [Hash] A Hash of simple types.
51
- def to_savon_data
46
+ # @return [Hash] A hash of simple types.
47
+ def to_savon
52
48
  hash = Hash.new()
53
- mappings.keys.each do |ivar_name|
54
- value = ivar_to_savon_data(ivar_name, instance_variable_get("@#{ivar_name}"))
49
+ type_mappings.keys.each do |ivar_name|
50
+ value = map_to_savon(ivar_name, instance_variable_get("@#{ivar_name}"))
55
51
  hash[ivar_name.to_sym] = value unless value.nil?
56
52
  end
57
53
  hash
58
54
  end
59
55
 
60
- # Set instance variables according to (type-converted) hash values
61
- # @param data [Hash] A hash of simple types as provided by Savon
62
- # @return [self]
63
- def fill_from_savon_data(data)
64
- data.each do |key, value|
65
- instance_variable_set("@#{key}", ivar_from_savon_data(key, value))
66
- end
67
- end
68
-
69
- # Return TypeMappings specific to the instance's class
56
+ # Return TypeMappings specific to the class
70
57
  # @return [Hash{Symbol => TypeMapping}]
71
- def mappings
72
- self.class.mappings
58
+ def self.type_mappings
59
+ @@type_mappings[self]
73
60
  end
74
61
 
75
- # Return TypeMappingschroch specific to the class
62
+ # Return TypeMappings.
76
63
  # @return [Hash{Symbol => TypeMapping}]
77
- def self.mappings
78
- mappings = @@mappings[self]
79
- return mappings if !mappings.nil?
80
- @@mappings[self] = Hash.new()
81
- @@mappings[self]
64
+ def self.all_type_mappings
65
+ self.superclass.all_type_mappings.merge(type_mappings())
82
66
  end
83
67
 
68
+ # Test if the given class understands the field definition
69
+ # @param field [String] A dot separeted list of accessors
70
+ # @return [Boolean]
84
71
  def self.has_attribute_chain(field)
85
72
  return true if self.method_defined?(field)
86
73
  current_class = self
87
74
  field.split('.').map(&:to_sym).all? do |each|
88
75
  # puts "Current Class: #{current_class.inspect}, Field: #{each}"
89
76
  if current_class.method_defined?(each)
90
- if current_class.respond_to?(:mappings)
91
- current_mapping = current_class.mappings[each]
77
+ if current_class.respond_to?(:type_mappings)
78
+ current_mapping = current_class.all_type_mappings[each]
92
79
  # puts "Mapping: #{current_mapping}"
93
80
  if !current_mapping.nil?
94
81
  current_class = current_mapping.object_klass
@@ -105,317 +92,80 @@ module SavonHelper
105
92
  end
106
93
  end
107
94
 
95
+ # Accessors defined by TypeMappings
96
+ # @return [Array<Symbol>]
108
97
  def self.defined_attributes()
109
- self.mappings.keys
98
+ self.type_mappings.keys
110
99
  end
111
100
 
101
+ # Convert the instance to a JSON representation
112
102
  def to_json(*a)
113
103
  result = {}
114
- self.mappings.keys.each { |key| result[key] = self.send(key).to_json }
104
+ self.type_mappings.keys.each { |key| result[key] = self.send(key).to_json }
115
105
  {
116
106
  'json_class' => self.class.name,
117
107
  'data' => result
118
108
  }.to_json(*a)
119
109
  end
120
110
 
121
- # @raise [MissingTypeMappingException] if no mapping for ivar_name can be found
122
- def ivar_from_savon_data(ivar_name, value)
123
- mapping = mappings[ivar_name]
124
- mapping = SavonHelper.define_missing_type_mapping(self.class, ivar_name, value, mappings) if mapping.nil?
125
- return nil if value.nil?
126
- mapping.from_savon_data(value)
127
- end
128
-
129
- def ivar_to_savon_data(ivar_name, value)
130
- mapping = mappings[ivar_name]
131
- mapping = SavonHelper.define_missing_type_mapping(self.class, ivar_name, value, mappings) if mapping.nil?
132
- return nil if value.nil?
133
- return mapping.to_savon_data(value)
134
- end
135
-
136
- # @group DSL to define attributes mapping
137
-
138
- # @macro [attach] attr_boolean_accessor
139
- # @!attribute [rw] $3
140
- # $2
141
- # @return [Boolean]
142
- # Define a new Boolean accessor
143
- # @param accessor [Symbol] The accessor to be created
144
- # @param description [String] The description for this accessor
145
- # @param alias_accessor [Symbol] An Alias for the accessor
146
- # @return [void]
147
- def self.attr_boolean_accessor(accessor, description='', alias_accessor=accessor)
148
- create_accessor(accessor, alias_accessor, BooleanMapping.new(description))
149
- end
150
-
151
- # @macro [attach] array_boolean_accessor
152
- # @!attribute [rw] $3
153
- # $2
154
- # @return [Array<Boolean>]
155
- # Define a new Boolean Array accessor
156
- # @param accessor [Symbol] The accessor to be created
157
- # @param description [String] The description for this accessor
158
- # @param alias_accessor [Symbol] An Alias for the accessor
159
- # @return [void]
160
- def self.array_boolean_accessor(accessor, description='', alias_accessor=accessor)
161
- create_accessor(accessor, alias_accessor, ArrayMapping.new(BooleanMapping.new, description))
162
- end
163
-
164
- # @macro [attach] attr_datetime_accessor
165
- # @!attribute [rw] $3
166
- # $2
167
- # @return [DateTime]
168
- # Define a new DateTime accessor
169
- # @param accessor [Symbol] The accessor to be created
170
- # @param description [String] The description for this accessor
171
- # @param alias_accessor [Symbol] An Alias for the accessor
172
- # @return [void]
173
- def self.attr_datetime_accessor(accessor, description='', alias_accessor=accessor)
174
- create_accessor(accessor, alias_accessor, DatetimeMapping.new(description))
175
- end
176
-
177
- # @macro [attach] array_datetime_accessor
178
- # @!attribute [rw] $3
179
- # $2
180
- # @return [Array<DateTime>]
181
- # Define a new DateTime Array accessor
182
- # @param accessor [Symbol] The accessor to be created
183
- # @param description [String] The description for this accessor
184
- # @param alias_accessor [Symbol] An Alias for the accessor
185
- # @return [void]
186
- def self.array_datetime_accessor(accessor, description='', alias_accessor=accessor)
187
- create_accessor(accessor, alias_accessor, ArrayMapping.new(DatetimeMapping.new, description))
188
- end
189
-
190
- # @macro [attach] attr_double_accessor
191
- # @!attribute [rw] $3
192
- # $2
193
- # @return [float]
194
- # Define a new Float accessor
195
- # @param accessor [Symbol] The accessor to be created
196
- # @param description [String] The description for this accessor
197
- # @param alias_accessor [Symbol] An Alias for the accessor
198
- # @return [void]
199
- def self.attr_double_accessor(accessor, description='', alias_accessor=accessor)
200
- create_accessor(accessor, alias_accessor, FloatMapping.new(description))
201
- end
202
-
203
- # @macro [attach] array_double__accessor
204
- # @!attribute [rw] $3
205
- # $2
206
- # @return [Array<float>]
207
- # Define a new Float Array accessor
208
- # @param accessor [Symbol] The accessor to be created
209
- # @param description [String] The description for this accessor
210
- # @param alias_accessor [Symbol] An Alias for the accessor
211
- # @return [void]
212
- def self.array_double_accessor(accessor, description='', alias_accessor=accessor)
213
- create_accessor(accessor, alias_accessor, ArrayMapping.new(FloatMapping.new, description))
214
- end
215
-
216
- # @macro [attach] attr_enum_accessor
217
- # @!attribute [rw] $4
218
- # $3
219
- # @return [$2]
220
- # Define a new Enum accessor
221
- # @param accessor [Symbol] The accessor to be created
222
- # @param enum [Enum] An hash of Enum to Symbol mappings
223
- # @param description [String] The description for this accessor
224
- # @param alias_accessor [Symbol] An Alias for the accessor
225
- # @return [void]
226
- def self.attr_enum_accessor(accessor, enum, description='', alias_accessor=accessor)
227
- create_accessor(accessor, alias_accessor, EnumMapping.new(enum, description))
228
- end
229
-
230
- # @macro [attach] array_enum_accessor
231
- # @!attribute [rw] $4
232
- # $3
233
- # @return [Array<$2>]
234
- # Define a new Enum Array accessor
235
- # @param accessor [Symbol] The accessor to be created
236
- # @param enum [Enum] An hash of Enum to Symbol mappings
237
- # @param description [String] The description for this accessor
238
- # @param alias_accessor [Symbol] An Alias for the accessor
239
- # @return [void]
240
- def self.array_enum_accessor(accessor, enum, description='', alias_accessor=accessor)
241
- create_accessor(accessor, alias_accessor, ArrayMapping.new(EnumMapping.new(enum), description))
242
- end
111
+ private
243
112
 
244
- # @macro [attach] attr_float_accessor
245
- # @!attribute [rw] $3
246
- # $2
247
- # @return [float]
248
- # Define a new Float accessor
249
- # @param accessor [Symbol] The accessor to be created
250
- # @param description [String] The description for this accessor
251
- # @param alias_accessor [Symbol] An Alias for the accessor
252
- # @return [void]
253
- def self.attr_float_accessor(accessor, description='', alias_accessor=accessor)
254
- create_accessor(accessor, alias_accessor, FloatMapping.new(description))
113
+ # Convert Savon data to ruby value.
114
+ # @param mapping_name [Symbol] The name of the instance variable
115
+ # @param data [Hash, Object]
116
+ # @return [Object]
117
+ def self.map_to_native(mapping_name, data, interface)
118
+ mapping = all_type_mappings[mapping_name]
119
+ mapping = SavonHelper.define_missing_type_mapping(self.class, mapping_name, data, type_mappings, interface) if mapping.nil?
120
+ return nil if data.nil?
121
+ mapping.to_native(data, interface)
255
122
  end
256
123
 
257
- # @macro [attach] array_float__accessor
258
- # @!attribute [rw] $3
259
- # $2
260
- # @return [Array<float>]
261
- # Define a new Float array accessor
262
- # @param accessor [Symbol] The accessor to be created
263
- # @param description [String] The description for this accessor
264
- # @param alias_accessor [Symbol] An Alias for the accessor
265
- # @return [void]
266
- def self.array_float__accessor(accessor, description='', alias_accessor=accessor)
267
- create_accessor(accessor, alias_accessor, ArrayMapping.new(FloatMapping.new, description))
268
- end
269
-
270
- # @macro [attach] attr_integer_accessor
271
- # @!attribute [rw] $3
272
- # $2
273
- # @return [int]
274
- # Define a new Integer accessor
275
- # @param accessor [Symbol] The accessor to be created
276
- # @param description [String] The description for this accessor
277
- # @param alias_accessor [Symbol] An Alias for the accessor
278
- # @return [void]
279
- def self.attr_integer_accessor(accessor, description='', alias_accessor=accessor)
280
- create_accessor(accessor, alias_accessor, IntegerMapping.new(description))
281
- end
282
-
283
- # @macro [attach] array_integer_accessor
284
- # @!attribute [rw] $3
285
- # $2
286
- # @return [Array<int>]
287
- # Define a new Integer Array accessor
288
- # @param accessor [Symbol] The accessor to be created
289
- # @param description [String] The description for this accessor
290
- # @param alias_accessor [Symbol] An Alias for the accessor
291
- # @return [void]
292
- def self.array_integer_accessor(accessor, description='', alias_accessor=accessor)
293
- create_accessor(accessor, alias_accessor, ArrayMapping.new(IntegerMapping.new, description))
294
- end
295
-
296
- # @macro [attach] attr_ip_address_accessor
297
- # @!attribute [rw] $3
298
- # $2
299
- # @return [int]
300
- # Define a new IP Address accessor
301
- # @param accessor [Symbol] The accessor to be created
302
- # @param description [String] The description for this accessor
303
- # @param alias_accessor [Symbol] An Alias for the accessor
304
- # @return [void]
305
- def self.attr_ip_address_accessor(accessor, description='', alias_accessor=accessor)
306
- create_accessor(accessor, alias_accessor, IPAddressMapping.new(description))
307
- end
308
-
309
- # @macro [attach] array_ip_address_accessor
310
- # @!attribute [rw] $3
311
- # $2
312
- # @return [Array<int>]
313
- # Define a new IP Address Array accessor
314
- # @param accessor [Symbol] The accessor to be created
315
- # @param description [String] The description for this accessor
316
- # @param alias_accessor [Symbol] An Alias for the accessor
317
- # @return [void]
318
- def self.array_ip_address_accessor(accessor, description='', alias_accessor=accessor)
319
- create_accessor(accessor, alias_accessor, ArrayMapping.new(IPAddressMapping.new, description))
320
- end
321
-
322
- # @macro [attach] attr_object_accessor
323
- # @!attribute [rw] $4
324
- # $3
325
- # @return [$2]
326
- # Define a new Object accessor
327
- # @param accessor [Symbol] The accessor to be created
328
- # @param klass [Class] The class of the accessed object
329
- # @param description [String] The description for this accessor
330
- # @param alias_accessor [Symbol] An Alias for the accessor
331
- # @return [void]
332
- def self.attr_object_accessor(accessor, klass, description='', alias_accessor=accessor)
333
- create_accessor(accessor, alias_accessor, ObjectMapping.new(klass, description))
334
- end
335
-
336
- # @macro [attach] array_object_accessor
337
- # @!attribute [rw] $4
338
- # $3
339
- # @return [Array<$2>]
340
- # Define a new Object Array accessor
341
- # @param accessor [Symbol] The accessor to be created
342
- # @param klass [Class] The class of the accessed object
343
- # @param description [String] The description for this accessor
344
- # @param alias_accessor [Symbol] An Alias for the accessor
345
- # @return [void]
346
- def self.array_object_accessor(accessor, klass, description='', alias_accessor=accessor)
347
- create_accessor(accessor, alias_accessor, ArrayMapping.new(ObjectMapping.new(klass, description)))
348
- end
349
-
350
- # @macro [attach] attr_string_accessor
351
- # @!attribute [rw] $3
352
- # $2
353
- # @return [String]
354
- # Define a new String accessor
355
- # @param accessor [Symbol] The accessor to be created
356
- # @param description [String] The description for this accessor
357
- # @param alias_accessor [Symbol] An Alias for the accessor
358
- # @return [void]
359
- def self.attr_string_accessor(accessor, description='', alias_accessor=accessor)
360
- create_accessor(accessor, alias_accessor, StringMapping.new(description))
124
+ # Convert Ruby value to Savon data.
125
+ # @param mapping_name [Symbol] The name of the instance variable
126
+ # @param value [Object]
127
+ # @return [Hash, Object]
128
+ def map_to_savon(mapping_name, value)
129
+ mapping = all_type_mappings[mapping_name]
130
+ mapping = SavonHelper.define_missing_type_mapping(self.class, mapping_name, value, type_mappings, interface) if mapping.nil?
131
+ return nil if value.nil?
132
+ mapping.to_savon(value)
361
133
  end
362
134
 
363
- # @macro [attach] array_string_accessor
364
- # @!attribute [rw] $3
365
- # $2
366
- # @return [Array<String>]
367
- # Define a new String Array accessor
368
- # @param accessor [Symbol] The accessor to be created
369
- # @param description [String] The description for this accessor
370
- # @param alias_accessor [Symbol] An Alias for the accessor
371
- # @return [void]
372
- def self.array_string_accessor(accessor, description='', alias_accessor=accessor)
373
- create_accessor(accessor, alias_accessor, ArrayMapping.new(StringMapping.new, description))
374
- end
135
+ end
375
136
 
376
- # @macro [attach] hint_object_accessor
377
- # @!attribute [rw] $4
378
- # $3
379
- # @return [$2]
380
- # Define a new "hint" for documentation purposes. Please note, that the method has to be define elsewhere!
381
- # @param accessor [Symbol] The accessor to be created
382
- # @param description [String] The description for this accessor
383
- # @param alias_accessor [Symbol] An Alias for the accessor
384
- # @return [void]
385
- def self.hint_object_accessor(accessor, klass, description='', alias_accessor=accessor)
386
- create_accessor(accessor, alias_accessor, HintMapping.new(klass, description))
387
- end
137
+ end
388
138
 
389
- # Create the specified accessor and it's alias
390
- # @param accessor [Symbol] The accessor
391
- # @param alias_accessor [Symbol] The accessor alias
392
- # @return [void]
393
- def self.create_accessor(accessor, alias_accessor, mapping)
394
- mappings[accessor] = mapping
395
- attr_accessor accessor
396
- create_alias(accessor, alias_accessor)
397
- end
139
+ class Object
398
140
 
399
- # Create the specified alias
400
- # @param accessor [Symbol] The accessor
401
- # @param alias_accessor [Symbol] The accessor alias
402
- # @return [void]
403
- def self.create_alias(accessor, alias_accessor)
404
- if alias_accessor != accessor
405
- alias_method alias_accessor, accessor
406
- alias_method alias_accessor.to_s + "=", accessor.to_s + "="
407
- end
408
- end
141
+ # Convert to Savon data.
142
+ def to_savon
143
+ self
144
+ end
409
145
 
410
- # @endgroup
146
+ # Return TypeMappings specific to the instance's class
147
+ # @return [Hash{Symbol => TypeMapping}]
148
+ def type_mappings
149
+ self.class.type_mappings
150
+ end
411
151
 
152
+ # Return TypeMappings specific to the class
153
+ # @return [Hash{Symbol => TypeMapping}]
154
+ def self.type_mappings
155
+ {}
412
156
  end
413
157
 
414
- end
158
+ # Return TypeMappings.
159
+ # @return [Hash{Symbol => TypeMapping}]
160
+ def all_type_mappings
161
+ self.class.all_type_mappings()
162
+ end
415
163
 
416
- class Object
417
- def to_savon_data
418
- self
164
+ # Return TypeMappings.
165
+ # @return [Hash{Symbol => TypeMapping}]
166
+ def self.all_type_mappings
167
+ {}
419
168
  end
169
+
420
170
  end
421
171