module ELC #:nodoc: class USPS # Testing Gateway # # replace with: http://production.shippingapis.com/ShippingAPI.dll GATEWAY_URL = "http://testing.shippingapis.com/ShippingAPITest.dll" API_NAME = "TrackV2" ERROR_MSG = "Missing parameter" # Initializes the ELC::USPS class # # usps = ELC::USPS.new(username) # # ==== Required option # username - Your USPS Username # def initialize(username) @username = validate(username) end # Track a Priority Mail, Express Mail or Package Services # # usps = ELC::USPS.new(username) # result = usps.track(tracking_number) # # Returns an array of TrackDetail hashes if it was found or an empty array if not. # # ==== Required option # tracking_number - The USPS-provided track id you wish to track # def track(tracking_number) @tracking_number = validate(tracking_number) parse_result(http_post) end private # Converts a XML Response into an array of hashes def parse_result(doc) event_list = [] (Hpricot.parse(doc)/:trackdetail).each do |detail| h = {} detail.children.each { |elem| h[elem.name.to_sym] = elem.inner_text unless elem.inner_text.blank? } event_list << h end event_list end # Sends the Request to the Web Tools server and return the Response def http_post Net::HTTP.post_form(URI.parse(GATEWAY_URL), {'API' => API_NAME, 'XML' => track_field_request}).body end # Track/Confirm Fields Request XML def track_field_request "" end def validate(param) raise ERROR_MSG if param.blank? param end end end