Class: FriendlyShipping::Services::Usps
- Inherits:
-
Object
- Object
- FriendlyShipping::Services::Usps
- Includes:
- Dry::Monads::Result::Mixin
- 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', # @deprecated post_card: 'POSTCARD', large_post_card: 'LARGE POSTCARD', package_service: 'PACKAGE SERVICE', # @deprecated package_service_retail: 'PACKAGE SERVICE RETAIL' # @deprecated }.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
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#login ⇒ Object
readonly
Returns the value of attribute login.
-
#test ⇒ Object
readonly
Returns the value of attribute test.
Instance Method Summary collapse
-
#address_validation(location, debug: false) ⇒ Result<ApiResult<Array<Physical::Location>>>
Validate an address.
- #carriers ⇒ Object
-
#city_state_lookup(location, debug: false) ⇒ Result<ApiResult<Array<Physical::Location>>>
Find city and state for a given ZIP code.
-
#initialize(login:, test: true, client: HttpClient.new) ⇒ Usps
constructor
A new instance of Usps.
-
#rate_estimates(shipment, options: RateEstimateOptions.new, debug: false) ⇒ Result<Array<FriendlyShipping::Rate>>
Get rate estimates from USPS.
-
#timings(shipment, options:, debug: false) ⇒ Object
Get timing estimates from USPS.
Constructor Details
#initialize(login:, test: true, client: HttpClient.new) ⇒ Usps
Returns a new instance of Usps.
29 30 31 32 33 |
# File 'lib/friendly_shipping/services/usps.rb', line 29 def initialize(login:, test: true, client: HttpClient.new) @login = login @test = test @client = client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/friendly_shipping/services/usps.rb', line 10 def client @client end |
#login ⇒ Object (readonly)
Returns the value of attribute login.
10 11 12 |
# File 'lib/friendly_shipping/services/usps.rb', line 10 def login @login end |
#test ⇒ Object (readonly)
Returns the value of attribute test.
10 11 12 |
# File 'lib/friendly_shipping/services/usps.rb', line 10 def test @test end |
Instance Method Details
#address_validation(location, debug: false) ⇒ Result<ApiResult<Array<Physical::Location>>>
Validate an address.
76 77 78 79 80 81 82 83 |
# File 'lib/friendly_shipping/services/usps.rb', line 76 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 ⇒ Object
35 36 37 |
# File 'lib/friendly_shipping/services/usps.rb', line 35 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
89 90 91 92 93 94 95 96 |
# File 'lib/friendly_shipping/services/usps.rb', line 89 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) ⇒ Result<Array<FriendlyShipping::Rate>>
Get rate estimates from USPS
48 49 50 51 52 53 54 55 |
# File 'lib/friendly_shipping/services/usps.rb', line 48 def rate_estimates(shipment, options: RateEstimateOptions.new, debug: false) rate_request_xml = SerializeRateRequest.call(shipment: shipment, login: login, 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: ) end end |
#timings(shipment, options:, debug: false) ⇒ Object
Get timing estimates from USPS
61 62 63 64 65 66 67 68 |
# File 'lib/friendly_shipping/services/usps.rb', line 61 def timings(shipment, options:, debug: false) timings_request_xml = SerializeTimeInTransitRequest.call(shipment: shipment, 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 |