Class: Shipping::Usps

Inherits:
ShipengineBase show all
Defined in:
app/services/shipping/usps.rb

Constant Summary collapse

USPS_MAX_LENGTH_PLUS_GIRTH =

USPS limits: https://www.usps.com/ship/prepare-packages.htm
Combined length and girth (length + 2width + 2height) cannot exceed 130 inches

130
USPS_MAX_WEIGHT =

lbs

70

Constants inherited from ShipengineBase

ShipengineBase::MAX_LABEL_REFERENCE_LENGTH, ShipengineBase::RATES_TIMEOUT_MS

Instance Attribute Summary

Attributes inherited from Base

#address, #address2, #address3, #address_residential, #attention_name, #billing_account, #billing_country, #billing_zip, #ci_comments, #city, #close_report_only, #cod_amount, #cod_collection_type, #company, #country, #currency_code, #data, #debug, #declared_value, #delivery_instructions, #delivery_total_value, #description, #discount_price, #dropoff_type, #email, #eta, #export_reason, #freight_class, #freightquote_authorization_url, #freightquote_client_id, #freightquote_client_secret, #freightquote_customer_code, #freightquote_events_url, #freightquote_rating_url, #freightquote_shipping_url, #freightquote_voiding_url, #handling_instructions, #has_loading_dock, #image_type, #include_first_class_mail_options, #insured_value, #is_construction_site, #is_trade_show, #label_type, #limited_access, #line_items, #master_tracking_number, #measure_height, #measure_length, #measure_units, #measure_width, #media_mail, #multiple_piece_shipping, #negotiated_rates, #package, #package_count, #package_sequence_number, #package_total, #packages, #packaging_type, #pay_type, #phone, #pickup_datetime, #pickup_instructions, #plain_response, #price, #rate_data, #reference_number_1, #reference_number_2, #reference_number_3, #reference_number_code_1, #reference_number_code_2, #required, #requires_appointment, #requires_inside_delivery, #requires_liftgate, #response, #return_to_address, #return_to_address2, #return_to_address3, #return_to_address_residential, #return_to_attention_name, #return_to_city, #return_to_company, #return_to_country, #return_to_email, #return_to_has_loading_dock, #return_to_is_construction_site, #return_to_is_trade_show, #return_to_limited_access, #return_to_name, #return_to_phone, #return_to_requires_appointment, #return_to_requires_inside_delivery, #return_to_requires_liftgate, #return_to_state, #return_to_zip, #rl_carriers_api_key, #rl_carriers_shipping_url, #saturday_delivery, #sender_address, #sender_address2, #sender_address3, #sender_address_residential, #sender_attention_name, #sender_city, #sender_company, #sender_country, #sender_email, #sender_has_loading_dock, #sender_is_construction_site, #sender_is_trade_show, #sender_limited_access, #sender_name, #sender_phone, #sender_requires_appointment, #sender_requires_inside_delivery, #sender_requires_liftgate, #sender_state, #sender_tax_identification_number, #sender_zip, #service_code, #service_type, #services, #ship_date, #shipengine_api_key, #shipengine_canadapost_account_id, #shipengine_canadapost_parent_account_number, #shipengine_canpar_account_id, #shipengine_dhl_express_account_id, #shipengine_fed_ex_account_id, #shipengine_fed_ex_ca_account_id, #shipengine_purolator_account_id, #shipengine_ups_account_id, #shipengine_ups_ca_account_id, #shipengine_usps_account_id, #shipper_address, #shipper_address2, #shipper_address3, #shipper_address_residential, #shipper_attention_name, #shipper_city, #shipper_company, #shipper_country, #shipper_email, #shipper_has_loading_dock, #shipper_is_construction_site, #shipper_is_trade_show, #shipper_limited_access, #shipper_name, #shipper_phone, #shipper_requires_appointment, #shipper_requires_inside_delivery, #shipper_requires_liftgate, #shipper_state, #shipper_zip, #signature_confirmation, #skip_png_download, #skip_rate_test, #special_instructions, #state, #tax_identification_number, #time_in_transit, #total_shipment_weight, #transaction_type, #weight, #weight_units, #zip

Instance Method Summary collapse

Methods inherited from ShipengineBase

#get_separate_labels_per_package_from_label_pdf_url, #label, #start_tracking, #track, #valid_address?, #void

Methods inherited from Base

#fedex, #initialize, #purolator, state_from_zip, #ups, #ups_freight

Constructor Details

This class inherits a constructor from Shipping::Base

Instance Method Details

#find_rates(logger = nil) ⇒ Object

Override find_rates to pre-filter packages that exceed USPS size limits



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/shipping/usps.rb', line 13

def find_rates(logger = nil)
  logger ||= Rails.logger

  # Check if any packages exceed USPS size limits before making the API call
  # Shipping::Package uses inches(:length) for dimensions and lbs for weight
  oversized_packages = @packages&.select do |pkg|
    length = pkg.inches(:length)
    width = pkg.inches(:width)
    height = pkg.inches(:height)
    weight = pkg.lbs

    # Use ::Shipping::Container (explicit top-level) because we're inside module Shipping
    # Without :: prefix, Ruby resolves Shipping::Container as Shipping::Shipping::Container
    container = ::Shipping::Container.new(length: length, width: width, height: height, weight: weight)
    container.length_plus_girth > USPS_MAX_LENGTH_PLUS_GIRTH || (weight && weight > USPS_MAX_WEIGHT)
  end

  if oversized_packages&.any?
    logger.info("USPS find_rates skipped: #{oversized_packages.size} package(s) exceed USPS size limits (max L+G: #{USPS_MAX_LENGTH_PLUS_GIRTH} in)")
    response = {
      success: false,
      message: "USPS: Package(s) exceed USPS size limits (max length + girth: #{USPS_MAX_LENGTH_PLUS_GIRTH} inches, max weight: #{USPS_MAX_WEIGHT} lbs)",
      request: nil,
      xml: '',
      rates: []
    }
    def response.method_missing(name, *args)
      key?(name) ? self[name] : super
    end
    def response.respond_to_missing?(name, include_private = false)
      key?(name) || super
    end
    return response
  end

  super
end