Class: FriendlyShipping::Services::Usps

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/usps/rate_estimate_options.rb,
lib/friendly_shipping/services/usps.rb,
lib/friendly_shipping/services/usps/timing_options.rb,
lib/friendly_shipping/services/usps/shipping_methods.rb,
lib/friendly_shipping/services/usps/machinable_package.rb,
lib/friendly_shipping/services/usps/parse_package_rate.rb,
lib/friendly_shipping/services/usps/parse_xml_response.rb,
lib/friendly_shipping/services/usps/choose_package_rate.rb,
lib/friendly_shipping/services/usps/parse_rate_response.rb,
lib/friendly_shipping/services/usps/serialize_rate_request.rb,
lib/friendly_shipping/services/usps/rate_estimate_package_options.rb,
lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb,
lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb,
lib/friendly_shipping/services/usps/parse_address_validation_response.rb,
lib/friendly_shipping/services/usps/serialize_time_in_transit_request.rb,
lib/friendly_shipping/services/usps/serialize_city_state_lookup_request.rb,
lib/friendly_shipping/services/usps/serialize_address_validation_request.rb

Overview

Options for one package when rating

Defined Under Namespace

Classes: ChoosePackageRate, MachinablePackage, ParseAddressValidationResponse, ParseCityStateLookupResponse, ParsePackageRate, ParseRateResponse, ParseTimeInTransitResponse, ParseXMLResponse, RateEstimateOptions, RateEstimatePackageOptions, SerializeAddressValidationRequest, SerializeCityStateLookupRequest, SerializeRateRequest, SerializeTimeInTransitRequest, TimingOptions

Constant Summary collapse

CARRIER =
FriendlyShipping::Carrier.new(
  id: 'usps',
  name: 'United States Postal Service',
  code: 'usps',
  shipping_methods: SHIPPING_METHODS
)
TEST_URL =
'https://stg-secure.shippingapis.com/ShippingAPI.dll'
LIVE_URL =
'https://secure.shippingapis.com/ShippingAPI.dll'
RESOURCES =
{
  address_validation: 'Verify',
  city_state_lookup: 'CityStateLookup',
  rates: 'RateV4',
  timings: 'SDCGetLocations'
}.freeze
CONTAINERS =
{
  variable: 'VARIABLE',
  large_flat_rate_box: 'LG FLAT RATE BOX',
  medium_flat_rate_box: 'MD FLAT RATE BOX',
  small_flat_rate_box: 'SM FLAT RATE BOX',
  regional_rate_box_a: 'REGIONALRATEBOXA',
  regional_rate_box_b: 'REGIONALRATEBOXB',
  flat_rate_envelope: 'FLAT RATE ENVELOPE',
  legal_flat_rate_envelope: 'LEGAL FLAT RATE ENVELOPE',
  padded_flat_rate_envelope: 'PADDED FLAT RATE ENVELOPE',
  gift_card_flat_rate_envelope: 'GIFT CARD FLAT RATE ENVELOPE',
  window_flat_rate_envelope: 'WINDOW FLAT RATE ENVELOPE',
  small_flat_rate_envelope: 'SM FLAT RATE ENVELOPE',
  cubic_soft_pack: 'CUBIC SOFT PACK',
  cubic_parcels: 'CUBIC PARCELS'
}.freeze
FIRST_CLASS_MAIL_TYPES =
{
  letter: 'LETTER',
  flat: 'FLAT',
  parcel: 'PARCEL',
  post_card: 'POSTCARD',
  package_service: 'PACKAGE SERVICE',
  package_service_retail: 'PACKAGE SERVICE RETAIL'
}.freeze
CLASS_IDS =
{
  priority_mail_express: {
    standard: '3',
    hold_for_pickup: '2',
    sunday_holiday_delivery: '23'
  },
  priority_mail_cubic: '999',
  ground_advantage: '1058'
}.freeze
SHIPPING_METHODS =
[
  ['FIRST CLASS', 'First-Class'],
  ['GROUND ADVANTAGE', 'Ground Advantage', CLASS_IDS[:ground_advantage]],
  ['PACKAGE SERVICES', 'Package Services'],
  ['PRIORITY', 'Priority Mail'],
  ['PRIORITY MAIL EXPRESS', 'Priority Mail Express', CLASS_IDS[:priority_mail_express].values],
  ['PRIORITY MAIL CUBIC', 'Priority Mail Cubic', CLASS_IDS[:priority_mail_cubic]],
  ['STANDARD POST', 'Standard Post'],
  ['RETAIL GROUND', 'Retail Ground'],
  ['MEDIA MAIL', 'Media Mail'],
  ['LIBRARY MAIL', 'Library Mail'],
].map do |code, name, class_ids|
  FriendlyShipping::ShippingMethod.new(
    origin_countries: [Carmen::Country.coded('US')],
    name: name,
    service_code: code,
    domestic: true,
    international: false,
    data: { class_ids: Array(class_ids) }
  )
end.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login:, test: true, client: HttpClient.new) ⇒ Usps

Returns a new instance of Usps.



40
41
42
43
44
# File 'lib/friendly_shipping/services/usps.rb', line 40

def initialize(login:, test: true, client: HttpClient.new)
  @login = 
  @test = test
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



21
22
23
# File 'lib/friendly_shipping/services/usps.rb', line 21

def client
  @client
end

#loginObject (readonly)

Returns the value of attribute login.



21
22
23
# File 'lib/friendly_shipping/services/usps.rb', line 21

def 
  @login
end

#testObject (readonly)

Returns the value of attribute test.



21
22
23
# File 'lib/friendly_shipping/services/usps.rb', line 21

def test
  @test
end

Instance Method Details

#address_validation(location, debug: false) ⇒ Result<ApiResult<Array<Physical::Location>>>

Validate an address.

Parameters:

  • location (Physical::Location)

    The address we want to verify

Returns:

  • (Result<ApiResult<Array<Physical::Location>>>)

    The response data from UPS encoded in a ‘Physical::Location` object. Name and Company name are always nil, the address lines will be made conformant to what USPS considers right. The returned location will have the address_type set if possible.



87
88
89
90
91
92
93
94
# File 'lib/friendly_shipping/services/usps.rb', line 87

def address_validation(location, debug: false)
  address_validation_request_xml = SerializeAddressValidationRequest.call(location: location, login: )
  request = build_request(api: :address_validation, xml: address_validation_request_xml, debug: debug)

  client.post(request).bind do |response|
    ParseAddressValidationResponse.call(response: response, request: request)
  end
end

#carriersObject



46
47
48
# File 'lib/friendly_shipping/services/usps.rb', line 46

def carriers
  Success([CARRIER])
end

#city_state_lookup(location, debug: false) ⇒ Result<ApiResult<Array<Physical::Location>>>

Find city and state for a given ZIP code

Parameters:

  • location (Physical::Location)

    A location object with country and ZIP code set

Returns:

  • (Result<ApiResult<Array<Physical::Location>>>)

    The response data from USPS encoded in a ‘Physical::Location` object. Country, City and ZIP code will be set, everything else nil.



100
101
102
103
104
105
106
107
# File 'lib/friendly_shipping/services/usps.rb', line 100

def city_state_lookup(location, debug: false)
  city_state_lookup_request_xml = SerializeCityStateLookupRequest.call(location: location, login: )
  request = build_request(api: :city_state_lookup, xml: city_state_lookup_request_xml, debug: debug)

  client.post(request).bind do |response|
    ParseCityStateLookupResponse.call(response: response, request: request)
  end
end

#rate_estimates(shipment, options: RateEstimateOptions.new, debug: false) ⇒ Result<Array<FriendlyShipping::Rate>>

Get rate estimates from USPS

Parameters:

Returns:

  • (Result<Array<FriendlyShipping::Rate>>)

    When successfully parsing, an array of rates in a Success Monad. When the parsing is not successful or USPS can’t give us rates, a Failure monad containing something that can be serialized into an error message using ‘to_s`.



59
60
61
62
63
64
65
66
# File 'lib/friendly_shipping/services/usps.rb', line 59

def rate_estimates(shipment, options: RateEstimateOptions.new, debug: false)
  rate_request_xml = SerializeRateRequest.call(shipment: shipment, login: , options: options)
  request = build_request(api: :rates, xml: rate_request_xml, debug: debug)

  client.post(request).bind do |response|
    ParseRateResponse.call(response: response, request: request, shipment: shipment, options: options)
  end
end

#timings(shipment, options:, debug: false) ⇒ Object

Get timing estimates from USPS

Parameters:



72
73
74
75
76
77
78
79
# File 'lib/friendly_shipping/services/usps.rb', line 72

def timings(shipment, options:, debug: false)
  timings_request_xml = SerializeTimeInTransitRequest.call(shipment: shipment, options: options, login: )
  request = build_request(api: :timings, xml: timings_request_xml, debug: debug)

  client.post(request).bind do |response|
    ParseTimeInTransitResponse.call(response: response, request: request)
  end
end