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.



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/vindicia.rb', line 282

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



373
374
375
376
377
378
379
380
381
382
# File 'lib/vindicia.rb', line 373

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.



272
273
274
# File 'lib/vindicia.rb', line 272

def request_status
  @request_status
end

Instance Method Details

#[](attr) ⇒ Object



310
311
312
313
# File 'lib/vindicia.rb', line 310

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

#attributesObject



274
275
276
277
278
279
280
# File 'lib/vindicia.rb', line 274

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



315
316
317
318
319
320
321
322
323
324
# File 'lib/vindicia.rb', line 315

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



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/vindicia.rb', line 326

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



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

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

#eachObject



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

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

#key?(k) ⇒ Boolean

Returns:

  • (Boolean)


364
365
366
# File 'lib/vindicia.rb', line 364

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

#refObject

TODO: respond_to?



386
387
388
389
390
# File 'lib/vindicia.rb', line 386

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



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/vindicia.rb', line 392

def to_hash
  instance_variables.inject({}) do |result, ivar|
    name = ivar[1..-1]
    next result if name == 'attributes'
    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



368
369
370
371
# File 'lib/vindicia.rb', line 368

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