Permalink
Browse files

add some working specs for location, status, timestamp

  • Loading branch information...
1 parent cbb1e58 commit bcba8b73b72a27fa1f80d44625815f96b249283a @Capncavedan committed Jan 10, 2014
Showing with 77 additions and 0 deletions.
  1. +46 −0 lib/usps_shipment.rb
  2. +31 −0 spec/lib/usps_shipment_spec.rb
View
@@ -6,6 +6,52 @@ class Shipshape::USPSShipment
def initialize(str)
@tracking_number = str
+ @xml ||= Nokogiri::XML.parse(get_xml)
+ end
+
+ def timestamp(str=nil)
+ str ||= self.track_summary
+ case str
+ when /\b([A-Za-z]+ \d{1,2}, \d\d\d\d, \d{1,2}:\d\d [ap]m)\b/
+ # January 2, 2014, 8:27 pm
+ DateTime.parse $1
+ when /\b(\d{1,2}:\d\d [ap]m) on ([A-Za-z]+ \d{1,2}, \d\d\d\d)\b/
+ # 3:21 pm on January 4, 2014
+ DateTime.parse "#{$2}, #{$1}"
+ when /\b([A-Za-z]+ \d{1,2}, \d\d\d\d)\b/
+ # January 4, 2014
+ DateTime.parse $1
+ else
+ nil
+ end
+ end
+
+ def status
+ case track_summary
+ when /^(Arrived)/
+ "#{$1} #{location}"
+ else
+ nil
+ end
+ end
+
+ def location
+ case track_summary
+ when /\b([A-Z\ ]+, [A-Z]{2} \d{5})\b/
+ # SECAUCUS, NJ 07094
+ # DES MOINES, IA 50318.
+ $1.strip
+ else
+ nil
+ end
+ end
+
+ def track_summary
+ @xml.xpath('//TrackSummary').first.content
+ end
+
+ def get_xml
+ ""
end
@@ -4,4 +4,35 @@
it "works" do
shipment = Shipshape::USPSShipment.new("xyz")
end
+
+ context "instance methods" do
+ before do
+ Shipshape::USPSShipment.any_instance.stub(:get_xml).and_return(
+<<-XML
+<?xml version="1.0" encoding="UTF-8"?>
+<TrackResponse>
+ <TrackInfo ID="9274899999158703477863">
+ <TrackSummary>Arrived Shipping Partner Facility, January 2, 2014, 8:27 pm, SECAUCUS, NJ 07094</TrackSummary>
+ <TrackDetail>Arrived Shipping Partner Facility, January 2, 2014, 8:27 pm, SECAUCUS, NJ 07094</TrackDetail>
+ </TrackInfo>
+</TrackResponse>
+XML
+)
+ @shipment = Shipshape::USPSShipment.new("9400110200881038748491")
+ end
+
+ it "returns a location" do
+ @shipment.location.should eql "SECAUCUS, NJ 07094"
+ end
+
+ it "returns a status" do
+ @shipment.status.should eql "Arrived SECAUCUS, NJ 07094"
+ end
+
+ it "returns a timestamp" do
+ # TODO: account for timezone
+ @shipment.timestamp.to_s.should eql "2014-01-02T20:27:00+00:00"
+ end
+
+ end
end

0 comments on commit bcba8b7

Please sign in to comment.