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 => 'RateV4',
  :world_rates => 'IntlRateV2',
  :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

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)


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 127

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
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 115

def self.size_code_for(package)
  if package.inches(:max) <= 12
    'REGULAR'
  else
    'LARGE'
  end
end

Instance Method Details

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



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

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



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

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

#requirementsObject



139
140
141
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 139

def requirements
  [:login]
end

#valid_credentials?Boolean

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/active_shipping/shipping/carriers/usps.rb', line 162

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