Class: ActiveMerchant::Shipping::USPS

Inherits:
Carrier show all
Defined in:
lib/active_shipping/shipping/carriers/usps.rb

Overview

After getting an API login from USPS (looks like ‘123YOURNAME456’), run the following test:

usps = USPS.new(:login => ‘123YOURNAME456’, :test => true) usps.valid_credentials?

This will send a test request to the USPS test servers, which they ask you to do before they put your API key in production mode.

Constant Summary collapse

LIVE_DOMAIN =
'production.shippingapis.com'
LIVE_RESOURCE =
'ShippingAPI.dll'
TEST_DOMAINS =

indexed by security; e.g. TEST_DOMAINS[USE_SSL]

{ #indexed by security; e.g. TEST_DOMAINS[USE_SSL[:rates]]
  true => 'secure.shippingapis.com',
  false => 'testing.shippingapis.com'
}
TEST_RESOURCE =
'ShippingAPITest.dll'
API_CODES =
{
  :us_rates => 'RateV3',
  :world_rates => 'IntlRate',
  :test => 'CarrierPickupAvailability'
}
USE_SSL =
{
  :us_rates => false,
  :world_rates => false,
  :test => true
}
CONTAINERS =
{
  :envelope => 'Flat Rate Envelope',
  :box => 'Flat Rate Box'
}
MAIL_TYPES =
{
  :package => 'Package',
  :postcard => 'Postcards or aerogrammes',
  :matter_for_the_blind => 'Matter for the blind',
  :envelope => 'Envelope'
}
PACKAGE_PROPERTIES =
{
  'ZipOrigination' => :origin_zip,
  'ZipDestination' => :destination_zip,
  'Pounds' => :pounds,
  'Ounces' => :ounces,
  'Container' => :container,
  'Size' => :size,
  'Machinable' => :machinable,
  'Zone' => :zone,
  'Postage' => :postage,
  'Restrictions' => :restrictions
}
POSTAGE_PROPERTIES =
{
  'MailService' => :service,
  'Rate' => :rate
}
US_SERVICES =
{
  :first_class => 'FIRST CLASS',
  :priority => 'PRIORITY',
  :express => 'EXPRESS',
  :bpm => 'BPM',
  :parcel => 'PARCEL',
  :media => 'MEDIA',
  :library => 'LIBRARY',
  :all => 'ALL'
}
COUNTRY_NAME_CONVERSIONS =

TODO: get rates for “U.S. possessions and Trust Territories” like Guam, etc. via domestic rates API: www.usps.com/ncsc/lookups/abbr_state.txt TODO: figure out how USPS likes to say “Ivory Coast”

Country names: pe.usps.gov/text/Imm/immctry.htm

{
  "BA" => "Bosnia-Herzegovina",
  "CD" => "Congo, Democratic Republic of the",
  "CG" => "Congo (Brazzaville),Republic of the",
  "CI" => "Côte d'Ivoire (Ivory Coast)",
  "CK" => "Cook Islands (New Zealand)",
  "FK" => "Falkland Islands",
  "GB" => "Great Britain and Northern Ireland",
  "GE" => "Georgia, Republic of",
  "IR" => "Iran",
  "KN" => "Saint Kitts (St. Christopher and Nevis)",
  "KP" => "North Korea (Korea, Democratic People's Republic of)",
  "KR" => "South Korea (Korea, Republic of)",
  "LA" => "Laos",
  "LY" => "Libya",
  "MC" => "Monaco (France)",
  "MD" => "Moldova",
  "MK" => "Macedonia, Republic of",
  "MM" => "Burma",
  "PN" => "Pitcairn Island",
  "RU" => "Russia",
  "SK" => "Slovak Republic",
  "TK" => "Tokelau (Union) Group (Western Samoa)",
  "TW" => "Taiwan",
  "TZ" => "Tanzania",
  "VA" => "Vatican City",
  "VG" => "British Virgin Islands",
  "VN" => "Vietnam",
  "WF" => "Wallis and Futuna Islands",
  "WS" => "Western Samoa"
}
@@name =
"USPS"

Instance Attribute Summary

Attributes inherited from Carrier

#last_request, #test_mode

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Carrier

#initialize

Methods included from PostsData

included, #raw_ssl_request, #ssl_get, #ssl_post, #ssl_request

Methods included from RequiresParameters

#requires!

Constructor Details

This class inherits a constructor from ActiveMerchant::Shipping::Carrier

Class Method Details

.package_machinable?(package, options = {}) ⇒ Boolean

from info at www.usps.com/businessmail101/mailcharacteristics/parcels.htm

package.options – 25 lb. limit instead of 35 for books or other printed matter.

Defaults to false.

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
139
140
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 130

def self.package_machinable?(package, options={})
  at_least_minimum =  package.inches(:length) >= 6.0 &&
                      package.inches(:width) >= 3.0 &&
                      package.inches(:height) >= 0.25 &&
                      package.ounces >= 6.0
  at_most_maximum  =  package.inches(:length) <= 34.0 &&
                      package.inches(:width) <= 17.0 &&
                      package.inches(:height) <= 17.0 &&
                      package.pounds <= (package.options[:books] ? 25.0 : 35.0)
  at_least_minimum && at_most_maximum
end

.size_code_for(package) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 115

def self.size_code_for(package)
  total = package.inches(:length) + package.inches(:girth)
  if total <= 84
    return 'REGULAR'
  elsif total <= 108
    return 'LARGE'
  else # <= 130
    return 'OVERSIZE'
  end
end

Instance Method Details

#find_rates(origin, destination, packages, options = {}) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 146

def find_rates(origin, destination, packages, options = {})
  options = @options.merge(options)
  
  origin = Location.from(origin)
  destination = Location.from(destination)
  packages = Array(packages)
  
  #raise ArgumentError.new("USPS packages must originate in the U.S.") unless ['US',nil].include?(origin.country_code(:alpha2))
  
  
  # domestic or international?
  
  response = if ['US',nil].include?(destination.country_code(:alpha2))
    us_rates(origin, destination, packages, options)
  else
    world_rates(origin, destination, packages, options)
  end
end

#maximum_weightObject



170
171
172
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 170

def maximum_weight
  Mass.new(70, :pounds)
end

#requirementsObject



142
143
144
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 142

def requirements
  [:login]
end

#valid_credentials?Boolean

Returns:

  • (Boolean)


165
166
167
168
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 165

def valid_credentials?
  # Cannot test with find_rates because USPS doesn't allow that in test mode
  test_mode? ? canned_address_verification_works? : super
end