Class: FriendlyShipping::Services::USPSShip
- Inherits:
-
Object
- Object
- FriendlyShipping::Services::USPSShip
- Includes:
- Dry::Monads::Result::Mixin
- Defined in:
- lib/friendly_shipping/services/usps_ship.rb,
lib/friendly_shipping/services/usps_ship/api_error.rb,
lib/friendly_shipping/services/usps_ship/access_token.rb,
lib/friendly_shipping/services/usps_ship/timing_options.rb,
lib/friendly_shipping/services/usps_ship/shipping_methods.rb,
lib/friendly_shipping/services/usps_ship/machinable_package.rb,
lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb,
lib/friendly_shipping/services/usps_ship/parse_timings_response.rb,
lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb,
lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb,
lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb
Defined Under Namespace
Classes: AccessToken, ApiError, MachinablePackage, ParseRateEstimatesResponse, ParseTimingsResponse, RateEstimateOptions, RateEstimatePackageOptions, SerializeRateEstimatesRequest
Constant Summary collapse
- CARRIER =
The USPS carrier
FriendlyShipping::Carrier.new( id: 'usps', name: 'United States Postal Service', code: 'usps', shipping_methods: SHIPPING_METHODS )
- BASE_URL =
The base URL for USPS Ship requests
'https://api.usps.com'
- RESOURCES =
The USPS Ship API endpoints
{ token: 'oauth2/v3/token', rates: 'prices/v3/base-rates/search', timings: 'service-standards/v3/estimates' }.freeze
- TimingOptions =
RateEstimateOptions
- SHIPPING_METHODS =
[ ["BOUND_PRINTED_MATTER", "Bound Printed Matter"], ["FIRST-CLASS_PACKAGE_RETURN_SERVICE", "First-Class Package Return Service"], ["FIRST-CLASS_PACKAGE_SERVICE", "First-Class Package Service"], ["GROUND_RETURN_SERVICE", "Ground Return Service"], ["LIBRARY_MAIL", "Library Mail"], ["MEDIA_MAIL", "Media Mail"], ["PARCEL_SELECT", "Parcel Select"], ["PARCEL_SELECT_LIGHTWEIGHT", "Parcel Select Lightweight"], ["PRIORITY_MAIL", "Priority Mail"], ["PRIORITY_MAIL_EXPRESS", "Priority Mail Express"], ["PRIORITY_MAIL_EXPRESS_RETURN_SERVICE", "Priority Mail Express Return Service"], ["PRIORITY_MAIL_RETURN_SERVICE", "Priority Mail Return Service"], ["USPS_CONNECT_LOCAL", "USPS Connect Local"], ["USPS_CONNECT_MAIL", "USPS Connect Mail"], ["USPS_CONNECT_NEXT_DAY", "USPS Connect Next Day"], ["USPS_CONNECT_REGIONAL", "USPS Connect Regional"], ["USPS_CONNECT_SAME_DAY", "USPS Connect Same Day"], ["USPS_GROUND_ADVANTAGE", "USPS Ground Advantage"], ["USPS_GROUND_ADVANTAGE_RETURN_SERVICE", "USPS Ground Advantage Return Service"], ["USPS_RETAIL_GROUND", "USPS Retail Ground"] ].map do |code, name| FriendlyShipping::ShippingMethod.new( origin_countries: [Carmen::Country.coded("US")], name: name, service_code: code, domestic: true, international: false ) end.freeze
Instance Attribute Summary collapse
-
#access_token ⇒ AccessToken
readonly
The access token.
-
#client ⇒ HttpClient
readonly
The HTTP client.
-
#test ⇒ Boolean
readonly
Whether to use the test API version.
Instance Method Summary collapse
- #carriers ⇒ Array<Carrier>
-
#create_access_token(client_id:, client_secret:, grant_type: "client_credentials", debug: false) ⇒ ApiResult<AccessToken>
Creates an access token that can be used for future API requests.
-
#initialize(access_token:, test: true, client: nil) ⇒ USPSShip
constructor
A new instance of USPSShip.
-
#rate_estimates(shipment, options:, debug: false) ⇒ Result<ApiResult<Array<Rate>>>
Get rate estimates.
-
#timings(shipment, options:, debug: false) ⇒ Result<ApiResult<Array<Timing>>>
Get timing estimates.
Constructor Details
#initialize(access_token:, test: true, client: nil) ⇒ USPSShip
Returns a new instance of USPSShip.
41 42 43 44 45 46 |
# File 'lib/friendly_shipping/services/usps_ship.rb', line 41 def initialize(access_token:, test: true, client: nil) @access_token = access_token @test = test error_handler = ApiErrorHandler.new(api_error_class: USPSShip::ApiError) @client = client || HttpClient.new(error_handler: error_handler) end |
Instance Attribute Details
#access_token ⇒ AccessToken (readonly)
Returns the access token.
12 13 14 |
# File 'lib/friendly_shipping/services/usps_ship.rb', line 12 def access_token @access_token end |
#client ⇒ HttpClient (readonly)
Returns the HTTP client.
18 19 20 |
# File 'lib/friendly_shipping/services/usps_ship.rb', line 18 def client @client end |
#test ⇒ Boolean (readonly)
Returns whether to use the test API version.
15 16 17 |
# File 'lib/friendly_shipping/services/usps_ship.rb', line 15 def test @test end |
Instance Method Details
#carriers ⇒ Array<Carrier>
49 50 51 |
# File 'lib/friendly_shipping/services/usps_ship.rb', line 49 def carriers Success([CARRIER]) end |
#create_access_token(client_id:, client_secret:, grant_type: "client_credentials", debug: false) ⇒ ApiResult<AccessToken>
Creates an access token that can be used for future API requests.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/friendly_shipping/services/usps_ship.rb', line 61 def create_access_token( client_id:, client_secret:, grant_type: "client_credentials", debug: false ) request = FriendlyShipping::Request.new( url: "#{BASE_URL}/#{RESOURCES[:token]}", http_method: "POST", body: "client_id=#{client_id}&" \ "client_secret=#{client_secret}&" \ "grant_type=#{grant_type}", headers: { Content_Type: "application/x-www-form-urlencoded", Accept: "application/json" }, debug: debug ) client.post(request).fmap do |response| hash = JSON.parse(response.body) FriendlyShipping::ApiResult.new( AccessToken.new( token_type: hash['token_type'], expires_in: hash['expires_in'], raw_token: hash['access_token'] ), original_request: request, original_response: response ) end end |
#rate_estimates(shipment, options:, debug: false) ⇒ Result<ApiResult<Array<Rate>>>
Get rate estimates. NOTE: Since USPS Ship does not support returning multiple rates at the same time, we have to make multiple API calls for shipments with more than one package and sum the results.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/friendly_shipping/services/usps_ship.rb', line 103 def rate_estimates(shipment, options:, debug: false) api_results = shipment.packages.map do |package| yield begin rate_request = SerializeRateEstimatesRequest.call(shipment: shipment, package: package, options: ) request = build_request(api: :rates, payload: rate_request, debug: debug) client.post(request).bind do |response| ParseRateEstimatesResponse.call(response: response, request: request) end end end rates = api_results.flat_map(&:data) amounts = rates.each_with_object({}) do |rate, result| rate.amounts.each do |name, amount| result[name] ||= 0 result[name] += amount end end Success( ApiResult.new( [ FriendlyShipping::Rate.new( amounts: amounts, shipping_method: rates.first.shipping_method, data: rates.first.data ) ], original_request: api_results.first.original_request, original_response: api_results.first.original_response ) ) end |
#timings(shipment, options:, debug: false) ⇒ Result<ApiResult<Array<Timing>>>
Get timing estimates.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/friendly_shipping/services/usps_ship.rb', line 144 def timings(shipment, options:, debug: false) request = FriendlyShipping::Request.new( url: "#{BASE_URL}/#{RESOURCES[:timings]}?" \ "originZIPCode=#{shipment.origin.zip}&" \ "destinationZIPCode=#{shipment.destination.zip}&" \ "mailClass=#{.shipping_method.service_code}&" \ "acceptanceDate=#{.mailing_date.strftime('%Y-%m-%d')}", http_method: "GET", debug: debug, headers: { Accept: "application/json", Authorization: "Bearer #{access_token.raw_token}" } ) client.get(request).bind do |response| ParseTimingsResponse.call(response: response, request: request) end end |