trackerific 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/trackerific/services/usps.rb +54 -39
- data/trackerific.gemspec +1 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.2
|
@@ -48,52 +48,20 @@ module Trackerific
|
|
48
48
|
# connect to the USPS shipping API via HTTParty
|
49
49
|
response = self.class.get(
|
50
50
|
Rails.env.production? ? "/ShippingAPI.dll" : "/ShippingAPITest.dll",
|
51
|
-
:query => {
|
52
|
-
:API => 'TrackV2',
|
53
|
-
:XML => build_tracking_xml_request
|
54
|
-
}.to_query
|
51
|
+
:query => { :API => 'TrackV2', :XML => build_tracking_xml_request }.to_query
|
55
52
|
)
|
56
53
|
# raise any errors
|
57
54
|
error = check_response_for_errors(response, :TrackV2)
|
58
55
|
raise error unless error.nil?
|
59
|
-
# get the tracking information from the response
|
60
|
-
# Trackerific::Details
|
56
|
+
# get the tracking information from the response
|
61
57
|
tracking_info = response['TrackResponse']['TrackInfo']
|
62
58
|
events = []
|
63
|
-
#
|
64
|
-
use_city_state_lookup = @options[:use_city_state_lookup] || false
|
65
|
-
# parse the details
|
59
|
+
# parse the tracking events out of the USPS tracking info
|
66
60
|
tracking_info['TrackDetail'].each do |d|
|
67
|
-
# each tracking detail is a string in this format:
|
68
|
-
# MM DD HH:MM am/pm DESCRIPTION CITY STATE ZIP
|
69
|
-
d = d.split(" ")
|
70
|
-
date = DateTime.parse(d[0..3].join(" "))
|
71
|
-
desc = d[4..d.length].join(" ")
|
72
|
-
# the zip code is always the last word, if it is all numbers
|
73
|
-
if use_city_state_lookup then
|
74
|
-
# this gets the exact location of the package, and is very accurate,
|
75
|
-
# however, it requires access to the shipping services in USPS
|
76
|
-
zip = d[d.length-1]
|
77
|
-
loc = ""
|
78
|
-
# check if zip is a number
|
79
|
-
if zip.to_i.to_s == zip
|
80
|
-
loc = city_state_lookup(zip)
|
81
|
-
loc = "#{loc[:city].titelize}, #{loc[:state]} #{loc[:zip]}"
|
82
|
-
# attempt to delete the location from the description
|
83
|
-
desc = desc.gsub("#{loc[:city]} #{loc[:state]} #{loc[:zip]}", "")
|
84
|
-
end
|
85
|
-
else
|
86
|
-
# extract the location from the description - not always accurate,
|
87
|
-
# but better than nothing
|
88
|
-
d = desc.split(" ") # => ['the', 'description', 'city', 'state', 'zip']
|
89
|
-
desc = d[0..d.length-4].join(" ") # => "the description"
|
90
|
-
loc = d[d.length-3, d.length] # => ['city', 'state', 'zip']
|
91
|
-
loc = "#{loc[0].titleize}, #{loc[1]} #{loc[2]}" # "City, STATE zip"
|
92
|
-
end
|
93
61
|
events << Trackerific::Event.new(
|
94
|
-
:date =>
|
95
|
-
:description =>
|
96
|
-
:location =>
|
62
|
+
:date => date_of_event(d),
|
63
|
+
:description => description_of_event(d).capitalize,
|
64
|
+
:location => location_of_event(d)
|
97
65
|
)
|
98
66
|
end unless tracking_info['TrackDetail'].nil?
|
99
67
|
# return the details
|
@@ -134,7 +102,54 @@ module Trackerific
|
|
134
102
|
}
|
135
103
|
end
|
136
104
|
|
137
|
-
|
105
|
+
private
|
106
|
+
|
107
|
+
# Parses a USPS tracking event, and returns its date
|
108
|
+
# @param [String] event The tracking event to parse
|
109
|
+
# @return [DateTime] The date / time of the event
|
110
|
+
# @api private
|
111
|
+
def date_of_event(event)
|
112
|
+
# get the date out of
|
113
|
+
# Mon DD HH:MM am/pm THE DESCRIPTION CITY STATE ZIP.
|
114
|
+
d = event.split(" ")
|
115
|
+
DateTime.parse(d[0..3].join(" "))
|
116
|
+
end
|
117
|
+
|
118
|
+
# Parses a USPS tracking event, and returns its description
|
119
|
+
# @param [String] event The tracking event to parse
|
120
|
+
# @return [DateTime] The description of the event
|
121
|
+
# @api private
|
122
|
+
def description_of_event(event)
|
123
|
+
# get the description out of
|
124
|
+
# Mon DD HH:MM am/pm THE DESCRIPTION CITY STATE ZIP.
|
125
|
+
d = event.split(" ")
|
126
|
+
d[4..d.length-4].join(" ").capitalize
|
127
|
+
end
|
128
|
+
|
129
|
+
# Parses a USPS tracking event, and returns its location
|
130
|
+
# @param [String] event The tracking event to parse
|
131
|
+
# @return The location of the event
|
132
|
+
# @api private
|
133
|
+
def location_of_event(event)
|
134
|
+
# remove periods, and split by spaces
|
135
|
+
d = event.gsub(".", "").split(" ")
|
136
|
+
l = d[d.length-3, d.length] # => ['city', 'state', 'zip']
|
137
|
+
# this is the location from the USPS tracking XML. it is not guaranteed
|
138
|
+
# to be completely accurate, since there's no way to know if it will
|
139
|
+
# always be the last 3 words.
|
140
|
+
city = l[0]
|
141
|
+
state = l[1]
|
142
|
+
zip = l[2]
|
143
|
+
# for greater accuracy, we can use the city/state lookup API from USPS
|
144
|
+
if @options[:use_city_state_lookup]
|
145
|
+
l = city_state_lookup(zip)
|
146
|
+
# these will be nil if USPS does not have the zipcode in their database
|
147
|
+
city = l[:city] unless l[:city].nil?
|
148
|
+
state = l[:state] unless l[:state].nil?
|
149
|
+
zip = l[:zip] unless l[:zip].nil?
|
150
|
+
end
|
151
|
+
"#{city.titleize}, #{state} #{zip}"
|
152
|
+
end
|
138
153
|
|
139
154
|
# Checks a HTTParty response for USPS, or HTTP errors
|
140
155
|
# @param [HTTParty::Response] response The HTTParty response to check
|
data/trackerific.gemspec
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 6
|
8
|
-
-
|
9
|
-
version: 0.6.
|
8
|
+
- 2
|
9
|
+
version: 0.6.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Travis Haynes
|
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
218
218
|
requirements:
|
219
219
|
- - ">="
|
220
220
|
- !ruby/object:Gem::Version
|
221
|
-
hash: -
|
221
|
+
hash: -1352465954365462278
|
222
222
|
segments:
|
223
223
|
- 0
|
224
224
|
version: "0"
|