awesome_usps 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/License.txt +20 -0
- data/Manifest.txt +38 -0
- data/PostInstall.txt +7 -0
- data/README.txt +48 -0
- data/Rakefile +4 -0
- data/config/hoe.rb +73 -0
- data/config/requirements.rb +15 -0
- data/lib/awesome_usps.rb +39 -0
- data/lib/awesome_usps/address_verification.rb +156 -0
- data/lib/awesome_usps/delivery_and_signature_confirmation.rb +160 -0
- data/lib/awesome_usps/electric_merchandis_return.rb +139 -0
- data/lib/awesome_usps/express_mail.rb +137 -0
- data/lib/awesome_usps/international_item.rb +43 -0
- data/lib/awesome_usps/international_mail_labels.rb +317 -0
- data/lib/awesome_usps/location.rb +97 -0
- data/lib/awesome_usps/open_distrubute_priority.rb +127 -0
- data/lib/awesome_usps/package.rb +132 -0
- data/lib/awesome_usps/service_standard.rb +143 -0
- data/lib/awesome_usps/shipping.rb +236 -0
- data/lib/awesome_usps/tracking.rb +88 -0
- data/lib/awesome_usps/version.rb +9 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +82 -0
- data/setup.rb +1585 -0
- data/tasks/deployment.rake +34 -0
- data/tasks/environment.rake +7 -0
- data/tasks/website.rake +17 -0
- data/test/test_awesome_usps.rb +11 -0
- data/test/test_helper.rb +2 -0
- data/website/index.html +145 -0
- data/website/index.txt +85 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.html.erb +48 -0
- metadata +113 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 FIXME full name
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
PostInstall.txt
|
5
|
+
README.txt
|
6
|
+
Rakefile
|
7
|
+
config/hoe.rb
|
8
|
+
config/requirements.rb
|
9
|
+
lib/awesome_usps.rb
|
10
|
+
lib/awesome_usps/version.rb
|
11
|
+
lib/awesome_usps/address_verification.rb
|
12
|
+
lib/awesome_usps/delivery_and_signature_confirmation.rb
|
13
|
+
lib/awesome_usps/electric_merchandis_return.rb
|
14
|
+
lib/awesome_usps/express_mail.rb
|
15
|
+
lib/awesome_usps/international_item.rb
|
16
|
+
lib/awesome_usps/international_mail_labels.rb
|
17
|
+
lib/awesome_usps/location.rb
|
18
|
+
lib/awesome_usps/open_distrubute_priority.rb
|
19
|
+
lib/awesome_usps/package.rb
|
20
|
+
lib/awesome_usps/service_standard.rb
|
21
|
+
lib/awesome_usps/shipping.rb
|
22
|
+
lib/awesome_usps/tracking.rb
|
23
|
+
lib/awesome_usps/version.rb
|
24
|
+
script/console
|
25
|
+
script/destroy
|
26
|
+
script/generate
|
27
|
+
script/txt2html
|
28
|
+
setup.rb
|
29
|
+
tasks/deployment.rake
|
30
|
+
tasks/environment.rake
|
31
|
+
tasks/website.rake
|
32
|
+
test/test_awesome_usps.rb
|
33
|
+
test/test_helper.rb
|
34
|
+
website/index.html
|
35
|
+
website/index.txt
|
36
|
+
website/javascripts/rounded_corners_lite.inc.js
|
37
|
+
website/stylesheets/screen.css
|
38
|
+
website/template.html.erb
|
data/PostInstall.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= awesome_usps
|
2
|
+
|
3
|
+
* FIX (url)
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
FIX (describe your package)
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* FIX (list of features or problems)
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
FIX (code sample of usage)
|
16
|
+
|
17
|
+
== REQUIREMENTS:
|
18
|
+
|
19
|
+
* FIX (list of requirements)
|
20
|
+
|
21
|
+
== INSTALL:
|
22
|
+
|
23
|
+
* FIX (sudo gem install, anything else)
|
24
|
+
|
25
|
+
== LICENSE:
|
26
|
+
|
27
|
+
(The MIT License)
|
28
|
+
|
29
|
+
Copyright (c) 2008 FIXME full name
|
30
|
+
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
32
|
+
a copy of this software and associated documentation files (the
|
33
|
+
'Software'), to deal in the Software without restriction, including
|
34
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
35
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
36
|
+
permit persons to whom the Software is furnished to do so, subject to
|
37
|
+
the following conditions:
|
38
|
+
|
39
|
+
The above copyright notice and this permission notice shall be
|
40
|
+
included in all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
43
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
44
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
45
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
46
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
47
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
48
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/config/hoe.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'awesome_usps/version'
|
2
|
+
|
3
|
+
AUTHOR = 'Matthew Bergman' # can also be an array of Authors
|
4
|
+
EMAIL = "fotoverite@gmail.com"
|
5
|
+
DESCRIPTION = "Ruby wrapper for the various USPS APIs"
|
6
|
+
GEM_NAME = 'awesome_usps' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'awesome-usps' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
EXTRA_DEPENDENCIES = [
|
11
|
+
# ['activesupport', '>= 1.3.1']
|
12
|
+
] # An array of rubygem dependencies [name, version]
|
13
|
+
|
14
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
15
|
+
@config = nil
|
16
|
+
RUBYFORGE_USERNAME = "fotoverite"
|
17
|
+
def rubyforge_username
|
18
|
+
unless @config
|
19
|
+
begin
|
20
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
21
|
+
rescue
|
22
|
+
puts <<-EOS
|
23
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
24
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
25
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
26
|
+
EOS
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
REV = nil
|
35
|
+
# UNCOMMENT IF REQUIRED:
|
36
|
+
# REV = YAML.load(`svn info`)['Revision']
|
37
|
+
VERS = AwesomeUsps::VERSION::STRING + (REV ? ".#{REV}" : "")
|
38
|
+
RDOC_OPTS = ['--quiet', '--title', 'awesome_usps documentation',
|
39
|
+
"--opname", "index.html",
|
40
|
+
"--line-numbers",
|
41
|
+
"--main", "README",
|
42
|
+
"--inline-source"]
|
43
|
+
|
44
|
+
class Hoe
|
45
|
+
def extra_deps
|
46
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
47
|
+
@extra_deps
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generate all the Rake tasks
|
52
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
53
|
+
$hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
54
|
+
p.developer(AUTHOR, EMAIL)
|
55
|
+
p.description = DESCRIPTION
|
56
|
+
p.summary = DESCRIPTION
|
57
|
+
p.url = HOMEPATH
|
58
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
59
|
+
p.test_globs = ["test/**/test_*.rb"]
|
60
|
+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
61
|
+
|
62
|
+
# == Optional
|
63
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
64
|
+
#p.extra_deps = EXTRA_DEPENDENCIES
|
65
|
+
|
66
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
67
|
+
end
|
68
|
+
|
69
|
+
CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
70
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
71
|
+
$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
72
|
+
$hoe.rsync_args = '-av --delete --ignore-errors'
|
73
|
+
$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem rubigen].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
data/lib/awesome_usps.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'hpricot'
|
2
|
+
require 'net/https'
|
3
|
+
require 'awesome_usps/package'
|
4
|
+
require 'awesome_usps/location'
|
5
|
+
require 'awesome_usps/international_item'
|
6
|
+
require 'awesome_usps/tracking'
|
7
|
+
require 'awesome_usps/shipping'
|
8
|
+
require 'awesome_usps/delivery_and_signature_confirmation'
|
9
|
+
require 'awesome_usps/service_standard'
|
10
|
+
require 'awesome_usps/open_distrubute_Priority'
|
11
|
+
require 'awesome_usps/electric_merchandis_return'
|
12
|
+
require 'awesome_usps/express_mail'
|
13
|
+
require 'awesome_usps/address_verification'
|
14
|
+
require 'awesome_usps/international_mail_labels'
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
module AwesomeUsps
|
19
|
+
class USPS
|
20
|
+
def initialize(username)
|
21
|
+
@username = validate(username)
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate(param)
|
25
|
+
raise ERROR_MSG if param.blank?
|
26
|
+
param
|
27
|
+
end
|
28
|
+
|
29
|
+
include Tracking
|
30
|
+
include Shipping
|
31
|
+
include DeliveryAndSignatureConfirmation
|
32
|
+
include ServiceStandard
|
33
|
+
include OpenDistrubutePriority
|
34
|
+
include ElectricMerchandisReturn
|
35
|
+
include ExpressMail
|
36
|
+
include AddressVerification
|
37
|
+
include InternationalMailLabels
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
module AwesomeUsps
|
2
|
+
module AddressVerification
|
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
|
+
:verify_address => 'Verify',
|
13
|
+
:zip_lookup => 'ZipCodeLookup',
|
14
|
+
:city_state_lookup =>"CityStateLookup"}
|
15
|
+
|
16
|
+
# Examines address and fills in missing information. Address must include city & state or the zip to be processed.
|
17
|
+
# Can do up to an array of five
|
18
|
+
def veryify_address(locations)
|
19
|
+
@locations = locations
|
20
|
+
@locations.to_a if not @locations.is_a? Array
|
21
|
+
@api = "AddressValidateRequest"
|
22
|
+
request = xml_for_verify_address
|
23
|
+
commit_address_information_request(:verify_address, request ,false)
|
24
|
+
end
|
25
|
+
|
26
|
+
def zip_lookup(locations)
|
27
|
+
@locations = locations
|
28
|
+
@locations = Array(@locations) if not @locations.is_a? Array
|
29
|
+
@api = "ZipCodeLookupRequest"
|
30
|
+
request = xml_for_address_information_api
|
31
|
+
commit_address_information_request(:zip_lookup, request ,false)
|
32
|
+
end
|
33
|
+
|
34
|
+
def city_state_lookup(locations)
|
35
|
+
@locations = locations
|
36
|
+
@locations = Array(@locations) if not @locations.is_a? Array
|
37
|
+
@api = "CityStateLookupRequest"
|
38
|
+
request = xml_for_address_information_api
|
39
|
+
commit_address_information_request(:zip_lookup, request ,false)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def canned_verify_address_test
|
44
|
+
@locations = [Location.new(:address2 => "6406 Ivy Lane", :city =>"Greenbelt", :state => "MD"), Location.new(:address2=>"8 Wildwood Drive", :city => "Old Lyme",:state => "CT", :zip5 => "06371" )]
|
45
|
+
@api = "AddressValidateRequest"
|
46
|
+
request = xml_for_address_information_api
|
47
|
+
commit_address_information_request(:verify_address, request ,true)
|
48
|
+
end
|
49
|
+
|
50
|
+
def canned_zip_lookup_test
|
51
|
+
@locations = [Location.new(:address2 => "6406 Ivy Lane", :city =>"Greenbelt", :state => "MD"), Location.new(:address2=>"8 Wildwood Drive", :city => "Old Lyme",:state => "CT", :zip5 => "06371" )]
|
52
|
+
@api = "ZipCodeLookupRequest"
|
53
|
+
request = xml_for_address_information_api
|
54
|
+
commit_address_information_request(:zip_lookup, request ,true)
|
55
|
+
end
|
56
|
+
|
57
|
+
def canned_city_state_lookup_test
|
58
|
+
@locations = [Location.new(:address2 => "6406 Ivy Lane", :city =>"Greenbelt", :state => "MD"), Location.new(:address2=>"8 Wildwood Drive", :city => "Old Lyme",:state => "CT", :zip5 => "06371" )]
|
59
|
+
@api = "CityStateLookupRequest"
|
60
|
+
request = xml_for_address_information_api
|
61
|
+
commit_address_information_request(:zip_lookup, request ,true)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
# XML from Builder::XmlMarkup.new
|
66
|
+
def xml_for_address_information_api
|
67
|
+
xm = Builder::XmlMarkup.new
|
68
|
+
xm.tag!("#{@api}", "USERID"=>"#{@username}") do
|
69
|
+
@locations.each_index do |id|
|
70
|
+
l=@locations[id]
|
71
|
+
xm.Address("ID" => "#{id}") do
|
72
|
+
xm.FirmName(l.firm_name)
|
73
|
+
xm.Address1(l.address1)
|
74
|
+
xm.Address2(l.address2)
|
75
|
+
if @api !="CityStateLookupRequest"
|
76
|
+
xm.City(l.city)
|
77
|
+
xm.State(l.state)
|
78
|
+
end
|
79
|
+
if @api != "ZipCodeLookupRequest"
|
80
|
+
xm.Zip5(l.zip5)
|
81
|
+
xm.Zip4(l.zip4)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
# Parses the XML into an array broken up by each address.
|
90
|
+
# For verify_address :verified will be false if multiple address were found.
|
91
|
+
|
92
|
+
def parse_address_information(xml)
|
93
|
+
i = 0
|
94
|
+
list_of_verified_addresses = []
|
95
|
+
(Hpricot.parse(xml)/:address).each do |address|
|
96
|
+
i+=1
|
97
|
+
h = {}
|
98
|
+
#Check if there was an error in an address element
|
99
|
+
if address.search("error") != []
|
100
|
+
RAILS_DEFAULT_LOGGER.info("Address number #{i} has the error '#{address.search("description").inner_html}' please fix before continuing")
|
101
|
+
|
102
|
+
return "Address number #{i} has the error '#{address.search("description").inner_html}' please fix before continuing"
|
103
|
+
end
|
104
|
+
if address.search("ReturnText") != []
|
105
|
+
h[:verified] = false
|
106
|
+
else
|
107
|
+
h[:verified] =true
|
108
|
+
end
|
109
|
+
address.children.each { |elem| h[elem.name.to_sym] = elem.inner_text unless elem.inner_text.blank? }
|
110
|
+
list_of_verified_addresses << h
|
111
|
+
end
|
112
|
+
#Check if there was an error in the basic XML formating
|
113
|
+
if list_of_verified_addresses == []
|
114
|
+
error =Hpricot.parse(xml)/:error
|
115
|
+
return error.search("description").inner_html
|
116
|
+
end
|
117
|
+
return list_of_verified_addresses
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
def commit_address_information_request(action, request, test = false)
|
123
|
+
retries = MAX_RETRIES
|
124
|
+
begin
|
125
|
+
url = URI.parse(test ? "http://#{TEST_DOMAIN}#{TEST_RESOURCE}" : "http://#{LIVE_DOMAIN}#{LIVE_RESOURCE}")
|
126
|
+
req = Net::HTTP::Post.new(url.path)
|
127
|
+
req.set_form_data({'API' => API_CODES[action], 'XML' => request})
|
128
|
+
response = Net::HTTP.new(url.host, url.port)
|
129
|
+
response.open_timeout = 5
|
130
|
+
response.read_timeout = 5
|
131
|
+
response.start
|
132
|
+
rescue Timeout::Error
|
133
|
+
if retries > 0
|
134
|
+
retries -= 1
|
135
|
+
retry
|
136
|
+
else
|
137
|
+
RAILS_DEFAULT_LOGGER.warn "The connection to the remote server timed out"
|
138
|
+
return "We appoligize for the inconvience but our USPS service is busy at the moment. To retry please refresh the browser"
|
139
|
+
|
140
|
+
end
|
141
|
+
rescue SocketError
|
142
|
+
RAILS_DEFAULT_LOGGER.error "There is a socket error with USPS plugin"
|
143
|
+
return "We appoligize for the inconvience but there is a problem with our server. To retry please refresh the browser"
|
144
|
+
end
|
145
|
+
|
146
|
+
response = response.request(req)
|
147
|
+
case response
|
148
|
+
when Net::HTTPSuccess
|
149
|
+
parse_address_information(response.body)
|
150
|
+
else
|
151
|
+
RAILS_DEFAULT_LOGGER.warn("USPS plugin settings are wrong #{response}")
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
module AwesomeUsps
|
2
|
+
module DeliveryAndSignatureConfirmation
|
3
|
+
|
4
|
+
MAX_RETRIES = 3
|
5
|
+
|
6
|
+
LIVE_DOMAIN = 'secure.shippingapis.com'
|
7
|
+
LIVE_RESOURCE = '/ShippingAPI.dll'
|
8
|
+
|
9
|
+
API_CODES = {:delivery =>'DeliveryConfirmationV3',
|
10
|
+
:delivery_confirmation_certify => "DelivConfirmCertifyV3",
|
11
|
+
:signature => "SignatureConfirmationV3",
|
12
|
+
:signature_confirmation_certify => "SignatureConfirmCertifyV3"}
|
13
|
+
|
14
|
+
def delivery_confirmation_label(origin, destination, service_type, image_type, label_type=1, options={})
|
15
|
+
@origin = origin
|
16
|
+
@destination = destination
|
17
|
+
@service_type = service_type
|
18
|
+
@image_type =image_type
|
19
|
+
@label_type = label_type
|
20
|
+
@options = options
|
21
|
+
@api = "DeliveryConfirmationV3.0Request"
|
22
|
+
request = confirmation_xml
|
23
|
+
#YES THE API IS THAT STUPID THAT WE MUST PASS WHAT TYPE OF MIME TYPE!
|
24
|
+
commit_confirmation_xml(:delivery, request, image_type, false)
|
25
|
+
end
|
26
|
+
|
27
|
+
def signature_confirmation_label(origin, destination, service_type, image_type, label_type=1, options={})
|
28
|
+
@origin = origin
|
29
|
+
@destination = destination
|
30
|
+
@service_type = service_type
|
31
|
+
@image_type =image_type
|
32
|
+
@label_type = label_type
|
33
|
+
@options = options
|
34
|
+
@api = "SignatureConfirmationV3.0Request"
|
35
|
+
request = confirmation_xml
|
36
|
+
#YES THE API IS THAT STUPID THAT WE MUST PASS WHAT TYPE OF MIME TYPE!
|
37
|
+
commit_confirmation_xml(:signature, request, image_type, false)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
def canned_delivery_confirmation_label_test
|
43
|
+
@origin = Location.new( :name=> "John Smith", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770')
|
44
|
+
@destination =Location.new( :name=> "Joe Customer", :address2 =>"136 Linwood Plz", :state => 'NJ', :city => 'Fort Lee', :zip5 => "07024")
|
45
|
+
@service_type = "Priority"
|
46
|
+
@image_type ="PDF"
|
47
|
+
@label_type = 1
|
48
|
+
@options = {:weight => 2}
|
49
|
+
@api = "DelivConfirmCertifyV3.0Request"
|
50
|
+
request = confirmation_xml
|
51
|
+
commit_confirmation_xml(:delivery_confirmation_certify, request, @image_type, true)
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
def canned_signature_confirmation_label_test
|
57
|
+
@origin = Location.new( :name=> "John Smith", :address2 => "6406 Ivy Lane", :state => 'MD', :city => 'Greenbelt', :zip5 => '20770')
|
58
|
+
@destination =Location.new( :name=> "Joe Customer", :address2 =>"136 Linwood Plz", :state => 'NJ', :city => 'Fort Lee', :zip5 => "07024")
|
59
|
+
@service_type = "Priority"
|
60
|
+
@image_type ="PDF"
|
61
|
+
@label_type = 1
|
62
|
+
@options = {:weight => 2}
|
63
|
+
@api = "SignatureConfirmCertifyV3.0Request"
|
64
|
+
request = confirmation_xml
|
65
|
+
commit_confirmation_xml(:signature_confirmation_certify, request, @image_type, true)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def confirmation_xml
|
70
|
+
xm = Builder::XmlMarkup.new
|
71
|
+
xm.tag!(@api, "USERID"=>"#{@username}") do
|
72
|
+
xm.Option(@label_type)
|
73
|
+
xm.ImageParameters #Will be used in the future. Is a required tag.
|
74
|
+
xm.FromName(@origin.name)
|
75
|
+
xm.FromFirm(@origin.firm_name)
|
76
|
+
xm.FromAddress1(@origin.address1) #Used for an apartment or suite number. Yes the API is a bit fucked.
|
77
|
+
xm.FromAddress2(@origin.address2)
|
78
|
+
xm.FromCity(@origin.city)
|
79
|
+
xm.FromState(@origin.state)
|
80
|
+
xm.FromZip5(@origin.zip5)
|
81
|
+
xm.FromZip4(@origin.zip4)
|
82
|
+
xm.ToName(@destination.name)
|
83
|
+
xm.ToFirm(@destination.firm_name)
|
84
|
+
xm.ToAddress1(@destination.address1)
|
85
|
+
xm.ToAddress2(@destination.address2)
|
86
|
+
xm.ToCity(@destination.city)
|
87
|
+
xm.ToState(@destination.state)
|
88
|
+
xm.ToZip5(@destination.zip5)
|
89
|
+
xm.ToZip4(@destination.zip4)
|
90
|
+
xm.WeightInOunces(@options[:weight_in_ounces])
|
91
|
+
xm.ServiceType(@service_type)
|
92
|
+
xm.SeparateReceiptPage(@options[:seperate])
|
93
|
+
xm.POZipCode(@options[:po_zip_code])
|
94
|
+
xm.ImageType(@image_type)
|
95
|
+
xm.LabelDate(@options[:label_date])
|
96
|
+
xm.CustomerRefNo(@options[:customer_reference_number])
|
97
|
+
xm.AddressServiceRequested(@options[:address_service])
|
98
|
+
xm.SenderName(@options[:sender_name])
|
99
|
+
xm.SenderEMail(@options[:sender_email])
|
100
|
+
xm.RecipientName(@options[:recipient_name])
|
101
|
+
xm.RecipientEmail(@options[:recipient_email])
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def parse_confirmation_label(xml, image_type)
|
106
|
+
if image_type == "TIF"
|
107
|
+
image_type = "image/tif"
|
108
|
+
else
|
109
|
+
image_type = "application/pdf"
|
110
|
+
end
|
111
|
+
parse = Hpricot.parse(xml)/:error
|
112
|
+
if parse != []
|
113
|
+
RAILS_DEFAULT_LOGGER.info "#{xml}"
|
114
|
+
return (Hpricot.parse(xml)/:description).inner_html
|
115
|
+
else
|
116
|
+
number = Hpricot.parse(xml)/:deliveryconfirmationnumber
|
117
|
+
label = Hpricot.parse(xml)/:deliveryconfirmationlabel
|
118
|
+
return {:image_type => image_type, :number => number.inner_html, :label => label.inner_html}
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
def commit_confirmation_xml(action, request, image_type, test=false)
|
124
|
+
retries = MAX_RETRIES
|
125
|
+
begin
|
126
|
+
#If and when their testing resource works again this will be useful tertiary command
|
127
|
+
url = URI.parse("https://#{LIVE_DOMAIN}#{LIVE_RESOURCE}")
|
128
|
+
req = Net::HTTP::Post.new(url.path)
|
129
|
+
req.set_form_data({'API' => API_CODES[action], 'XML' => request})
|
130
|
+
response = Net::HTTP.new(url.host, 443)
|
131
|
+
response.use_ssl
|
132
|
+
response.open_timeout = 5
|
133
|
+
response.read_timeout = 5
|
134
|
+
response.use_ssl = true
|
135
|
+
response.start
|
136
|
+
|
137
|
+
rescue Timeout::Error
|
138
|
+
if retries > 0
|
139
|
+
retries -= 1
|
140
|
+
retry
|
141
|
+
else
|
142
|
+
RAILS_DEFAULT_LOGGER.warn "The connection to the remote server timed out"
|
143
|
+
return "We appoligize for the inconvience but our USPS service is busy at the moment. To retry please refresh the browser"
|
144
|
+
end
|
145
|
+
rescue SocketError
|
146
|
+
RAILS_DEFAULT_LOGGER.error "There is a socket error with USPS plugin"
|
147
|
+
return "We appoligize for the inconvience but there is a problem with our server. To retry please refresh the browser"
|
148
|
+
end
|
149
|
+
|
150
|
+
response = response.request(req)
|
151
|
+
case response
|
152
|
+
when Net::HTTPSuccess
|
153
|
+
parse_confirmation_label(response.body, image_type)
|
154
|
+
else
|
155
|
+
RAILS_DEFAULT_LOGGER.warn("USPS plugin settings are wrong #{response}")
|
156
|
+
return "USPS plugin settings are wrong #{response}"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|