Class: ActiveShipping::USPSReturns

Inherits:
Carrier
  • Object
show all
Defined in:
lib/active_shipping/carriers/usps_returns.rb

Constant Summary collapse

LIVE_DOMAIN =
'returns.usps.com'
LIVE_RESOURCE =
'Services/ExternalCreateReturnLabel.svc/ExternalCreateReturnLabel'
TEST_DOMAIN =
'returns.usps.com'
TEST_RESOURCE =
'Services/ExternalCreateReturnLabel.svc/ExternalCreateReturnLabel'
API_CODES =
{
  :external_return_label_request => 'externalReturnLabelRequest'
}
USE_SSL =
{
  :external_return_label_request => true
}
@@name =
"USPS Returns"

Instance Attribute Summary

Attributes inherited from Carrier

#last_request, #test_mode

Instance Method Summary collapse

Methods inherited from Carrier

#available_services, #cancel_shipment, #create_shipment, default_location, #find_rates, #find_tracking_info, #initialize, #maximum_address_field_length, #maximum_weight, #save_request, #timestamp_from_business_day, #valid_credentials?

Constructor Details

This class inherits a constructor from ActiveShipping::Carrier

Instance Method Details

#commit(action, request, test = false) ⇒ Object (protected)



74
75
76
# File 'lib/active_shipping/carriers/usps_returns.rb', line 74

def commit(action, request, test = false)
  ssl_get(request_url(action, request, test))
end

#external_return_label_errors(document) ⇒ Object (protected)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_shipping/carriers/usps_returns.rb', line 57

def external_return_label_errors(document)
  return {} unless document.respond_to?(:elements)
  res = {}
  if node = document.at('*/errors')
    if node.at('ExternalReturnLabelError')
      if message = node.at('ExternalReturnLabelError/InternalErrorDescription').try(:text)
        code = node.at('ExternalReturnLabelError/InternalErrorNumber').try(:text) || ''
        res = {:error => {:code => code, :message => message}}
      elsif message = node.at('ExternalReturnLabelError/ExternalErrorDescription').try(:text)
        code = node.at('ExternalReturnLabelError/ExternalErrorNumber').try(:text) || ''
        res = {:error => {:code => code, :message => message}}
      end
    end
  end
  res
end

#external_return_label_request(label, options = {}) ⇒ Object



28
29
30
31
# File 'lib/active_shipping/carriers/usps_returns.rb', line 28

def external_return_label_request(label, options = {})
  response = commit(:external_return_label_request, label.to_xml, (options[:test] || false))
  parse_external_return_label_response(response)
end

#parse_external_return_label_response(response) ⇒ Object (protected)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_shipping/carriers/usps_returns.rb', line 35

def parse_external_return_label_response(response)
  tracking_number, postal_routing, return_label, message = '', '', '', '', ''
  xml = Nokogiri::XML(response)
  error = external_return_label_errors(xml)
  if error.is_a?(Hash) && error.size > 0
    message << "#{error[:error][:code]}: #{error[:error][:message]}"
  else
    tracking_number = xml.at('TrackingNumber').try(:text)
    postal_routing = xml.at('PostalRouting').try(:text)
    return_label = xml.at('ReturnLabel').try(:text)
  end

  ExternalReturnLabelResponse.new(message.length == 0, message, Hash.from_xml(response),
    :xml => response,
    :carrier => @@name,
    :request => last_request,
    :return_label => return_label,
    :postal_routing => postal_routing,
    :tracking_number => tracking_number
  )
end

#request_url(action, request, test) ⇒ Object (protected)



78
79
80
81
82
83
# File 'lib/active_shipping/carriers/usps_returns.rb', line 78

def request_url(action, request, test)
  scheme = USE_SSL[action] ? 'https://' : 'http://'
  host = test ? TEST_DOMAIN : LIVE_DOMAIN
  resource = test ? TEST_RESOURCE : LIVE_RESOURCE
  "#{scheme}#{host}/#{resource}?#{API_CODES[action]}=#{URI.encode(request)}"
end

#requirementsObject



24
25
26
# File 'lib/active_shipping/carriers/usps_returns.rb', line 24

def requirements
  []
end