friendly_shipping 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +3 -0
  4. data/.env.test.local.template +6 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop-relaxed.yml +7 -23
  7. data/.rubocop.yml +11 -2
  8. data/.rubocop_todo.yml +21 -0
  9. data/.yardopts +6 -0
  10. data/CHANGELOG.md +71 -0
  11. data/Gemfile +17 -0
  12. data/friendly_shipping.gemspec +4 -13
  13. data/lib/friendly_shipping/api_error.rb +6 -4
  14. data/lib/friendly_shipping/api_error_handler.rb +10 -6
  15. data/lib/friendly_shipping/api_failure.rb +4 -0
  16. data/lib/friendly_shipping/api_result.rb +16 -4
  17. data/lib/friendly_shipping/carrier.rb +28 -8
  18. data/lib/friendly_shipping/http_client.rb +25 -2
  19. data/lib/friendly_shipping/inflections.rb +10 -0
  20. data/lib/friendly_shipping/item_options.rb +3 -0
  21. data/lib/friendly_shipping/label.rb +41 -20
  22. data/lib/friendly_shipping/package_options.rb +21 -2
  23. data/lib/friendly_shipping/rate.rb +50 -15
  24. data/lib/friendly_shipping/request.rb +23 -7
  25. data/lib/friendly_shipping/response.rb +21 -6
  26. data/lib/friendly_shipping/services/rl/api_error.rb +33 -0
  27. data/lib/friendly_shipping/services/rl/bol_options.rb +107 -0
  28. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  29. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  30. data/lib/friendly_shipping/services/rl/item_options.rb +38 -0
  31. data/lib/friendly_shipping/services/rl/package_options.rb +40 -0
  32. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +46 -0
  33. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +50 -0
  34. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +47 -0
  35. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +66 -0
  37. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +76 -0
  38. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +86 -0
  39. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  41. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  42. data/lib/friendly_shipping/services/rl/serialize_location.rb +46 -0
  43. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  44. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  45. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  46. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  47. data/lib/friendly_shipping/services/rl/shipment_options.rb +50 -0
  48. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  49. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  50. data/lib/friendly_shipping/services/rl.rb +204 -0
  51. data/lib/friendly_shipping/services/ship_engine/api_error.rb +33 -0
  52. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  53. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  54. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -5
  55. data/lib/friendly_shipping/services/ship_engine/label_options.rb +31 -14
  56. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -11
  57. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +77 -0
  58. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -0
  59. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -0
  60. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +26 -6
  61. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +101 -0
  62. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +4 -0
  63. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -4
  64. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +28 -0
  65. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +61 -0
  66. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +20 -0
  67. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  68. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  70. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  71. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  72. data/lib/friendly_shipping/services/ship_engine.rb +94 -21
  73. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  74. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +24 -5
  75. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +39 -4
  76. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +4 -0
  77. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +10 -0
  78. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +29 -11
  79. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +7 -2
  80. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +50 -16
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  82. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  83. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl.rb +50 -30
  85. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +43 -0
  86. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +42 -0
  87. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  88. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  89. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  90. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +165 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +113 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +65 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  97. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  98. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +121 -0
  99. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +94 -0
  100. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +58 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +31 -0
  103. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  104. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +12 -0
  105. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +162 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +12 -0
  107. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  108. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +49 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  111. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  112. data/lib/friendly_shipping/services/tforce_freight.rb +202 -0
  113. data/lib/friendly_shipping/services/ups/label_options.rb +14 -2
  114. data/lib/friendly_shipping/services/ups/label_package_options.rb +1 -1
  115. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  116. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +12 -0
  117. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  118. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -1
  119. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -1
  120. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  121. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +2 -1
  122. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  123. data/lib/friendly_shipping/services/ups.rb +1 -1
  124. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -5
  125. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  126. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +3 -20
  127. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +2 -20
  128. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  129. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  130. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  131. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -2
  132. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -1
  133. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  134. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +31 -21
  135. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -8
  136. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +46 -0
  137. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  138. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +49 -0
  139. data/lib/friendly_shipping/services/ups_freight.rb +3 -1
  140. data/lib/friendly_shipping/services/ups_json/access_token.rb +29 -0
  141. data/lib/friendly_shipping/services/ups_json/api_error.rb +29 -0
  142. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  143. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  144. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +211 -0
  145. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  146. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +86 -0
  147. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  148. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  149. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  150. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +77 -0
  151. data/lib/friendly_shipping/services/ups_json/label_options.rb +177 -0
  152. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +51 -0
  153. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  154. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +44 -0
  155. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  156. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  157. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  158. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +105 -0
  159. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  160. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  161. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +22 -0
  162. data/lib/friendly_shipping/services/ups_json/rates_options.rb +113 -0
  163. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  164. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  165. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  166. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  167. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  168. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  169. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +6 -6
  170. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +6 -6
  171. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -1
  172. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -1
  173. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -2
  174. data/lib/friendly_shipping/services/usps/shipping_methods.rb +4 -3
  175. data/lib/friendly_shipping/services/usps.rb +1 -1
  176. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +3 -3
  177. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -1
  178. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +1 -1
  179. data/lib/friendly_shipping/services/usps_international.rb +1 -1
  180. data/lib/friendly_shipping/services/usps_ship/access_token.rb +37 -0
  181. data/lib/friendly_shipping/services/usps_ship/api_error.rb +29 -0
  182. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +53 -0
  183. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  184. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +80 -0
  185. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  186. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +124 -0
  187. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +55 -0
  188. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  189. data/lib/friendly_shipping/services/{ship_engine_ltl/bad_request.rb → usps_ship/timing_options.rb} +2 -2
  190. data/lib/friendly_shipping/services/usps_ship.rb +199 -0
  191. data/lib/friendly_shipping/shipment_options.rb +13 -1
  192. data/lib/friendly_shipping/shipping_method.rb +38 -11
  193. data/lib/friendly_shipping/structure_options.rb +38 -0
  194. data/lib/friendly_shipping/timing.rb +42 -7
  195. data/lib/friendly_shipping/version.rb +1 -1
  196. data/lib/friendly_shipping.rb +6 -0
  197. metadata +140 -174
  198. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  199. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
  200. data/lib/friendly_shipping/services/ship_engine_ltl/bad_request_handler.rb +0 -33
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/usps/machinable_package'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class USPSShip
8
+ class SerializeRateEstimatesRequest
9
+ class << self
10
+ # Serialize a rate estimates request.
11
+ #
12
+ # @param shipment [Physical::Shipment]
13
+ # @param package [Physical::Package]
14
+ # @param options [RateEstimateOptions]
15
+ # @return [Hash]
16
+ def call(shipment:, package:, options:)
17
+ length, width, height = package.dimensions.sort.reverse
18
+ package_options = options.options_for_package(package)
19
+ {
20
+ originZIPCode: shipment.origin.zip,
21
+ destinationZIPCode: shipment.destination.zip,
22
+ weight: package.weight.convert_to(:lbs).value.to_f.round(2),
23
+ length: length.convert_to(:in).value.to_f.round(2),
24
+ width: width.convert_to(:in).value.to_f.round(2),
25
+ height: height.convert_to(:in).value.to_f.round(2),
26
+ mailClass: options.shipping_method.service_code,
27
+ processingCategory: processing_category(package, package_options),
28
+ rateIndicator: package_options.rate_indicator,
29
+ destinationEntryFacilityType: options.destination_entry_facility_type,
30
+ priceType: package_options.price_type,
31
+ mailingDate: options.mailing_date.strftime("%Y-%m-%d")
32
+ }
33
+ end
34
+
35
+ private
36
+
37
+ # If the processing category is machinable (or non-machinable), we can programmatically
38
+ # verify if the package is actually machinable (or non-machinable). If the processing
39
+ # category was specified incorrectly, we return the correct value instead.
40
+ #
41
+ # @param package [Physical::Package]
42
+ # @param package_options [RateEstimatePackageOptions]
43
+ # @return [String]
44
+ def processing_category(package, package_options)
45
+ if %w[MACHINABLE NON_MACHINABLE].include?(package_options.processing_category)
46
+ MachinablePackage.new(package).machinable? ? "MACHINABLE" : "NON_MACHINABLE"
47
+ else
48
+ package_options.processing_category
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class USPSShip
6
+ SHIPPING_METHODS = [
7
+ ["BOUND_PRINTED_MATTER", "Bound Printed Matter"],
8
+ ["FIRST-CLASS_PACKAGE_RETURN_SERVICE", "First-Class Package Return Service"],
9
+ ["FIRST-CLASS_PACKAGE_SERVICE", "First-Class Package Service"],
10
+ ["GROUND_RETURN_SERVICE", "Ground Return Service"],
11
+ ["LIBRARY_MAIL", "Library Mail"],
12
+ ["MEDIA_MAIL", "Media Mail"],
13
+ ["PARCEL_SELECT", "Parcel Select"],
14
+ ["PARCEL_SELECT_LIGHTWEIGHT", "Parcel Select Lightweight"],
15
+ ["PRIORITY_MAIL", "Priority Mail"],
16
+ ["PRIORITY_MAIL_EXPRESS", "Priority Mail Express"],
17
+ ["PRIORITY_MAIL_EXPRESS_RETURN_SERVICE", "Priority Mail Express Return Service"],
18
+ ["PRIORITY_MAIL_RETURN_SERVICE", "Priority Mail Return Service"],
19
+ ["USPS_CONNECT_LOCAL", "USPS Connect Local"],
20
+ ["USPS_CONNECT_MAIL", "USPS Connect Mail"],
21
+ ["USPS_CONNECT_NEXT_DAY", "USPS Connect Next Day"],
22
+ ["USPS_CONNECT_REGIONAL", "USPS Connect Regional"],
23
+ ["USPS_CONNECT_SAME_DAY", "USPS Connect Same Day"],
24
+ ["USPS_GROUND_ADVANTAGE", "USPS Ground Advantage"],
25
+ ["USPS_GROUND_ADVANTAGE_RETURN_SERVICE", "USPS Ground Advantage Return Service"],
26
+ ["USPS_RETAIL_GROUND", "USPS Retail Ground"]
27
+ ].map do |code, name|
28
+ FriendlyShipping::ShippingMethod.new(
29
+ origin_countries: [Carmen::Country.coded("US")],
30
+ name: name,
31
+ service_code: code,
32
+ domestic: true,
33
+ international: false
34
+ )
35
+ end.freeze
36
+ end
37
+ end
38
+ end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module FriendlyShipping
4
4
  module Services
5
- class ShipEngineLTL
6
- class BadRequest < ShipEngine::BadRequest; end
5
+ class USPSShip
6
+ TimingOptions = RateEstimateOptions
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,199 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/http_client'
4
+
5
+ require 'friendly_shipping/services/usps_ship/access_token'
6
+ require 'friendly_shipping/services/usps_ship/api_error'
7
+ require 'friendly_shipping/services/usps_ship/shipping_methods'
8
+
9
+ require 'friendly_shipping/services/usps_ship/rate_estimate_options'
10
+ require 'friendly_shipping/services/usps_ship/rate_estimate_package_options'
11
+ require 'friendly_shipping/services/usps_ship/timing_options'
12
+
13
+ require 'friendly_shipping/services/usps_ship/serialize_rate_estimates_request'
14
+ require 'friendly_shipping/services/usps_ship/machinable_package'
15
+
16
+ require 'friendly_shipping/services/usps_ship/parse_rate_estimates_response'
17
+ require 'friendly_shipping/services/usps_ship/parse_timings_response'
18
+
19
+ module FriendlyShipping
20
+ module Services
21
+ class USPSShip
22
+ include Dry::Monads::Result::Mixin
23
+ include Dry::Monads::Do.for(:rate_estimates)
24
+
25
+ # @return [AccessToken] the access token
26
+ attr_reader :access_token
27
+
28
+ # @return [Boolean] whether to use the test API version
29
+ attr_reader :test
30
+
31
+ # @return [HttpClient] the HTTP client
32
+ attr_reader :client
33
+
34
+ # The USPS carrier
35
+ CARRIER = FriendlyShipping::Carrier.new(
36
+ id: 'usps',
37
+ name: 'United States Postal Service',
38
+ code: 'usps',
39
+ shipping_methods: SHIPPING_METHODS
40
+ )
41
+
42
+ # The base URL for USPS Ship requests
43
+ BASE_URL = 'https://api.usps.com'
44
+
45
+ # The USPS Ship API endpoints
46
+ RESOURCES = {
47
+ token: 'oauth2/v3/token',
48
+ rates: 'prices/v3/base-rates/search',
49
+ timings: 'service-standards/v3/estimates'
50
+ }.freeze
51
+
52
+ # @param access_token [AccessToken] the access token
53
+ # @param test [Boolean] whether to use the test API version
54
+ # @param client [HttpClient] optional HTTP client to use for requests
55
+ def initialize(access_token:, test: true, client: nil)
56
+ @access_token = access_token
57
+ @test = test
58
+ error_handler = ApiErrorHandler.new(api_error_class: USPSShip::ApiError)
59
+ @client = client || HttpClient.new(error_handler: error_handler)
60
+ end
61
+
62
+ # @return [Array<Carrier>]
63
+ def carriers
64
+ Success([CARRIER])
65
+ end
66
+
67
+ # Creates an access token that can be used for future API requests.
68
+ # @see https://developer.usps.com/api/81 OAuth 2.0 docs
69
+ #
70
+ # @param client_id [String] the Client ID of your application from the OAuth Client dialog
71
+ # @param client_secret [String] the secret you created in the OAuth Client Secrets section of the OAuth Client dialog
72
+ # @param grant_type [String] defaults to "client_credentials" to indicate you wish to obtain a token representing your confidential client application
73
+ # @param debug [Boolean] whether to append debug information to the API result
74
+ # @return [ApiResult<AccessToken>] the {AccessToken} wrapped in an {ApiResult} object
75
+ def create_access_token(
76
+ client_id:,
77
+ client_secret:,
78
+ grant_type: "client_credentials",
79
+ debug: false
80
+ )
81
+ request = FriendlyShipping::Request.new(
82
+ url: "#{BASE_URL}/#{RESOURCES[:token]}",
83
+ http_method: "POST",
84
+ body: "client_id=#{client_id}&" \
85
+ "client_secret=#{client_secret}&" \
86
+ "grant_type=#{grant_type}",
87
+ headers: {
88
+ Content_Type: "application/x-www-form-urlencoded",
89
+ Accept: "application/json"
90
+ },
91
+ debug: debug
92
+ )
93
+ client.post(request).fmap do |response|
94
+ hash = JSON.parse(response.body)
95
+ FriendlyShipping::ApiResult.new(
96
+ AccessToken.new(
97
+ token_type: hash['token_type'],
98
+ expires_in: hash['expires_in'],
99
+ raw_token: hash['access_token']
100
+ ),
101
+ original_request: request,
102
+ original_response: response
103
+ )
104
+ end
105
+ end
106
+
107
+ # Get rate estimates.
108
+ # @see https://developer.usps.com/api/73#tag/Resources/operation/post-base-rates-search API documentation
109
+ #
110
+ # NOTE: Since USPS Ship does not support returning multiple rates at the same time, we have to
111
+ # make multiple API calls for shipments with more than one package and sum the results.
112
+ #
113
+ # @param shipment [Physical::Shipment] the shipment for which we want to get rates
114
+ # @param options [RateEstimateOptions] options for obtaining rates for this shipment
115
+ # @param debug [Boolean] whether to append debug information to the API result
116
+ # @return [Result<ApiResult<Array<Rate>>>] the {Rate}s wrapped in an {ApiResult} object
117
+ def rate_estimates(shipment, options:, debug: false)
118
+ api_results = shipment.packages.map do |package|
119
+ yield begin
120
+ rate_request = SerializeRateEstimatesRequest.call(shipment: shipment, package: package, options: options)
121
+ request = build_request(api: :rates, payload: rate_request, debug: debug)
122
+
123
+ client.post(request).bind do |response|
124
+ ParseRateEstimatesResponse.call(response: response, request: request)
125
+ end
126
+ end
127
+ end
128
+
129
+ rates = api_results.flat_map(&:data)
130
+ amounts = rates.each_with_object({}) do |rate, result|
131
+ rate.amounts.each do |name, amount|
132
+ result[name] ||= 0
133
+ result[name] += amount
134
+ end
135
+ end
136
+
137
+ Success(
138
+ ApiResult.new(
139
+ [
140
+ FriendlyShipping::Rate.new(
141
+ amounts: amounts,
142
+ shipping_method: rates.first.shipping_method,
143
+ data: rates.first.data
144
+ )
145
+ ],
146
+ original_request: api_results.first.original_request,
147
+ original_response: api_results.first.original_response
148
+ )
149
+ )
150
+ end
151
+
152
+ # Get timing estimates.
153
+ # @see https://developer.usps.com/api/85#tag/Resources/operation/get-estimates API documentation
154
+ #
155
+ # @param shipment [Physical::Shipment] the shipment for which we want to get timings
156
+ # @param options [TimingOptions] options for the timing estimate call
157
+ # @return [Result<ApiResult<Array<Timing>>>] the {Timing}s wrapped in an {ApiResult} object
158
+ def timings(shipment, options:, debug: false)
159
+ request = FriendlyShipping::Request.new(
160
+ url: "#{BASE_URL}/#{RESOURCES[:timings]}?" \
161
+ "originZIPCode=#{shipment.origin.zip}&" \
162
+ "destinationZIPCode=#{shipment.destination.zip}&" \
163
+ "mailClass=#{options.shipping_method.service_code}&" \
164
+ "acceptanceDate=#{options.mailing_date.strftime('%Y-%m-%d')}",
165
+ http_method: "GET",
166
+ debug: debug,
167
+ headers: {
168
+ Accept: "application/json",
169
+ Authorization: "Bearer #{access_token.raw_token}"
170
+ }
171
+ )
172
+
173
+ client.get(request).bind do |response|
174
+ ParseTimingsResponse.call(response: response, request: request)
175
+ end
176
+ end
177
+
178
+ private
179
+
180
+ # @param api [Symbol]
181
+ # @param payload [Hash]
182
+ # @param debug [Boolean]
183
+ # @return [Request]
184
+ def build_request(api:, payload:, debug:)
185
+ FriendlyShipping::Request.new(
186
+ url: "#{BASE_URL}/#{RESOURCES[api]}",
187
+ http_method: "POST",
188
+ body: payload.to_json,
189
+ debug: debug,
190
+ headers: {
191
+ Content_Type: "application/json",
192
+ Accept: "application/json",
193
+ Authorization: "Bearer #{access_token.raw_token}"
194
+ }
195
+ )
196
+ end
197
+ end
198
+ end
199
+ end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyShipping
4
+ # Base class for shipment options. Used when serializing API requests.
4
5
  class ShipmentOptions
6
+ # @param package_options [Array<PackageOptions>] the options for packages in this shipment
7
+ # @param package_options_class [Class] the class to use for package options when none are provided
5
8
  def initialize(
6
9
  package_options: Set.new,
7
10
  package_options_class: PackageOptions
@@ -10,6 +13,11 @@ module FriendlyShipping
10
13
  @package_options_class = package_options_class
11
14
  end
12
15
 
16
+ # Finds and returns package options for the given item. If options cannot be
17
+ # found, the `package_options_class` is used to construct new options.
18
+ #
19
+ # @param package [#id] the package for which to get options
20
+ # @return [PackageOptions]
13
21
  def options_for_package(package)
14
22
  package_options.detect do |package_option|
15
23
  package_option.package_id == package.id
@@ -18,6 +26,10 @@ module FriendlyShipping
18
26
 
19
27
  private
20
28
 
21
- attr_reader :package_options, :package_options_class
29
+ # @return [Array<PackageOptions>]
30
+ attr_reader :package_options
31
+
32
+ # @return [Class]
33
+ attr_reader :package_options_class
22
34
  end
23
35
  end
@@ -1,17 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyShipping
4
+ # Represents a shipping method (UPS Ground, USPS Priority, etc.) that belongs to a {Carrier}.
4
5
  class ShippingMethod
5
- attr_reader :name, :service_code, :carrier, :origin_countries, :data
6
-
7
- # @param [String] name The shipping method's name
8
- # @param [String] service_code The shipping method's service code
9
- # @param [Boolean] domestic Whether this is a domestic shipping method
10
- # @param [Boolean] international Whether this is an international shipping method
11
- # @param [Boolean] multi_package Whether this is a multi-package shipping method
12
- # @param [FriendlyShipping::Carrier] carrier This shipping method's carrier
13
- # @param [Array] origin_countries Countries this shipping method ships from
14
- # @param [Hash] data Additional carrier-specific data for this shipping method
6
+ # @return [String] the shipping method's name
7
+ attr_reader :name
8
+
9
+ # @return [String] the shipping method's service code
10
+ attr_reader :service_code
11
+
12
+ # @return [Carrier] this shipping method's carrier
13
+ attr_reader :carrier
14
+
15
+ # @return [Array<String>] countries this shipping method ships from
16
+ attr_reader :origin_countries
17
+
18
+ # @return [Hash] additional carrier-specific data for this shipping method
19
+ attr_reader :data
20
+
21
+ # @param name [String] the shipping method's name
22
+ # @param service_code [String] the shipping method's service code
23
+ # @param domestic [Boolean] whether this is a domestic shipping method
24
+ # @param international [Boolean] whether this is an international shipping method
25
+ # @param multi_package [Boolean] whether this is a multi-package shipping method
26
+ # @param carrier [Carrier] this shipping method's carrier
27
+ # @param origin_countries [Array] countries this shipping method ships from
28
+ # @param data [Hash] additional carrier-specific data for this shipping method
15
29
  def initialize(
16
30
  name: nil,
17
31
  service_code: nil,
@@ -32,20 +46,33 @@ module FriendlyShipping
32
46
  @data = data
33
47
  end
34
48
 
49
+ # Returns true if this is a domestic shipping method.
50
+ # @return [Boolean]
35
51
  def domestic?
36
52
  !!domestic
37
53
  end
38
54
 
55
+ # Returns true if this is an international shipping method.
56
+ # @return [Boolean]
39
57
  def international?
40
58
  !!international
41
59
  end
42
60
 
61
+ # Returns true if this shipping method supports multiple packages.
62
+ # @return [Boolean]
43
63
  def multi_package?
44
64
  !!multi_package
45
65
  end
46
66
 
47
67
  private
48
68
 
49
- attr_reader :domestic, :international, :multi_package
69
+ # @return [Boolean]
70
+ attr_reader :domestic
71
+
72
+ # @return [Boolean]
73
+ attr_reader :international
74
+
75
+ # @return [Boolean]
76
+ attr_reader :multi_package
50
77
  end
51
78
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ # Options for structures (pallets, skids, etc) in a shipment.
5
+ class StructureOptions
6
+ # @return [Object] unique identifier for this set of options
7
+ attr_reader :structure_id
8
+
9
+ # @param structure_id [Object] unique identifier for this set of options
10
+ # @param package_options [Array<PackageOptions>] the options for packages in this structure
11
+ # @param package_options_class [Class] the class to use for package options when none are provided
12
+ def initialize(
13
+ structure_id:,
14
+ package_options: [],
15
+ package_options_class: PackageOptions
16
+ )
17
+ @structure_id = structure_id
18
+ @package_options = package_options
19
+ @package_options_class = package_options_class
20
+ end
21
+
22
+ # @param package [#id] the package for which to get options
23
+ # @return [PackageOptions]
24
+ def options_for_package(package)
25
+ package_options.detect do |package_option|
26
+ package_option.package_id == package.id
27
+ end || package_options_class.new(package_id: nil)
28
+ end
29
+
30
+ private
31
+
32
+ # @return [Array<PackageOptions>]
33
+ attr_reader :package_options
34
+
35
+ # @return [Class]
36
+ attr_reader :package_options_class
37
+ end
38
+ end
@@ -1,27 +1,62 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyShipping
4
+ # Base class for a transit timing estimate returned by a carrier API.
4
5
  class Timing
5
- attr_reader :shipping_method,
6
- :pickup,
7
- :delivery,
8
- :guaranteed,
9
- :properties
6
+ # @return [ShippingMethod] this timing's shipping method
7
+ attr_reader :shipping_method
10
8
 
9
+ # @return [Time] the pickup estimate
10
+ attr_reader :pickup
11
+
12
+ # @return [Time] the delivery estimate
13
+ attr_reader :delivery
14
+
15
+ # @return [Boolean] whether the delivery estimate is guaranteed
16
+ attr_reader :guaranteed
17
+
18
+ # @return [Array] any warnings that were generated
19
+ attr_reader :warnings
20
+
21
+ # @return [Array] any errors that were generated
22
+ attr_reader :errors
23
+
24
+ # @return [Hash] additional data related to the timing (DEPRECATED: use `data` instead)
25
+ attr_reader :properties
26
+
27
+ # @return [Hash] additional data related to the timing
28
+ attr_reader :data
29
+
30
+ # @param shipping_method [ShippingMethod] this timing's shipping method
31
+ # @param pickup [Time] the pickup estimate
32
+ # @param delivery [Time] the delivery estimate
33
+ # @param guaranteed [Boolean] whether the delivery estimate is guaranteed
34
+ # @param warnings [Array] any warnings that were generated
35
+ # @param errors [Array] any errors that were generated
36
+ # @param properties [Hash] additional data related to the timing
37
+ # @param data [Hash] additional data related to the timing (DEPRECATED: use `data` instead)
11
38
  def initialize(
12
39
  shipping_method:,
13
40
  pickup:,
14
41
  delivery:,
15
42
  guaranteed: false,
16
- properties: {}
43
+ warnings: [],
44
+ errors: [],
45
+ properties: {},
46
+ data: {}
17
47
  )
18
48
  @shipping_method = shipping_method
19
49
  @pickup = pickup
20
50
  @delivery = delivery
21
51
  @guaranteed = guaranteed
22
- @properties = properties
52
+ @warnings = warnings
53
+ @errors = errors
54
+ @data = @properties = properties.presence || data
55
+ warn "[DEPRECATION] `properties` is deprecated. Please use `data` instead." if properties.present?
23
56
  end
24
57
 
58
+ # Calculates and returns the time between pickup and delivery.
59
+ # @return [Time]
25
60
  def time_in_transit
26
61
  delivery - pickup
27
62
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyShipping
4
- VERSION = "0.8.1"
4
+ VERSION = "0.9.0"
5
5
  end
@@ -7,21 +7,27 @@ require "money"
7
7
  Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
8
8
 
9
9
  require "friendly_shipping/version"
10
+ require "friendly_shipping/inflections"
10
11
  require "friendly_shipping/request"
11
12
  require "friendly_shipping/response"
12
13
  require "friendly_shipping/carrier"
13
14
  require "friendly_shipping/shipping_method"
15
+ require "friendly_shipping/structure_options"
14
16
  require "friendly_shipping/label"
15
17
  require "friendly_shipping/rate"
16
18
  require "friendly_shipping/api_result"
17
19
  require "friendly_shipping/api_failure"
18
20
 
21
+ require 'friendly_shipping/services/rl'
19
22
  require "friendly_shipping/services/ship_engine"
20
23
  require 'friendly_shipping/services/ship_engine_ltl'
24
+ require "friendly_shipping/services/tforce_freight"
21
25
  require "friendly_shipping/services/ups"
22
26
  require "friendly_shipping/services/ups_freight"
27
+ require "friendly_shipping/services/ups_json"
23
28
  require "friendly_shipping/services/usps"
24
29
  require "friendly_shipping/services/usps_international"
30
+ require "friendly_shipping/services/usps_ship"
25
31
 
26
32
  module FriendlyShipping
27
33
  end