class CorreiosToolkit::Base

Constants

CORREIOS_DEFAULT_ENCODE
SOAP_URL

Protected Instance Methods

request(payload:, response_node:) click to toggle source
# File lib/correios_toolkit/base.rb, line 17
def request(payload:, response_node:)
  response = RestClient.post(SOAP_URL, payload, content_type: 'application/xml')

  if response.code.between?(200, 299)
    json_parse(response.body, response_node)
  else
    raise(RestClient::ExceptionWithResponse, response)
  end
rescue RestClient::ExceptionWithResponse => e
  raise(GatewayError, json_parse(e.response.body))
end

Private Instance Methods

error_parser(xml) click to toggle source
# File lib/correios_toolkit/base.rb, line 45
def error_parser(xml)
  Hash.from_xml(xml).dig('Envelope', 'Body', 'Fault', 'faultstring')
end
json_parse(xml, node = nil) click to toggle source
# File lib/correios_toolkit/base.rb, line 31
def json_parse(xml, node = nil)
  xml.encode!('UTF-8', CORREIOS_DEFAULT_ENCODE)

  if node.present?
    success_parser(xml, node)
  else
    error_parser(xml)
  end
end
success_parser(xml, node) click to toggle source
# File lib/correios_toolkit/base.rb, line 41
def success_parser(xml, node)
  Hash.from_xml(xml).dig('Envelope', 'Body', "#{node}Response", 'return').with_indifferent_access
end