class Correios::Pricefier::CalculatePrice
Public Class Methods
new(data = {})
click to toggle source
Calls superclass method
# File lib/pricefier/requests/calculate_price.rb, line 4 def initialize(data = {}) @credentials = Correios.credentials @show_request = data[:show_request] @service_codes = data[:service_codes] @source_zip_code = data[:source_zip_code] @target_zip_code = data[:target_zip_code] @object = data[:object] @own_hands = data[:own_hands] @receipt_notification = data[:receipt_notification] @declared_value = data[:declared_value] @reference_date = data[:reference_date] super() end
Public Instance Methods
request(method)
click to toggle source
# File lib/pricefier/requests/calculate_price.rb, line 19 def request(method) @method = method @method_snake = method.underscore puts xml if @show_request == true begin format_response(Pricefier.client.call( @method_snake.to_sym, soap_action: "http://tempuri.org/#{@method}", xml: xml ).to_hash) rescue Savon::SOAPFault => error generate_soap_fault_exception(error) rescue Savon::HTTPError => error generate_http_exception(error.http.code) end end
Private Instance Methods
format_response(response)
click to toggle source
# File lib/pricefier/requests/calculate_price.rb, line 67 def format_response(response) response = response["#{@method_snake}_response".to_sym]["#{@method_snake}_result".to_sym] services = response[:servicos][:c_servico] services = [services] if services.is_a?(Hash) { services: services.map { |s| format_service(s) } } end
format_service(service)
click to toggle source
# File lib/pricefier/requests/calculate_price.rb, line 76 def format_service(service) if [0, 10, 11].include?(service[:erro].to_i) { code: service[:codigo], prices: { additional_serivces: { own_hands: string_to_decimal(service[:valor_mao_propria]), receipt_notification: string_to_decimal( service[:valor_aviso_recebimento] ), declared_value: string_to_decimal( service[:valor_valor_declarado] ) }, only_shipping: string_to_decimal(service[:valor_sem_adicionais]), total: string_to_decimal(service[:valor]) } } else { code: service[:codigo], error: { code: service[:erro], description: service[:msg_erro] } } end end
xml()
click to toggle source
# File lib/pricefier/requests/calculate_price.rb, line 39 def xml Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| xml['soap'].Envelope(Pricefier.namespaces) do xml['soap'].Body do xml['ns1'].send(@method) do xml.nCdEmpresa @credentials.administrative_code xml.sDsSenha @credentials.sigep_password xml.nCdServico array_to_string_comma(@service_codes) xml.sCepOrigem @source_zip_code xml.sCepDestino @target_zip_code xml.nCdFormato pricefier_object_type(@object[:type]) xml.nVlPeso @object[:weight].to_f / 1000 xml.nVlComprimento @object[:length] || 0 xml.nVlAltura @object[:height] || 0 xml.nVlLargura @object[:width] || 0 xml.nVlDiametro @object[:diameter] || 0 xml.sCdMaoPropria bool_to_string(@own_hands) xml.sCdAvisoRecebimento bool_to_string(@receipt_notification) xml.nVlValorDeclarado @declared_value || 0 if @method == 'CalcPrecoData' xml.sDtCalculo date_to_string(@reference_date) end end end end end.doc.root.to_xml end