lib/friendly_shipping/services/usps.rb in friendly_shipping-0.4.6 vs lib/friendly_shipping/services/usps.rb in friendly_shipping-0.4.7
- old
+ new
@@ -3,13 +3,16 @@
require 'friendly_shipping/http_client'
require 'friendly_shipping/services/usps/shipping_methods'
require 'friendly_shipping/services/usps/serialize_address_validation_request'
require 'friendly_shipping/services/usps/serialize_city_state_lookup_request'
require 'friendly_shipping/services/usps/serialize_rate_request'
+require 'friendly_shipping/services/usps/serialize_time_in_transit_request'
require 'friendly_shipping/services/usps/parse_address_validation_response'
require 'friendly_shipping/services/usps/parse_city_state_lookup_response'
require 'friendly_shipping/services/usps/parse_rate_response'
+require 'friendly_shipping/services/usps/parse_time_in_transit_response'
+require 'friendly_shipping/services/usps/timing_options'
module FriendlyShipping
module Services
class Usps
include Dry::Monads::Result::Mixin
@@ -27,11 +30,12 @@
LIVE_URL = 'https://secure.shippingapis.com/ShippingAPI.dll'
RESOURCES = {
address_validation: 'Verify',
city_state_lookup: 'CityStateLookup',
- rates: 'RateV4'
+ rates: 'RateV4',
+ timings: 'SDCGetLocations'
}.freeze
def initialize(login:, test: true, client: HttpClient.new)
@login = login
@test = test
@@ -61,9 +65,22 @@
rate_request_xml = SerializeRateRequest.call(shipment: shipment, login: login, shipping_method: shipping_method)
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)
+ end
+ end
+
+ # 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
+ 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
# Validate an address.
# @param [Physical::Location] location The address we want to verify