module FastshopCatalog class BaseService AUX_LOG = Logger.new(STDOUT) AUX_LOG.level = Logger::WARN class << self attr_accessor :log_level attr_accessor :base_url attr_accessor :crypto_key end def service_url env = "development" env = Rails.env if defined?(Rails) url_service = FastshopCatalog::ServiceUrls.new(env) @service_url ||= url_service.url_for(@service) end def action @action ||= "#{@interface}/#{BaseService.camelize(@soap_method)}" end def self.camelize(val) val.to_s.split('_').map {|w| w.capitalize}.join end def encrypt(payload) require_relative 'crypto' Crypto.new.encrypt(payload.to_s) end def initialize(params={}) @@log_level ||= :info default_params = {:wsdl => "#{service_url}?wsdl", :soap_version => 2, :namespaces => {"xmlns:x" => "http://www.w3.org/2005/08/addressing"}, :soap_header => {"x:To" => service_url, "x:Action" => "http://tempuri.org/#{action}"}, :log_level => @log_level} @client = Savon.client(recursive_merge(default_params, params)) HTTPI.log = false end protected def service_invoker(parameters={}) response = @client.call(@soap_method, :message => adapt_output(parameters)) result = JSON.parse(response.body["#{@soap_method}_response".to_sym]["#{@soap_method}_result".to_sym]) result_status = result['Retorno'] if result_status['IdRetorno'] != 0 raise ServiceException.new(:code => result_status['IdRetorno'], :description => result_status['MsgRetorno']) end result[@return_key] end def service_invoker_with_encryption(encrypted_payload_key, parameters={}) encrypted_payload = encrypt(parameters[encrypted_payload_key]) FastshopCatalog::BaseService::AUX_LOG.debug('Encrypted payload: ' + encrypted_payload) parameters[encrypted_payload_key] = encrypted_payload service_invoker(parameters.merge(:salt => '')) end private def adapt_output(params={}, namespace='tns') Hash[params.map {|k, v| ["#{namespace}:#{k}", v] }] end def recursive_merge(h1, h2) h1.merge(h2) { |k, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2) : v2 } end end end