module Biro module Ph3a class Request < BaseRequest include Utils::Soap def development_url 'https://ws.databusca.com.br/DataHistory.svc?wsdl' end def production_url 'https://ws.databusca.com.br/DataHistory.svc?wsdl' end def required_params [:username, :password, :domain] end def find(document) require 'pry'; binding.pry raise AuthenticationError.new('Invalid authentication credentials') if token.blank? begin params = {} get_parameters params, document response = soap.call(:get_person_data, message: params) Response.new(response) rescue => e Biro.log(:warn, "Unable to process Ph3a request") raise AuthenticationError.new("Error at PH3A request: #{e.message}") if e.message.include?("User not authenticated") raise StandardError.new("Error at PH3A request: #{e.message}") end end private def token @token ||= get_token end def get_token login_params = {} get_login_parameters login_params begin response = soap.call(:login, message: login_params) rescue raise AuthenticationError.new('Authentication Error') end LoginBuilder.new(response).token end def get_login_parameters login_params login_params['tns:user'] = {} login_params['tns:user']['ph3a:DomainId'] = @domain login_params['tns:user']['ph3a:Password'] = @password login_params['tns:user']['ph3a:UserName'] = @username end def get_parameters params, document params['tns:token'] = token params['tns:document'] = document end def savon_options { namespaces: { "xmlns:soapenv"=>"http://schemas.xmlsoap.org/soap/envelope/", "xmlns:tem"=>"http://tempuri.org/", "xmlns:ph3a"=>"http://schemas.datacontract.org/2004/07/PH3A.Viper.Web.PortalService.Models" }} end end end end