Class: Vindicia::SoapObject

Inherits:
Object
  • Object
show all
Includes:
Comparable, XMLBuilder
Defined in:
lib/vindicia.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from XMLBuilder

#build_array_xml, #build_tag_xml, #build_xml

Constructor Details

#initialize(arg = nil) ⇒ SoapObject

Returns a new instance of SoapObject.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/vindicia.rb', line 275

def initialize(arg=nil)
  case arg
  when String, nil
    arg = {"merchant#{classname}Id" => arg}
  when Array
    arg = Hash[arg]
  end

  arg.each do |key, value|
    if key == :type
      # XML->Hash conversion causes conflict between 'type' metadata
      # and 'type' data field in CreditCard (+others?)
      # so extract the value we want.
      value = [value].flatten.reject{|e|e =~ /:/}.first
      next if value.nil?
    end
    # skip metadata
    next if [:xmlns, :array_type].include? key
    type = attributes[camelcase(key.to_s)]
    cast_as_soap_object(type, value) do |obj|
      value = obj
    end

    key = underscore(key) # old camelCase back-compat
    instance_variable_set("@#{key}", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



366
367
368
369
370
371
372
373
374
375
# File 'lib/vindicia.rb', line 366

def method_missing(method, *args)
  attr = underscore(method.to_s).to_sym # back compatability from camelCase api
  key = camelcase(attr.to_s)

  if attributes[key]
    self[key]
  else
    super
  end
end

Instance Attribute Details

#request_statusObject

Returns the value of attribute request_status.



265
266
267
# File 'lib/vindicia.rb', line 265

def request_status
  @request_status
end

Instance Method Details

#[](attr) ⇒ Object



303
304
305
306
# File 'lib/vindicia.rb', line 303

def [](attr)
  key = camelcase(attr.to_s)
  Vindicia.coerce(key, attributes[key], instance_variable_get("@#{underscore(attr)}"))
end

#attributesObject



267
268
269
270
271
272
273
# File 'lib/vindicia.rb', line 267

def attributes
  @attributes ||= Vindicia.xsd(classname).inject({}) do |memo, attr|
    memo[attr["name"]] = attr["type"]
    memo["vid"] = attr["type"] if attr["name"] == "VID" # oh, casing
    memo
  end
end

#build(xml, tag) ⇒ Object



308
309
310
311
312
313
314
315
316
317
# File 'lib/vindicia.rb', line 308

def build(xml, tag)
  xml.tag!(tag, {"xsi:type" => "vin:#{classname}"}) do |xml|
    attributes.each do |name, type|
      next if name == 'vid'

      value = instance_variable_get("@#{underscore(name)}") || instance_variable_get("@#{name}")
      build_xml(xml, name, type, value)
    end
  end
end

#cast_as_soap_object(type, value) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/vindicia.rb', line 319

def cast_as_soap_object(type, value)
  return nil if type.nil? or value.nil?
  return value unless type =~ /tns:/

  if type =~ /ArrayOf/
    type = singularize(type.sub('ArrayOf',''))

    if value.kind_of?(Hash) && value[:array_type]
      key = value.keys - [:type, :array_type, :xmlns]
      value = value[key.first]
    end
    value = [value] unless value.kind_of? Array

    ary = value.map{|e| cast_as_soap_object(type, e) }
    yield ary if block_given?
    return ary
  end

  if klass = Vindicia.class(type)
    obj = klass.new(value)
    yield obj if block_given?
    return obj
  else
    value
  end
end

#classnameObject



346
347
348
# File 'lib/vindicia.rb', line 346

def classname
  self.class.to_s.split('::').last
end

#eachObject



350
351
352
353
354
355
# File 'lib/vindicia.rb', line 350

def each
  attributes.each do |attr, type|
    value = self.send(attr)
    yield attr, value if value
  end
end

#key?(k) ⇒ Boolean

Returns:

  • (Boolean)


357
358
359
# File 'lib/vindicia.rb', line 357

def key?(k)
  attributes.key?(k.to_s)
end

#refObject

TODO: respond_to?



379
380
381
382
383
# File 'lib/vindicia.rb', line 379

def ref
  key = instance_variable_get("@merchant#{classname}Id")
  ukey = instance_variable_get("@merchant_#{underscore(classname)}_id")
  {"merchant#{classname}Id" => ukey || key}
end

#to_hashObject



385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/vindicia.rb', line 385

def to_hash
  instance_variables.inject({}) do |result, ivar|
    name = ivar[1..-1]
    value = instance_variable_get(ivar)
    case value
    when SoapObject
      value = value.to_hash
    when Array
      value = value.map{|e| e.kind_of?(SoapObject) ? e.to_hash : e}
    end
    result[name] = value
    result
  end
end

#type(*args) ⇒ Object



361
362
363
364
# File 'lib/vindicia.rb', line 361

def type(*args)
  # type is deprecated, override so that it does a regular attribute lookup
  method_missing(:type, *args)
end