require 'savon' module Northern911Api class Client attr_reader :configuration, :methods def initialize @configuration = Northern911Api.configuration @savon = savon_client @methods = savon_client.operations end def method_missing(method_name, *args, &block) if methods.include? method_name params = {} params = (args.first || {}).merge(hash: auth_string) params.merge!(vendor_code: configuration.vendor_code) # because savon do not support Customer object declared in WSDL as a complex type if(method_name == :addor_update_customer) raw_xml = "#{params[:customer][:city]}Y#{params[:customer][:first_name]}#{params[:customer][:last_name]}#{params[:customer][:other_address_info]}#{params[:customer][:phone_number]}#{params[:customer][:postal_code_zip]}#{params[:customer][:province_state]}#{params[:customer][:street_name]}#{params[:customer][:street_number]}#{params[:customer][:suite_apt]}#{configuration.vendor_code}#{params[:hash]}" @savon.call(method_name, xml: raw_xml) elsif(method_name == :query_customer) raw_xml = "#{configuration.vendor_code}#{params[:phone_number]}#{params[:hash]}" @savon.call(method_name, xml: raw_xml) elsif(method_name == :delete_customer) raw_xml = "#{configuration.vendor_code}#{params[:phone_number]}#{params[:hash]}" @savon.call(method_name, xml: raw_xml) else @savon.call(method_name, message: params) end else raise NoMethodError, "#{method_name} is not a valid api endpoint!" end end private def savon_client Savon::Client.new wsdl: configuration.wsdl end def auth_string gmdate = Time.now.strftime('%Y%m%d') Digest::MD5.hexdigest(configuration.vendor_code + configuration.soap_passcode + gmdate) end end end