usps-fork 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +89 -0
  4. data/Rakefile +15 -0
  5. data/lib/usps.rb +47 -0
  6. data/lib/usps/address.rb +72 -0
  7. data/lib/usps/client.rb +55 -0
  8. data/lib/usps/configuration.rb +17 -0
  9. data/lib/usps/errors.rb +46 -0
  10. data/lib/usps/request.rb +14 -0
  11. data/lib/usps/request/address_standardization.rb +44 -0
  12. data/lib/usps/request/base.rb +42 -0
  13. data/lib/usps/request/city_and_state_lookup.rb +32 -0
  14. data/lib/usps/request/delivery_confirmation.rb +88 -0
  15. data/lib/usps/request/delivery_confirmation_certify.rb +10 -0
  16. data/lib/usps/request/tracking_field_lookup.rb +28 -0
  17. data/lib/usps/request/tracking_lookup.rb +28 -0
  18. data/lib/usps/request/zip_code_lookup.rb +45 -0
  19. data/lib/usps/response.rb +8 -0
  20. data/lib/usps/response/address_standardization.rb +52 -0
  21. data/lib/usps/response/base.rb +13 -0
  22. data/lib/usps/response/city_and_state_lookup.rb +42 -0
  23. data/lib/usps/response/delivery_confirmation.rb +25 -0
  24. data/lib/usps/response/tracking_field_lookup.rb +34 -0
  25. data/lib/usps/response/tracking_lookup.rb +19 -0
  26. data/lib/usps/test.rb +34 -0
  27. data/lib/usps/test/address_verification.rb +31 -0
  28. data/lib/usps/test/city_and_state_lookup.rb +19 -0
  29. data/lib/usps/test/delivery_confirmation.rb +59 -0
  30. data/lib/usps/test/tracking_lookup.rb +29 -0
  31. data/lib/usps/test/zip_code_lookup.rb +37 -0
  32. data/lib/usps/track_detail.rb +49 -0
  33. data/lib/usps/version.rb +3 -0
  34. data/spec/address_spec.rb +65 -0
  35. data/spec/configuration_spec.rb +19 -0
  36. data/spec/data/address_standardization_1.xml +1 -0
  37. data/spec/data/address_standardization_2.xml +1 -0
  38. data/spec/data/city_and_state_lookup_1.xml +1 -0
  39. data/spec/data/city_and_state_lookup_2.xml +1 -0
  40. data/spec/data/delivery_confirmation_1.xml +1 -0
  41. data/spec/data/delivery_confirmation_2.xml +1 -0
  42. data/spec/data/tracking_field_lookup.xml +41 -0
  43. data/spec/data/tracking_field_lookup_2.xml +17 -0
  44. data/spec/data/tracking_lookup_1.xml +1 -0
  45. data/spec/data/tracking_lookup_2.xml +1 -0
  46. data/spec/request/address_standardization_spec.rb +45 -0
  47. data/spec/request/base_spec.rb +16 -0
  48. data/spec/request/city_and_state_lookup_spec.rb +32 -0
  49. data/spec/request/delivery_confirmation_certify_spec.rb +11 -0
  50. data/spec/request/delivery_confirmation_spec.rb +57 -0
  51. data/spec/request/tracking_field_spec.rb +20 -0
  52. data/spec/request/tracking_spec.rb +21 -0
  53. data/spec/request/zip_code_lookup_spec.rb +42 -0
  54. data/spec/response/address_standardization_spec.rb +54 -0
  55. data/spec/response/base_spec.rb +0 -0
  56. data/spec/response/city_and_state_lookup_spec.rb +27 -0
  57. data/spec/response/delivery_confirmation_spec.rb +43 -0
  58. data/spec/response/tracking_field_lookup_spec.rb +29 -0
  59. data/spec/response/tracking_lookup_spec.rb +27 -0
  60. data/spec/spec.opts +1 -0
  61. data/spec/spec_helper.rb +13 -0
  62. data/spec/track_detail_spec.rb +51 -0
  63. data/spec/usps_spec.rb +8 -0
  64. metadata +147 -0
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Request::Base do
4
+ class RequestSubclassForTesting < USPS::Request::Base
5
+ config(:api => 'testing', :tag => 'TestingTag')
6
+ end
7
+
8
+ it "should prepend the configured tag and USPS username" do
9
+ sub = RequestSubclassForTesting.new
10
+
11
+ xml = Nokogiri::XML.parse(sub.build)
12
+
13
+ expect(xml.root.name).to eq('TestingTag')
14
+ expect(xml.root.attr('USERID')).to eq(USPS.username)
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Request::CityAndStateLookup do
4
+ it "should be using the proper USPS api settings" do
5
+ USPS::Request::CityAndStateLookup.tap do |klass|
6
+ expect(klass.secure).to be_falsey
7
+ expect(klass.api).to eq('CityStateLookup')
8
+ expect(klass.tag).to eq('CityStateLookupRequest')
9
+ end
10
+ end
11
+
12
+ it "should not allow more than 5 addresses" do
13
+ expect do
14
+ USPS::Request::CityAndStateLookup.new([12345] * 10)
15
+ end.to raise_exception(ArgumentError)
16
+ end
17
+
18
+ it "should be able to build a proper request" do
19
+ request = USPS::Request::CityAndStateLookup.new(
20
+ 90210, 48917, 49423, 99111, 12345
21
+ )
22
+
23
+ xml = Nokogiri::XML.parse(request.build)
24
+
25
+ #
26
+ expect(xml.search('ZipCode[@ID="0"]/Zip5').text).to eq('90210')
27
+ expect(xml.search('ZipCode[@ID="1"]/Zip5').text).to eq('48917')
28
+ expect(xml.search('ZipCode[@ID="2"]/Zip5').text).to eq('49423')
29
+ expect(xml.search('ZipCode[@ID="3"]/Zip5').text).to eq('99111')
30
+ expect(xml.search('ZipCode[@ID="4"]/Zip5').text).to eq('12345')
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Request::DeliveryConfirmationCertify do
4
+ it 'should be using the proper USPS api settings' do
5
+ USPS::Request::DeliveryConfirmationCertify.tap do |klass|
6
+ expect(klass.secure).to be_truthy
7
+ expect(klass.api).to eq('DelivConfirmCertifyV3')
8
+ expect(klass.tag).to eq('DelivConfirmCertifyV3.0Request')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Request::DeliveryConfirmation do
4
+ it 'should be using the proper USPS api settings' do
5
+ USPS::Request::DeliveryConfirmation.tap do |klass|
6
+ expect(klass.secure).to be_truthy
7
+ expect(klass.api).to eq('DeliveryConfirmationV3')
8
+ expect(klass.tag).to eq('DeliveryConfirmationV3.0Request')
9
+ end
10
+ end
11
+
12
+ it 'should conform to test request 1' do
13
+ to = USPS::Address.new(
14
+ :name => 'Joe Customer',
15
+ :address => '6060 PRIMACY PKWY',
16
+ :address2 => 'STE 201',
17
+ :city => 'MEMPHIS',
18
+ :state => 'TN'
19
+ )
20
+
21
+ from = USPS::Address.new(
22
+ :name => 'John Smith',
23
+ :address => "475 L'Enfant Plaza, SW",
24
+ :city => 'Washington',
25
+ :state => 'DC',
26
+ :zip => 20260
27
+ )
28
+
29
+ request = USPS::Request::DeliveryConfirmation.new(to, from, 2).build
30
+
31
+ xml = Nokogiri::XML.parse(request)
32
+ expect(xml.search('Option').text).to eq('1')
33
+ expect(xml.search('ImageParameters').text).to eq('')
34
+
35
+ expect(xml.search('FromName').text).to eq('John Smith')
36
+ expect(xml.search('FromFirm').text).to eq('')
37
+ expect(xml.search('FromAddress1').text).to eq('')
38
+ expect(xml.search('FromAddress2').text).to eq("475 L'Enfant Plaza, SW")
39
+ expect(xml.search('FromCity').text).to eq('Washington')
40
+ expect(xml.search('FromState').text).to eq('DC')
41
+ expect(xml.search('FromZip5').text).to eq('20260')
42
+ expect(xml.search('FromZip4').text).to eq('')
43
+
44
+ expect(xml.search('ToName').text).to eq('Joe Customer')
45
+ expect(xml.search('ToFirm').text).to eq('')
46
+ expect(xml.search('ToAddress1').text).to eq('STE 201')
47
+ expect(xml.search('ToAddress2').text).to eq('6060 PRIMACY PKWY')
48
+ expect(xml.search('ToCity').text).to eq('MEMPHIS')
49
+ expect(xml.search('ToState').text).to eq('TN')
50
+ expect(xml.search('ToZip5').text).to eq('')
51
+ expect(xml.search('ToZip4').text).to eq('')
52
+
53
+ expect(xml.search('WeightInOunces').text).to eq('2')
54
+ expect(xml.search('ImageType').text).to eq('TIF')
55
+ expect(xml.search('ServiceType').text).to eq('Priority')
56
+ end
57
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Request::TrackingFieldLookup do
4
+
5
+ it 'should be using the proper USPS api settings' do
6
+ USPS::Request::TrackingFieldLookup.tap do |klass|
7
+ expect(klass.secure).to be_falsey
8
+ expect(klass.api).to eq('TrackV2')
9
+ expect(klass.tag).to eq('TrackFieldRequest')
10
+ end
11
+ end
12
+
13
+ it "should build a proper request" do
14
+ request = USPS::Request::TrackingFieldLookup.new("EJ958083578US").build
15
+ xml = Nokogiri::XML.parse(request)
16
+ expect(xml.search('TrackID').text).to eq('')
17
+ expect(xml.search('TrackID').attr("ID").text).to eq("EJ958083578US")
18
+ end
19
+
20
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Request::TrackingLookup do
4
+
5
+ it 'should be using the proper USPS api settings' do
6
+ USPS::Request::TrackingLookup.tap do |klass|
7
+ expect(klass.secure).to be_falsey
8
+ expect(klass.api).to eq('TrackV2')
9
+ expect(klass.tag).to eq('TrackRequest')
10
+ end
11
+ end
12
+
13
+ it "should build a proper request" do
14
+ request = USPS::Request::TrackingLookup.new("EJ958083578US").build
15
+
16
+ xml = Nokogiri::XML.parse(request)
17
+ expect(xml.search('TrackID').text).to eq('')
18
+ expect(xml.search('TrackID').attr("ID").text).to eq("EJ958083578US")
19
+ end
20
+
21
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Request::ZipCodeLookup do
4
+ it "should be using the proper USPS api settings" do
5
+ USPS::Request::ZipCodeLookup.tap do |klass|
6
+ expect(klass.secure).to be_falsey
7
+ expect(klass.api).to eq('ZipCodeLookup')
8
+ expect(klass.tag).to eq('ZipCodeLookupRequest')
9
+ end
10
+ end
11
+
12
+ it "should not allow more than 5 addresses" do
13
+ expect do
14
+ USPS::Request::AddressStandardization.new([USPS::Address.new] * 6)
15
+ end.to raise_exception(ArgumentError)
16
+ end
17
+
18
+ it "should be able to build a proper request" do
19
+ request = USPS::Request::AddressStandardization.new(
20
+ USPS::Address.new(
21
+ :name => 'Joe Jackson',
22
+ :company => 'Widget Tel Co.',
23
+ :address => '999 Serious Business Av',
24
+ :address2 => 'Suite 2000',
25
+ :city => 'Awesome Town',
26
+ :state => 'FL'
27
+ )
28
+ )
29
+
30
+ xml = Nokogiri::XML.parse(request.build)
31
+
32
+ xml.search('Address').first.tap do |node|
33
+ expect(node.attr('ID')).to eq('0')
34
+
35
+ expect(node.search('FirmName').text).to eq('Widget Tel Co.')
36
+ expect(node.search('Address1').text).to eq('Suite 2000')
37
+ expect(node.search('Address2').text).to eq('999 Serious Business Av')
38
+ expect(node.search('City').text).to eq('Awesome Town')
39
+ expect(node.search('State').text).to eq('FL')
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Response::AddressStandardization do
4
+ it "should handle test request 1" do
5
+ address = USPS::Address.new(
6
+ :address => '6406 Ivy Lane',
7
+ :city => 'Greenbelt',
8
+ :state => 'MD'
9
+ )
10
+
11
+ response = USPS::Response::AddressStandardization.new(
12
+ address, load_xml('address_standardization_1.xml')
13
+ )
14
+
15
+ standard = response.get(address)
16
+
17
+ expect(standard.address).to eq('6406 IVY LN')
18
+ expect(standard.city).to eq('GREENBELT')
19
+ expect(standard.state).to eq('MD')
20
+ expect(standard.zip).to eq('20770-1441')
21
+ end
22
+
23
+ it "should handle test request 2" do
24
+ address = USPS::Address.new(
25
+ :address => '8 Wildwood Dr',
26
+ :city => 'Old Lyme',
27
+ :state => 'CT',
28
+ :zip => '06371'
29
+ )
30
+
31
+ response = USPS::Response::AddressStandardization.new(
32
+ address, load_xml('address_standardization_2.xml')
33
+ )
34
+
35
+ standard = response.get(address)
36
+
37
+ expect(standard.address).to eq('8 WILDWOOD DR')
38
+ expect(standard.city).to eq('OLD LYME')
39
+ expect(standard.state).to eq('CT')
40
+ expect(standard.zip).to eq('06371-1844')
41
+ end
42
+
43
+ it "should propogate name to the result address as it is not returned from api" do
44
+ address = USPS::Address.new(
45
+ :name => 'Carl Sagan'
46
+ )
47
+
48
+ response = USPS::Response::AddressStandardization.new(
49
+ address, load_xml('address_standardization_2.xml')
50
+ )
51
+
52
+ expect(response.get(address).name).to eq('Carl Sagan')
53
+ end
54
+ end
File without changes
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Response::CityAndStateLookup do
4
+ it "should handle test request 1" do
5
+ response = USPS::Response::CityAndStateLookup.new(
6
+ load_xml('city_and_state_lookup_1.xml')
7
+ )
8
+
9
+ response.get(90210).tap do |r|
10
+ expect(r.zip).to eq(90210)
11
+ expect(r.city).to eq('BEVERLY HILLS')
12
+ expect(r.state).to eq('CA')
13
+ end
14
+ end
15
+
16
+ it "should handle test request 2" do
17
+ response = USPS::Response::CityAndStateLookup.new(
18
+ load_xml('city_and_state_lookup_2.xml')
19
+ )
20
+
21
+ response.get(20770).tap do |r|
22
+ expect(r.zip).to eq(20770)
23
+ expect(r.city).to eq('GREENBELT')
24
+ expect(r.state).to eq('MD')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Response::DeliveryConfirmation do
4
+ it "should handle test request 1" do
5
+ response = USPS::Response::DeliveryConfirmation.new(
6
+ load_xml('delivery_confirmation_1.xml')
7
+ )
8
+
9
+ expect(response.confirmation).to eq('420381199101805213907126809651')
10
+ expect(response.label).to eq('LABEL DATA GOES HERE')
11
+ expect(response.postnet).to eq('38119571851')
12
+
13
+ # Check the address
14
+ addy = response.address
15
+ expect(addy.name).to eq('Joe Customer')
16
+ expect(addy.address).to eq('6060 PRIMACY PKWY')
17
+ expect(addy.address2).to eq('STE 201')
18
+ expect(addy.city).to eq('Memphis')
19
+ expect(addy.state).to eq('TN')
20
+ expect(addy.zip).to eq('38119-5718')
21
+ end
22
+
23
+ # TODO: Test is still missing custom fields
24
+ it "should handle test request 2" do
25
+ response = USPS::Response::DeliveryConfirmation.new(
26
+ load_xml('delivery_confirmation_2.xml')
27
+ )
28
+
29
+ expect(response.confirmation).to eq('420381199101805213907116323891')
30
+ expect(response.label).to eq('LABEL DATA GOES HERE')
31
+ expect(response.postnet).to eq('38119571851')
32
+
33
+ # Check the address
34
+ addy = response.address
35
+ expect(addy.company).to eq('U.S. Postal Service NCSC')
36
+ expect(addy.name).to eq('Joe Customer')
37
+ expect(addy.address).to eq('6060 PRIMACY PKWY')
38
+ expect(addy.address2).to eq('STE 201')
39
+ expect(addy.city).to eq('MEMPHIS')
40
+ expect(addy.state).to eq('TN')
41
+ expect(addy.zip).to eq('38119-5718')
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe USPS::Response::TrackingFieldLookup do
5
+
6
+ it "should handle test request" do
7
+ response = USPS::Response::TrackingFieldLookup.new(load_xml("tracking_field_lookup.xml"))
8
+ expect(response).to be_a_kind_of(USPS::Response::TrackingFieldLookup)
9
+
10
+ expect(response.summary.event).to eq("DELIVERED")
11
+ expect(response.summary.city).to eq("NEWTON")
12
+ expect(response.summary.date).to eq(Time.parse("2001-05-21 12:12:00"))
13
+ expect(response.summary.event_time).to eq("12:12 pm")
14
+ expect(response.summary.event_date).to eq("May 21, 2001")
15
+ expect(response.summary.event_state).to eq("IA")
16
+ expect(response.summary.event_zip_code).to eq("50208")
17
+ expect(response.summary.name).to eq("")
18
+ expect(response.summary.firm_name).to eq("")
19
+ expect(response.summary.authorized_agent).to eq("")
20
+
21
+ expect(response.details.length).to eq(2)
22
+ expect(response.details[0].event).to eq("ENROUTE")
23
+ expect(response.details[1].event).to eq("ACCEPTANCE")
24
+ end
25
+ it "should handle no detail records" do
26
+ response = USPS::Response::TrackingFieldLookup.new(load_xml("tracking_field_lookup_2.xml"))
27
+ expect(response.details.length).to eq(0)
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::Response::TrackingLookup do
4
+
5
+ it "should handle test request 1" do
6
+ response = USPS::Response::TrackingLookup.new(load_xml("tracking_lookup_1.xml"))
7
+
8
+ expect(response.summary).to eq("Your item was delivered at 8:10 am on June 1 in Wilmington DE 19801.")
9
+ expect(response.details.length).to eq(3)
10
+
11
+ expect(response.details[0]).to eq("May 30 11:07 am NOTICE LEFT WILMINGTON DE 19801.")
12
+ expect(response.details[1]).to eq("May 30 10:08 am ARRIVAL AT UNIT WILMINGTON DE 19850.")
13
+ expect(response.details[2]).to eq("May 29 9:55 am ACCEPT OR PICKUP EDGEWATER NJ 07020.")
14
+ end
15
+
16
+ it "should handle test request 2" do
17
+ response = USPS::Response::TrackingLookup.new(load_xml("tracking_lookup_2.xml"))
18
+
19
+ expect(response.summary).to eq("Your item was delivered at 1:39 pm on June 1 in WOBURN MA 01815.")
20
+ expect(response.details.length).to eq(3)
21
+
22
+ expect(response.details[0]).to eq("May 30 7:44 am NOTICE LEFT WOBURN MA 01815.")
23
+ expect(response.details[1]).to eq("May 30 7:36 am ARRIVAL AT UNIT NORTH READING MA 01889.")
24
+ expect(response.details[2]).to eq("May 29 6:00 pm ACCEPT OR PICKUP PORTSMOUTH NH 03801.")
25
+ end
26
+
27
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,13 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default, :development)
3
+
4
+ require 'usps'
5
+ USPS.username = 'TESTING'
6
+
7
+ def load_data(path)
8
+ (@_file_cache ||= {})[path] ||= File.read(File.expand_path("../data/#{path}", __FILE__))
9
+ end
10
+
11
+ def load_xml(path)
12
+ Nokogiri::XML.parse(load_data(path))
13
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe USPS::TrackDetail do
4
+ it "should have the expected fields" do
5
+ track_detail = USPS::TrackDetail.new
6
+
7
+ expect(track_detail).to respond_to(
8
+ :event_time, :event_time=,
9
+ :event_date, :event_date=,
10
+ :event, :event=,
11
+ :event_city, :event_city=,
12
+ :event_state, :event_state=,
13
+ :event_zip_code, :event_zip_code=,
14
+ :event_country, :event_country=,
15
+ :firm_name, :firm_name=,
16
+ :name, :name=,
17
+ :authorized_agent, :authorized_agent=,
18
+ :date)
19
+ end
20
+
21
+ it "should be initializable with a hash" do
22
+ track_detail = USPS::TrackDetail.new(
23
+ :name => 'Chris',
24
+ :event_city => '123 Main St',
25
+ :event => 'DELIVERED'
26
+ )
27
+
28
+ expect(track_detail.name).to eq('Chris')
29
+ expect(track_detail.event_city).to eq('123 Main St')
30
+ expect(track_detail.event).to eq('DELIVERED')
31
+ end
32
+
33
+ it "should calculate a date" do
34
+ track_detail = USPS::TrackDetail.new(
35
+ :event_time => '9:24 pm',
36
+ :event_date => 'March 28, 2001',
37
+ )
38
+ track_detail.date.should == Time.parse('2001-03-28 21:24:00')
39
+ end
40
+
41
+ it "should handle blank dates" do
42
+ track_detail = USPS::TrackDetail.new(
43
+ :event_time => '',
44
+ :event_date => '',
45
+ )
46
+ expect(track_detail.event_time).to eq("")
47
+ expect(track_detail.event_date).to eq("")
48
+ expect(track_detail.date).to eq(nil)
49
+ end
50
+
51
+ end