lib/gotransverse-tract-api.rb in gotransverse-tract-api-0.3.4 vs lib/gotransverse-tract-api.rb in gotransverse-tract-api-0.4.0
- old
+ new
@@ -1,9 +1,10 @@
require "gotransverse-tract-api/version"
require "gotransverse-tract-api/configuration"
require "gotransverse-tract-api/hash"
+require "gotransverse-tract-api/api_data"
require "gotransverse-tract-api/billing_account/adjustment"
require "gotransverse-tract-api/billing_account/adjustment_application"
require "gotransverse-tract-api/billing_account/adjustment_reason"
require "gotransverse-tract-api/billing_account/billing_account"
@@ -153,11 +154,15 @@
# @param {Hash} request_body
# @param {String} command
#
def self.post_request_for(klass, api_params={}, request_body, command)
api_url = GoTransverseTractApi.get_api_url_for(klass)
- api_url = "#{api_url}/#{command}"
+ if (api_params.empty?)
+ api_url = "#{api_url}/#{command}"
+ else
+ api_url = "#{api_url}/#{api_params[:eid]}/#{command}"
+ end
self.call(klass, api_url, api_params, :post, request_body)
end
#
@@ -170,10 +175,32 @@
def self.put_request_for(klass, api_params={}, request_body)
api_url = GoTransverseTractApi.get_api_url_for(klass)
self.call(klass, api_url, api_params, :put, request_body.to_xml)
end
+ #
+ # Generate XML for request body
+ #
+ # @param {hash} data
+ # @param (String} root element
+ #
+
+ def self.generateXML(data, root_elem)
+ tract_api_ver = GoTransverseTractApi::TARGET_API_VERSION
+ data[root_elem.to_sym][:xmlns] = "http://www.tractbilling.com/billing/#{tract_api_ver}/domain"
+
+ builder = Nokogiri::XML::Builder.new do|xml|
+ xml.send(root_elem,Hash[data[root_elem.to_sym]]) do
+ arr = []
+ arr << root_elem.to_sym
+ self.process_data(data, arr, xml)
+ end
+ end
+
+ builder.doc.root.to_xml
+ end
+
private
#
# self.get_cached_response_for
#
@@ -240,8 +267,31 @@
new_key = k.to_s.camelize(:lower)
new_key = new_key.to_sym if k.is_a? Symbol
hsh[new_key] = hsh.delete(k)
end
hsh
+ end
+
+ def self.process_data(data, arr, xml)
+ data.each do|key,val|
+ next if arr.include?(key)
+
+ if (val.is_a?Hash)
+ if (val.has_key?(:attributes))
+ xml.send(key, Hash[data[key][:attributes]]) do
+ arr << 'attributes'.to_sym
+ self.process_data(data[key],arr,xml)
+ end
+ else
+ xml.send(key, Hash[data[key]])
+ end
+ elsif (val.is_a?Array)
+ val.each do|item|
+ self.process_data(item,arr,xml)
+ end
+ else
+ xml.send(key, val)
+ end
+ end
end
end