Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first cut at time zone handling for events

  • Loading branch information...
commit 3f180f8c65465b336798a9cf8982d5d22e907282 1 parent 59d5000
@Capncavedan authored
View
2  Gemfile
@@ -2,6 +2,8 @@ source "https://rubygems.org"
gem "faraday"
gem "nokogiri"
+gem "zip-codes", "~> 0.2"
+gem "tzinfo"
group :test do
gem "rspec"
View
8 Gemfile.lock
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
+ atomic (1.1.14)
celluloid (0.15.2)
timers (~> 1.1.0)
coderay (1.1.0)
@@ -47,7 +48,12 @@ GEM
ruby_gntp (0.3.4)
slop (3.4.7)
thor (0.18.1)
+ thread_safe (0.1.3)
+ atomic
timers (1.1.0)
+ tzinfo (1.1.0)
+ thread_safe (~> 0.1)
+ zip-codes (0.2.0)
PLATFORMS
ruby
@@ -59,3 +65,5 @@ DEPENDENCIES
rake
rspec
ruby_gntp
+ tzinfo
+ zip-codes (~> 0.2)
View
32 lib/usps_shipment.rb
@@ -1,6 +1,8 @@
require 'faraday'
require 'nokogiri'
require 'date'
+require 'zip-codes'
+require 'tzinfo'
class Shipshape::USPSShipment
@@ -10,16 +12,32 @@ def initialize(str)
end
def timestamp
- case self.track_summary
+ time_string = case track_summary
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
+ $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}"
+ "#{$2}, #{$1}"
when /\b([A-Za-z]+ \d{1,2}, \d\d\d\d)\b/
# January 4, 2014
- DateTime.parse $1
+ $1
+ else
+ nil
+ end
+
+ datetime = DateTime.parse time_string
+
+ if timestamp_timezone
+ timestamp_timezone.local_to_utc(datetime)
+ else
+ datetime
+ end
+ end
+
+ def timestamp_timezone
+ if location =~ /(\d{5})\Z/
+ TZInfo::Timezone.get ZipCodes.identify($1)[:time_zone]
else
nil
end
@@ -54,12 +72,12 @@ def get_xml
end
def tracking_response
- conn = Faraday.new(:url => "http://production.shippingapis.com") do |faraday|
+ conn = Faraday.new(url: "http://production.shippingapis.com") do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
- builder = Nokogiri::XML::Builder.new do |xml|
+ tracking_request = Nokogiri::XML::Builder.new do |xml|
xml.TrackRequest("USERID" => ENV['USPS_USERID']) {
xml.TrackID("ID" => @tracking_number)
}
@@ -67,7 +85,7 @@ def tracking_response
resp = conn.get do |req|
req.url '/ShippingAPI.dll'
req.params["API"] = "TrackV2"
- req.params["XML"] = builder.to_xml
+ req.params["XML"] = tracking_request.to_xml
end
resp.body
end
View
11 spec/lib/usps_shipment_spec.rb
@@ -1,11 +1,9 @@
require 'spec_helper'
describe "Shipshape::USPSShipment" do
- it "works" do
- shipment = Shipshape::USPSShipment.new("xyz")
- end
context "instance methods" do
+
before do
Shipshape::USPSShipment.any_instance.stub(:tracking_response).and_return(
<<-XML
@@ -31,7 +29,12 @@
it "returns a timestamp" do
# TODO: account for timezone
- @shipment.timestamp.to_s.should eql "2014-01-02T20:27:00+00:00"
+ @shipment.timestamp.should eql DateTime.parse("2014-01-03T01:27:00+00:00")
+ end
+
+ it "returns a timezone" do
+ @shipment.timestamp_timezone.class.to_s.should eql("TZInfo::DataTimezone")
+ @shipment.timestamp_timezone.to_s.should eql "America - New York"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.