class FriendlyShipping::Services::Usps

@param [Physical::ShippingMethod] shipping_method The shipping method (“service” in USPS parlance) we want

to get rates for.

@param [Boolean] commercial_pricing Whether we prefer commercial pricing results or retail results @param [Boolean] hold_for_pickup Whether we want a rate with Hold For Pickup Service

Options for one package when rating

@param [Symbol] box_name The type of box we want to get rates for. Has to be one of the keys

of FriendlyShipping::Services::Usps::CONTAINERS.

Constants

CARRIER
CLASS_IDS
CONTAINERS
FIRST_CLASS_MAIL_TYPES
LIVE_URL
RESOURCES
SHIPPING_METHODS
TEST_URL

Attributes

client[R]
login[R]
test[R]

Public Class Methods

new(login:, test: true, client: HttpClient.new) click to toggle source
# File lib/friendly_shipping/services/usps.rb, line 40
def initialize(login:, test: true, client: HttpClient.new)
  @login = login
  @test = test
  @client = client
end

Public Instance Methods

address_validation(location, debug: false) click to toggle source

Validate an address. @param [Physical::Location] location The address we want to verify @return [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.
# File lib/friendly_shipping/services/usps.rb, line 87
def address_validation(location, debug: false)
  address_validation_request_xml = SerializeAddressValidationRequest.call(location: location, login: 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
carriers() click to toggle source
# File lib/friendly_shipping/services/usps.rb, line 46
def carriers
  Success([CARRIER])
end
city_state_lookup(location, debug: false) click to toggle source

Find city and state for a given ZIP code @param [Physical::Location] location A location object with country and ZIP code set @return [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.
# 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: 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) click to toggle source

Get rate estimates from USPS

@param [Physical::Shipment] shipment @param [FriendlyShipping::Services::Usps::RateEstimateOptions] options What options

to use for this rate estimate call

@return [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`.
# 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: 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) click to toggle source

Get timing estimates from USPS

@param [Physical::Shipment] shipment The shipment we want to estimate. Only destination zip and origin zip are used. @param [FriendlyShipping::Services::Usps::TimingOptions] options Options for the timing estimate call

# 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: 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

Private Instance Methods

base_url() click to toggle source
# File lib/friendly_shipping/services/usps.rb, line 120
def base_url
  test ? TEST_URL : LIVE_URL
end
build_request(api:, xml:, debug:) click to toggle source
# File lib/friendly_shipping/services/usps.rb, line 111
def build_request(api:, xml:, debug:)
  FriendlyShipping::Request.new(
    url: base_url,
    body: "API=#{RESOURCES[api]}&XML=#{CGI.escape xml}",
    readable_body: xml,
    debug: debug
  )
end