module Biro module Spc class Request < BaseRequest include Utils::Soap SCORE_3_MONTHS = 77 SCORE_12_MONTHS = 78 def development_url 'https://treina.spc.org.br/spc/remoting/ws/consulta/consultaWebService?wsdl' end def production_url 'https://servicos.spc.org.br/spc/remoting/ws/consulta/consultaWebService?wsdl' end def required_params [:username, :password] end def find(document, options = {}) begin params = {} add_basic_info(params, document, options[:kind] || 'F', options[:product_code]) add_score_info(params) if options[:include_score] response = soap.call(:consultar, message: params) Response.new(response) rescue => e Biro.log(:warn, "Unable to process SPC request") raise AuthenticationError.new("Error at SPC request: #{e.message}") if e.message.include?("CS_AUT") raise DocumentNotFoundError.new("Error at SPC request: #{e.message}") if e.message.include?("CPF inválido") || e.message.include?("(7)") raise StandardError.new("Error at SPC request: #{e.message}") end end def display_products response = soap.call(:listar_produtos, xml: '') response.body end private def savon_options { basic_auth: [@username, @password] } end def add_basic_info(params, document, kind, product_code) params['codigo-produto'] = product_code params['tipo-consumidor'] = kind params['documento-consumidor'] = document end def add_score_info(request) request['codigo-insumo-opcional'] = [SCORE_3_MONTHS, SCORE_12_MONTHS] end end end end