awesome_usps 0.6.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,70 +1,54 @@
1
1
  module AwesomeUsps
2
2
  module OpenDistrubutePriority
3
-
4
- MAX_RETRIES = 3
5
-
6
- LIVE_DOMAIN = 'secure.shippingapis.com'
7
- LIVE_RESOURCE = '/ShippingAPI.dll'
8
-
9
-
10
- API_CODES = {:open_distrubute_priority => "OpenDistributePriority",
11
- :open_distribute_priority_certify => "OpenDistributePriorityCertify"}
12
-
13
- def open_distrubute_priority_label(orgin, destination, package_weight_in_ounces, mail_type, image_type, label_type=1, options={})
14
- @package_weight_in_ounces = package_weight_in_ounces
15
- @origin = origin
16
- @destination = destination
17
- @mail_type = mail_type
18
- @image_type = image_type
19
- @options =options
20
- @api = "OpenDistributePriorityRequest"
21
- request = open_distrubute_priority_xml
3
+ def open_distrubute_priority_label(origin, destination, package_weight_in_ounces, mail_type, image_type, label_type=1, api_requst = "OpenDistributePriorityRequest", options={})
4
+ request = open_distrubute_priority_xml(api_requst, origin, destination, package_weight_in_ounces, mail_type, image_type, label_type, options)
22
5
  #YES THE API IS THAT STUPID THAT WE MUST PASS WHAT TYPE OF MIME TYPE!
23
- commit_open_distrubute_priority_xml(:open_distrubute_priority, request, image_type, false)
6
+ gateway_commit(:open_distrubute_priority, 'OpenDistributePriority', request, :ssl, image_type)
24
7
  end
25
8
 
26
9
  def canned_open_distrubute_priority_label_test
27
- @origin = Location.new( :name=> "John Smith", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770')
28
- @destination =Location.new( :name=> "Fairfax Post Office", :address2 =>"10660 Page Ave", :state => 'VA', :city => 'Fairfax', :zip5 => "22030", :facility_type => "DDU")
29
- @mail_type = "Letters"
30
- @image_type = "PDF"
31
- @package_weight_in_ounces = 1
32
- @options = {:address_service => true, :permit_number => "21718", :permit_zip => "07204"}
33
- @api = "OpenDistributePriorityCertifyRequest"
34
- request= open_distrubute_priority_xml
35
- commit_open_distrubute_priority_xml(:open_distribute_priority_certify, request, @image_type, true)
10
+ origin = Location.new( :name=> "John Smith", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770')
11
+ destination =Location.new( :name=> "Fairfax Post Office", :address2 =>"10660 Page Ave", :state => 'VA', :city => 'Fairfax', :zip5 => "22030", :facility_type => "DDU")
12
+ mail_type = "Letters"
13
+ image_type = "PDF"
14
+ package_weight_in_ounces = 1
15
+ options = {:address_service => true, :permit_number => "21718", :permit_zip => "07204"}
16
+ api_requst = "OpenDistributePriorityCertifyRequest"
17
+ label_type=1
18
+ request= open_distrubute_priority_xml(api_requst, origin, destination, package_weight_in_ounces, mail_type, image_type, label_type, options)
19
+ gateway_commit(:open_distribute_priority_certify, 'OpenDistributePriorityCertify', request, :ssl, image_type)
36
20
  end
37
21
 
38
22
  private
39
- def open_distrubute_priority_xml
23
+ def open_distrubute_priority_xml(api_requst, origin, destination, package_weight_in_ounces, mail_type, image_type, label_type, options)
40
24
  xm = Builder::XmlMarkup.new
41
- xm.tag!("#{@api}", "USERID"=>"#{@username}") do
42
- xm.PermitNumber(@options[:permit_number])
43
- xm.PermitIssuingPOZip5(@options[:permit_zip])
44
- xm.FromName(@origin.name)
45
- xm.FromFirm(@origin.firm_name)
46
- xm.FromAddress1(@origin.address1) #Used for an apartment or suite number. Yes the API is a bit fucked.
47
- xm.FromAddress2(@origin.address2)
48
- xm.FromCity(@origin.city)
49
- xm.FromState(@origin.state)
50
- xm.FromZip5(@origin.zip5)
51
- xm.FromZip4(@origin.zip4)
52
- xm.POZipCode(@options[:po_zip_code])
53
- xm.ToFacilityName(@destination.name)
54
- xm.ToFacilityAddress1(@destination.address1)
55
- xm.ToFacilityAddress2(@destination.address2)
56
- xm.ToFacilityCity(@destination.city)
57
- xm.ToFacilityState(@destination.state)
58
- xm.ToFacilityZip5(@destination.zip5)
59
- xm.ToFacilityZip4(@destination.zip4)
60
- xm.FacilityType(@destination.facility_type)
61
- xm.MailClassEnclosed(@mail_type)
62
- xm.MailClassOther(@options[:other])
25
+ xm.tag!("#{api_requst}", "USERID"=>"#{@username}") do
26
+ xm.PermitNumber(options[:permit_number])
27
+ xm.PermitIssuingPOZip5(options[:permit_zip])
28
+ xm.FromName(origin.name)
29
+ xm.FromFirm(origin.firm_name)
30
+ xm.FromAddress1(origin.address1) #Used for an apartment or suite number. Yes the API is a bit fucked.
31
+ xm.FromAddress2(origin.address2)
32
+ xm.FromCity(origin.city)
33
+ xm.FromState(origin.state)
34
+ xm.FromZip5(origin.zip5)
35
+ xm.FromZip4(origin.zip4)
36
+ xm.POZipCode(options[:po_zip_code])
37
+ xm.ToFacilityName(destination.name)
38
+ xm.ToFacilityAddress1(destination.address1)
39
+ xm.ToFacilityAddress2(destination.address2)
40
+ xm.ToFacilityCity(destination.city)
41
+ xm.ToFacilityState(destination.state)
42
+ xm.ToFacilityZip5(destination.zip5)
43
+ xm.ToFacilityZip4(destination.zip4)
44
+ xm.FacilityType(destination.facility_type)
45
+ xm.MailClassEnclosed(mail_type)
46
+ xm.MailClassOther(options[:other])
63
47
  xm.WeightInPounds("0")
64
- xm.WeightInOunces(@package_weight_in_ounces)
65
- xm.ImageType(@image_type)
66
- xm.SeparateReceiptPage(@options[:seperate])
67
- xm.LabelDate(@options[:label_date])
48
+ xm.WeightInOunces(package_weight_in_ounces)
49
+ xm.ImageType(image_type)
50
+ xm.SeparateReceiptPage(options[:seperate])
51
+ xm.LabelDate(options[:label_date])
68
52
  xm.AllowNonCleansedFacilityAddr("false")
69
53
  end
70
54
  end
@@ -80,48 +64,11 @@ module AwesomeUsps
80
64
  RAILS_DEFAULT_LOGGER.info "#{xml}"
81
65
  return (Hpricot.parse(xml)/:description).inner_html
82
66
  else
83
- number = Hpricot.parse(xml)/:openDistributeprioritynumber
67
+ number = Hpricot.parse(xml)/:opendistributeprioritynumber
84
68
  label = Hpricot.parse(xml)/:opendistributeprioritylabel
85
69
  return {:image_type => image_type, :number => number.inner_html, :label => label.inner_html}
86
70
  end
87
71
  end
88
-
89
- def commit_open_distrubute_priority_xml(action, request, image_type, test=false)
90
- retries = MAX_RETRIES
91
- begin
92
- #If and when their testing resource works again this will be useful tertiary command
93
- url = URI.parse("https://#{LIVE_DOMAIN}#{LIVE_RESOURCE}")
94
- req = Net::HTTP::Post.new(url.path)
95
- req.set_form_data({'API' => API_CODES[action], 'XML' => request})
96
- response = Net::HTTP.new(url.host, 443)
97
- response.use_ssl
98
- response.open_timeout = 5
99
- response.read_timeout = 5
100
- response.use_ssl = true
101
- response.start
102
-
103
- rescue Timeout::Error
104
- if retries > 0
105
- retries -= 1
106
- retry
107
- else
108
- RAILS_DEFAULT_LOGGER.warn "The connection to the remote server timed out"
109
- return "We appoligize for the inconvience but our USPS service is busy at the moment. To retry please refresh the browser"
110
- end
111
- rescue SocketError
112
- RAILS_DEFAULT_LOGGER.error "There is a socket error with USPS plugin"
113
- return "We appoligize for the inconvience but there is a problem with our server. To retry please refresh the browser"
114
- end
115
-
116
- response = response.request(req)
117
- case response
118
- when Net::HTTPSuccess
119
-
120
- parse_open_distrubute_priority(response.body, image_type)
121
- else
122
- RAILS_DEFAULT_LOGGER.warn("USPS plugin settings are wrong #{response.body}")
123
- return "USPS plugin settings are wrong #{response.body}"
124
- end
125
- end
72
+
126
73
  end
127
74
  end
@@ -1,72 +1,59 @@
1
1
  module AwesomeUsps
2
2
  module ServiceStandard
3
- MAX_RETRIES = 3
4
-
5
- LIVE_DOMAIN = 'production.shippingapis.com'
6
- LIVE_RESOURCE = '/ShippingAPI.dll'
7
-
8
- TEST_DOMAIN ='testing.shippingapis.com'
9
- TEST_RESOURCE = '/ShippingAPITest.dll'
10
-
11
- API_CODES ={
12
- :priority_mail => 'PriorityMail',
13
- :standard => 'StandardB',
14
- :express => 'ExpressMailCommitment'
15
- }
16
-
17
3
  # Takes your package tracking number and returns information for the USPS web API
18
- def priority_mail_estimated_time(origin, destination)
19
- @origin = orgin
20
- @destination=destination
21
- request = xml_for_estimated_time_for_delivery("PriorityMailRequest")
22
- commit_service_standard_request(:priority_mail, request ,false)
4
+ def priority_mail_estimated_time(origin, destination, api_request="PriorityMailRequest")
5
+ origin = orgin
6
+ destination=destination
7
+ request = xml_for_estimated_time_for_delivery(api_request, origin, destination)
8
+ commit_service_standard_request(:priority_mail, 'PriorityMail', request , :live)
23
9
  end
24
10
 
25
- def standard_mail_estimated_time(origin, destination)
26
- @origin = orgin
27
- @destination=destination
28
- request = xml_for_estimated_time_for_delivery("StandardBRequest")
29
- commit_service_standard_request(:standard, request ,false)
11
+ def standard_mail_estimated_time(origin, destination, api_request='StandardBRequest')
12
+ origin = orgin
13
+ destination=destination
14
+ request = xml_for_estimated_time_for_delivery(api_request, origin, destination)
15
+ gateway_commit(:standard, 'StandardB', request, :live)
30
16
  end
31
17
 
32
- def express_mail_commitment(origin, destination, date=nil)
33
- @origin = orgin
34
- @destination=destination
35
- @date = date
36
- request = xml_for_estimated_time_for_delivery("ExpressMailCommitmentRequest")
37
- commit_service_standard_request(:express, request ,false)
18
+ def express_mail_commitment(origin, destination, date=nil, api_request='ExpressMailCommitmentRequest')
19
+ xml_for_estimated_time_for_delivery(api_request, origin, destination, date)
20
+ gateway_commit(:express, 'ExpressMailCommitment', request, :live)
38
21
  end
39
22
 
40
23
  def canned_standard_mail_estimated_time_test
41
- @origin = Location.new( :zip5 => '4')
42
- @destination = Location.new( :zip5 => '4')
43
- request = xml_for_estimated_time_for_delivery("PriorityMailRequest")
44
- commit_service_standard_request(:priority_mail, request ,true)
24
+ origin = Location.new( :zip5 => '4')
25
+ destination = Location.new( :zip5 => '4')
26
+ api_request="StandardBRequest"
27
+ request = xml_for_estimated_time_for_delivery(api_request, origin, destination)
28
+ gateway_commit(:priority_mail, 'PriorityMail', request, :test)
45
29
  end
46
30
 
47
31
  def canned_priority_mail_estimated_time_test
48
- @origin = Location.new( :zip5 => '4')
49
- @destination = Location.new( :zip5 => '4')
50
- request = xml_for_estimated_time_for_delivery("PriorityMailRequest")
51
- commit_service_standard_request(:standard, request ,true)
32
+ origin = Location.new( :zip5 => '4')
33
+ destination = Location.new( :zip5 => '4')
34
+ api_request="PriorityMailRequest"
35
+ request = xml_for_estimated_time_for_delivery(api_request, origin, destination)
36
+ gateway_commit(:standard, 'StandardB', request, :test)
52
37
  end
53
38
 
54
39
  def canned_express_mail_commitment_test
55
- @origin= Location.new( :zip5 =>'20770')
56
- @destination=Location.new( :zip5 =>'11210')
57
- @date = '05-Aug-2004'
58
- request = xml_for_estimated_time_for_delivery("ExpressMailCommitmentRequest")
59
- commit_service_standard_request(:express, request ,true)
40
+ origin= Location.new( :zip5 =>'20770')
41
+ destination=Location.new( :zip5 =>'11210')
42
+ date = '05-Aug-2004'
43
+ api_request = 'ExpressMailCommitmentRequest'
44
+ request = xml_for_estimated_time_for_delivery(api_request, origin, destination, date)
45
+ gateway_commit(:express, 'ExpressMailCommitment', request, :test)
60
46
  end
61
47
 
62
48
  # XML from a straight string.
63
- # "<TrackFieldRequest USERID='#{@username}'><TrackID ID='#{@tracking_number}'></TrackID></TrackFieldRequest>"
64
- def xml_for_estimated_time_for_delivery(type_of_request)
49
+ def xml_for_estimated_time_for_delivery(api_request, origin, destination, date=nil)
65
50
  xm = Builder::XmlMarkup.new
66
- xm.tag!(type_of_request, "USERID"=>"#{@username}") do
67
- xm.OriginZIP(@origin.zip5)
68
- xm.DestinationZIP(@destination.zip5)
69
- xm.Date(@date) if type_of_request == "ExpressMailCommitmentRequest"
51
+ xm.tag!(api_request, "USERID"=>"#{@username}") do
52
+ xm.OriginZIP(origin.zip5)
53
+ xm.DestinationZIP(destination.zip5)
54
+ if api_request == 'ExpressMailCommitmentRequest'
55
+ xm.Date(date)
56
+ end
70
57
  end
71
58
  end
72
59
 
@@ -100,44 +87,7 @@ module AwesomeUsps
100
87
  return location_list
101
88
  end
102
89
  end
103
-
104
- private
105
- def commit_service_standard_request(action, request, test = false)
106
- retries = MAX_RETRIES
107
- begin
108
- url = URI.parse(test ? "http://#{TEST_DOMAIN}#{TEST_RESOURCE}" : "http://#{LIVE_DOMAIN}#{LIVE_RESOURCE}")
109
- req = Net::HTTP::Post.new(url.path)
110
- req.set_form_data({'API' => API_CODES[action], 'XML' => request})
111
- response = Net::HTTP.new(url.host, url.port)
112
- response.open_timeout = 5
113
- response.read_timeout = 5
114
- response.start
115
- rescue Timeout::Error
116
- if retries > 0
117
- retries -= 1
118
- retry
119
- else
120
- RAILS_DEFAULT_LOGGER.warn "The connection to the remote server timed out"
121
- return "We appoligize for the inconvience but our USPS service is busy at the moment. To retry please refresh the browser"
122
-
123
- end
124
- rescue SocketError
125
- RAILS_DEFAULT_LOGGER.error "There is a socket error with USPS plugin"
126
- return "We appoligize for the inconvience but there is a problem with our server. To retry please refresh the browser"
127
- end
128
-
129
- response = response.request(req)
130
- case response
131
- when Net::HTTPSuccess
132
- if action == :express
133
- parse_express(response.body)
134
- else
135
- parse_service(response.body)
136
- end
137
- else
138
- RAILS_DEFAULT_LOGGER.warn("USPS plugin settings are wrong #{response}")
139
- end
140
- end
90
+
141
91
 
142
92
  end
143
93
  end
@@ -1,17 +1,5 @@
1
1
  module AwesomeUsps
2
2
  module Shipping
3
-
4
-
5
- MAX_RETRIES = 3
6
-
7
- LIVE_DOMAIN = 'production.shippingapis.com'
8
- LIVE_RESOURCE = '/ShippingAPI.dll'
9
-
10
- API_CODES = {
11
- :us_rates => 'RateV3',
12
- :world_rates => 'IntlRate',
13
- }
14
-
15
3
  CONTAINERS = {
16
4
  :envelope => 'Flat Rate Envelope',
17
5
  :box => 'Flat Rate Box'
@@ -52,28 +40,21 @@ module AwesomeUsps
52
40
 
53
41
  # options Are?
54
42
  def domestic_rates(origin_zip, destination_zip, packages, options={})
55
- @origin_zip = origin_zip
56
- @packages = packages
57
- Array(@packages) if not @packages.is_a? Array
58
- @destination_zip = destination_zip
59
- @options = options
60
- request = xml_for_us
61
- tracking_commit(:us_rates, request ,false)
43
+ Array(packages) if not packages.is_a? Array
44
+ request = xml_for_us(origin_zip, destination_zip, packages, options)
45
+ gateway_commit(:us_rates, 'RateV3', request, :live)
62
46
  end
63
47
 
64
48
  # options Are?
65
- def world_rates(country, packages, options={})
66
- @packages = packages
67
- Array(@packages) if not @packages.is_a? Array
68
- @country = country
69
- @options = options
70
- request = xml_for_world
71
- tracking_commit(:world_rates, request ,false)
49
+ def world_rates(country, packages, api_request='IntlRate', options={})
50
+ Array(packages) if not packages.is_a? Array
51
+ request = xml_for_world(country, packages, options)
52
+ gateway_commit(:world_rates, 'IntlRate', request, :live)
72
53
  end
73
54
 
74
55
  def canned_domestic_rates_test
75
- @origin_zip = "07024"
76
- @packages =[
56
+ origin_zip = "07024"
57
+ packages =[
77
58
  Package.new( 100,
78
59
  [93,10],
79
60
  :cylinder => true),
@@ -82,15 +63,16 @@ module AwesomeUsps
82
63
  [15, 10, 4.5],
83
64
  :units => :imperial)
84
65
  ]
85
- @destination_zip = "10010"
86
- @options = {}
87
- request = xml_for_us
88
- tracking_commit(:us_rates, request ,false)
66
+ destination_zip = "10010"
67
+ options = {}
68
+ request = xml_for_us(origin_zip, destination_zip, packages, options)
69
+ gateway_commit(:us_rates, 'RateV3', request, :live)
89
70
  end
90
71
 
91
72
 
92
73
  def canned_world_rates_test
93
- @packages = [
74
+ api_request='IntlRate'
75
+ packages = [
94
76
  Package.new( 100,
95
77
  [93,10],
96
78
  :cylinder => true),
@@ -99,10 +81,10 @@ module AwesomeUsps
99
81
  [15, 10, 4.5],
100
82
  :units => :imperial)
101
83
  ]
102
- @country = "Japan"
103
- @options ={}
104
- request = xml_for_world
105
- tracking_commit(:world_rates, request ,false)
84
+ country = "Japan"
85
+ options ={}
86
+ request = xml_for_world(country, packages, options)
87
+ gateway_commit(:world_rates,'IntlRate', request, :live)
106
88
  end
107
89
 
108
90
  private
@@ -120,15 +102,15 @@ module AwesomeUsps
120
102
  end
121
103
 
122
104
  # XML built with Build:XmlMarkup
123
- def xml_for_us
105
+ def xml_for_us(origin_zip, destination_zip, packages, options)
124
106
  xm = Builder::XmlMarkup.new
125
107
  xm.RateV3Request("USERID"=>"#{@username}") do
126
- @packages.each_index do |id|
127
- p = @packages[id]
108
+ packages.each_index do |id|
109
+ p = packages[id]
128
110
  xm.Package("ID" => "#{id}") {
129
- xm.Service("#{US_SERVICES[@options[:service]] || :all}")
130
- xm.ZipOrigination(@origin_zip)
131
- xm.ZipDestination(@destination_zip)
111
+ xm.Service("#{US_SERVICES[options[:service]] || :all}")
112
+ xm.ZipOrigination(origin_zip)
113
+ xm.ZipDestination(destination_zip)
132
114
  xm.Pounds("0")
133
115
  xm.Ounces("#{'%0.1f' % [p.ounces,1].max}")
134
116
  if p.options[:container] and [nil,:all,:express,:priority].include? p.service
@@ -146,17 +128,17 @@ module AwesomeUsps
146
128
  end
147
129
 
148
130
  # XML built with Build:XmlMarkup
149
- def xml_for_world
131
+ def xml_for_world(country, packages, options)
150
132
  xm = Builder::XmlMarkup.new
151
133
  xm.IntlRateRequest("USERID"=>"#{@username}") do
152
- @packages.each_index do |id|
153
- p = @packages[id]
134
+ packages.each_index do |id|
135
+ p = packages[id]
154
136
  xm.Package("ID" => "#{id}") {
155
137
  xm.Pounds("0")
156
138
  xm.Ounces("#{'%0.1f' % [p.ounces,1].max.ceil}")
157
139
  xm.MailType("#{MAIL_TYPES[p.options[:mail_type]] || 'Package'}")
158
140
  xm.ValueOfContents(p.value / 100.0) if p.value && p.currency == 'USD'
159
- xm.Country(@country)
141
+ xm.Country(country)
160
142
  }
161
143
  end
162
144
  end
@@ -224,41 +206,5 @@ module AwesomeUsps
224
206
  return international_rate_array
225
207
  end
226
208
 
227
- def tracking_commit(action, request, test = false)
228
- retries = MAX_RETRIES
229
- begin
230
- url = URI.parse("http://#{LIVE_DOMAIN}#{LIVE_RESOURCE}")
231
- req = Net::HTTP::Post.new(url.path)
232
- req.set_form_data({'API' => API_CODES[action], 'XML' => request})
233
- response = Net::HTTP.new(url.host, url.port)
234
- response.open_timeout = 2
235
- response.read_timeout = 2
236
- response.start
237
- rescue Timeout::Error
238
- if retries > 0
239
- retries -= 1
240
- retry
241
- else
242
- RAILS_DEFAULT_LOGGER.warn "The connection to the remote server timed out"
243
- return "We appoligize for the inconvience but our USPS service is busy at the moment. To retry please refresh the browser"
244
- end
245
- rescue SocketError
246
- RAILS_DEFAULT_LOGGER.error "There is a socket error with USPS plugin"
247
- return "We appoligize for the inconvience but there is a problem with our server. To retry please refresh the browser"
248
- end
249
-
250
- response = response.request(req)
251
- case response
252
- when Net::HTTPSuccess, Net::HTTPRedirection
253
- if (action == :us_rates)
254
- parse_us(response.body)
255
- else
256
- parse_world(response.body)
257
- end
258
- else
259
- RAILS_DEFAULT_LOGGER.warn("USPS plugin settings are wrong #{response}")
260
- return "USPS plugin settings are wrong #{response}"
261
- end
262
- end
263
209
  end
264
210
  end