class FriendlyShipping::Services::Ups::ParseXMLResponse

Constants

SUCCESSFUL_RESPONSE_STATUS_CODE

Public Class Methods

call(request:, response:, expected_root_tag:) click to toggle source
# File lib/friendly_shipping/services/ups/parse_xml_response.rb, line 11
def call(request:, response:, expected_root_tag:)
  xml = Nokogiri.XML(response.body, &:strict)

  if xml.root.nil? || xml.root.name != expected_root_tag
    wrap_failure('Invalid document', request, response)
  elsif request_successful?(xml)
    Success(xml)
  else
    wrap_failure(error_message(xml), request, response)
  end
rescue Nokogiri::XML::SyntaxError => e
  wrap_failure(e, request, response)
end

Private Class Methods

error_message(xml) click to toggle source
# File lib/friendly_shipping/services/ups/parse_xml_response.rb, line 31
def error_message(xml)
  status = xml.root.at_xpath('Response/ResponseStatusDescription')&.text
  desc = xml.root.at_xpath('Response/Error/ErrorDescription')&.text
  [status, desc].compact.join(": ").presence || 'UPS could not process the request.'
end
request_successful?(xml) click to toggle source
# File lib/friendly_shipping/services/ups/parse_xml_response.rb, line 27
def request_successful?(xml)
  xml.root.at('Response/ResponseStatusCode').text == SUCCESSFUL_RESPONSE_STATUS_CODE
end
wrap_failure(failure, request, response) click to toggle source
# File lib/friendly_shipping/services/ups/parse_xml_response.rb, line 37
def wrap_failure(failure, request, response)
  Failure(
    FriendlyShipping::ApiFailure.new(
      failure,
      original_request: request,
      original_response: response
    )
  )
end