class UpdateConnector

Public Class Methods

new(name) click to toggle source

Constructor, takes the connector name

# File lib/afasgem/updateconnector.rb, line 4
def initialize(name)
        @connectorname = name
        if Afasgem::debug
                # Build a debug client if the debug flag is set
                @client = Savon.client(
                        wsdl: Afasgem::updateconnector_url,
                        log: true,
                        log_level: :debug,
                        pretty_print_xml: true
                )
        else
                # Build a normal client otherwise
                @client = Savon.client(wsdl: Afasgem::updateconnector_url)
        end
end

Public Instance Methods

client() click to toggle source

Method to return the savon client for this constructor

# File lib/afasgem/updateconnector.rb, line 21
def client
        return @client
end
delete(objecthash) click to toggle source

Executes a delete action using the passed object hash

# File lib/afasgem/updateconnector.rb, line 38
def delete(objecthash)
        xml = build_xml(objecthash, 'delete')
        return execute(xml)
end
insert(objecthash) click to toggle source

Executes an insert action using the passed object hash

# File lib/afasgem/updateconnector.rb, line 26
def insert(objecthash)
        xml = build_xml(objecthash, 'insert')
        return execute(xml)
end
update(objecthash) click to toggle source

Executes an update action using the passed object hash

# File lib/afasgem/updateconnector.rb, line 32
def update(objecthash)
        xml = build_xml(objecthash, 'update')
        return execute(xml)
end

Private Instance Methods

build_nested_xml(xml, objects, action) click to toggle source

Builds the xml for nested objects

# File lib/afasgem/updateconnector.rb, line 63
def build_nested_xml(xml, objects, action)
        xml.Objects {
                objects.each do |obj, values|

                        xml.send(obj) {
                                xml.Element {
                                        xml.Fields(Action: action) {
                                                values.each do |k, v|
                                                        xml.send(k, v)
                                                end
                                        }
                                }
                        }
                end
        }
end
build_xml(objecthash, action) click to toggle source

Builds the inner xml to send to the afas api from the passed hash and action

# File lib/afasgem/updateconnector.rb, line 46
def build_xml(objecthash, action)
        builder = Nokogiri::XML::Builder.new do |xml|
                xml.send(@connectorname.to_sym, 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance") {
                        xml.Element {
                                xml.Fields(Action: action) {
                                        objecthash.each do |k, v|
                                                xml.send(k.to_sym, v) unless k.to_s == 'Objects'
                                                build_nested_xml(xml, v, action) if k.to_s == 'Objects'
                                        end
                                }
                        }
                }
        end
        return builder.to_xml.to_s
end
execute(xml) click to toggle source

Actually calls the afas api

# File lib/afasgem/updateconnector.rb, line 81
def execute(xml)
        message = {
                token: Afasgem.get_token,
                connectorType: @connectorname,
                connectorVersion: 1,
                dataXml: xml
        }
        resp = @client.call(:execute, message: message)
        return resp
end