friendly_shipping 0.8.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) 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 +17 -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/README.md +1 -1
  13. data/friendly_shipping.gemspec +8 -17
  14. data/lib/friendly_shipping/access_token.rb +24 -0
  15. data/lib/friendly_shipping/api_error.rb +8 -4
  16. data/lib/friendly_shipping/api_error_handler.rb +11 -9
  17. data/lib/friendly_shipping/api_failure.rb +2 -7
  18. data/lib/friendly_shipping/api_result.rb +26 -4
  19. data/lib/friendly_shipping/carrier.rb +28 -8
  20. data/lib/friendly_shipping/http_client.rb +25 -8
  21. data/lib/friendly_shipping/inflections.rb +10 -0
  22. data/lib/friendly_shipping/item_options.rb +3 -0
  23. data/lib/friendly_shipping/label.rb +41 -22
  24. data/lib/friendly_shipping/package_options.rb +21 -2
  25. data/lib/friendly_shipping/rate.rb +50 -15
  26. data/lib/friendly_shipping/request.rb +23 -7
  27. data/lib/friendly_shipping/response.rb +21 -6
  28. data/lib/friendly_shipping/services/rl/api_error.rb +31 -0
  29. data/lib/friendly_shipping/services/rl/bol_options.rb +103 -0
  30. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  31. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  32. data/lib/friendly_shipping/services/rl/item_options.rb +36 -0
  33. data/lib/friendly_shipping/services/rl/package_options.rb +38 -0
  34. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +43 -0
  35. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +44 -0
  37. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +44 -0
  38. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +93 -0
  39. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +73 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +82 -0
  41. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  42. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  43. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  44. data/lib/friendly_shipping/services/rl/serialize_location.rb +53 -0
  45. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  46. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  47. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  48. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  49. data/lib/friendly_shipping/services/rl/shipment_options.rb +48 -0
  50. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  51. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  52. data/lib/friendly_shipping/services/rl.rb +187 -0
  53. data/lib/friendly_shipping/services/ship_engine/api_error.rb +31 -0
  54. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  55. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  56. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -7
  57. data/lib/friendly_shipping/services/ship_engine/label_options.rb +30 -17
  58. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -14
  59. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +75 -0
  60. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -2
  61. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -2
  62. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +27 -9
  63. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +99 -0
  64. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +5 -1
  65. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -6
  66. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +26 -0
  67. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +59 -0
  68. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +18 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  70. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  71. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  72. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  73. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  74. data/lib/friendly_shipping/services/ship_engine.rb +89 -33
  75. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  76. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +24 -5
  77. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +39 -4
  78. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +4 -2
  79. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +10 -2
  80. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +29 -11
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +7 -2
  82. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +50 -16
  83. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  85. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  86. data/lib/friendly_shipping/services/ship_engine_ltl.rb +47 -39
  87. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +28 -0
  88. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +40 -0
  89. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  90. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +153 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +111 -0
  97. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +63 -0
  98. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  99. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  100. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +119 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +90 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  103. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +50 -0
  104. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +29 -0
  105. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +10 -0
  107. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +159 -0
  108. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +10 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  111. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +47 -0
  112. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  113. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  114. data/lib/friendly_shipping/services/tforce_freight.rb +176 -0
  115. data/lib/friendly_shipping/services/ups/label_item_options.rb +0 -2
  116. data/lib/friendly_shipping/services/ups/label_options.rb +14 -5
  117. data/lib/friendly_shipping/services/ups/label_package_options.rb +1 -3
  118. data/lib/friendly_shipping/services/ups/parse_address_validation_response.rb +1 -1
  119. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  120. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +11 -3
  121. data/lib/friendly_shipping/services/ups/parse_shipment_accept_response.rb +0 -3
  122. data/lib/friendly_shipping/services/ups/parse_shipment_confirm_response.rb +0 -2
  123. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  124. data/lib/friendly_shipping/services/ups/parse_void_shipment_response.rb +0 -2
  125. data/lib/friendly_shipping/services/ups/parse_xml_response.rb +1 -1
  126. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -3
  127. data/lib/friendly_shipping/services/ups/rate_estimate_package_options.rb +0 -2
  128. data/lib/friendly_shipping/services/ups/serialize_access_request.rb +0 -2
  129. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -5
  130. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  131. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +2 -4
  132. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  133. data/lib/friendly_shipping/services/ups.rb +2 -24
  134. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -7
  135. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  136. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +2 -23
  137. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +1 -27
  138. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  139. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  140. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  141. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -5
  142. data/lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb +0 -4
  143. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -3
  144. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  145. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +30 -24
  146. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -10
  147. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +44 -0
  148. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  149. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +47 -0
  150. data/lib/friendly_shipping/services/ups_freight.rb +2 -20
  151. data/lib/friendly_shipping/services/ups_json/access_token.rb +20 -0
  152. data/lib/friendly_shipping/services/ups_json/api_error.rb +27 -0
  153. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  154. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  155. data/lib/friendly_shipping/services/ups_json/generate_city_state_lookup_payload.rb +23 -0
  156. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +208 -0
  157. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  158. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +84 -0
  159. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  160. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  161. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  162. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +75 -0
  163. data/lib/friendly_shipping/services/ups_json/label_options.rb +174 -0
  164. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +49 -0
  165. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  166. data/lib/friendly_shipping/services/ups_json/parse_city_state_lookup_response.rb +44 -0
  167. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +51 -0
  168. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  169. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  170. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  171. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +107 -0
  172. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  173. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  174. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +20 -0
  175. data/lib/friendly_shipping/services/ups_json/rates_options.rb +111 -0
  176. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  177. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  178. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  179. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  180. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  181. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  182. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +6 -6
  183. data/lib/friendly_shipping/services/usps/parse_rate_response.rb +0 -4
  184. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +6 -9
  185. data/lib/friendly_shipping/services/usps/parse_xml_response.rb +1 -1
  186. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -3
  187. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -3
  188. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -4
  189. data/lib/friendly_shipping/services/usps/shipping_methods.rb +4 -3
  190. data/lib/friendly_shipping/services/usps.rb +2 -13
  191. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +3 -3
  192. data/lib/friendly_shipping/services/usps_international/parse_rate_response.rb +0 -3
  193. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -3
  194. data/lib/friendly_shipping/services/usps_international/rate_estimate_package_options.rb +0 -2
  195. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +1 -3
  196. data/lib/friendly_shipping/services/usps_international.rb +2 -6
  197. data/lib/friendly_shipping/services/usps_ship/access_token.rb +22 -0
  198. data/lib/friendly_shipping/services/usps_ship/api_error.rb +27 -0
  199. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +55 -0
  200. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  201. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +82 -0
  202. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  203. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +122 -0
  204. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +53 -0
  205. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  206. data/lib/friendly_shipping/services/{ship_engine_ltl/bad_request.rb → usps_ship/timing_options.rb} +2 -2
  207. data/lib/friendly_shipping/services/usps_ship.rb +185 -0
  208. data/lib/friendly_shipping/shipment_options.rb +13 -1
  209. data/lib/friendly_shipping/shipping_method.rb +38 -11
  210. data/lib/friendly_shipping/structure_options.rb +38 -0
  211. data/lib/friendly_shipping/timing.rb +42 -7
  212. data/lib/friendly_shipping/version.rb +1 -1
  213. data/lib/friendly_shipping.rb +20 -15
  214. metadata +143 -174
  215. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  216. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
  217. data/lib/friendly_shipping/services/ship_engine_ltl/bad_request_handler.rb +0 -33
@@ -5,7 +5,7 @@ module FriendlyShipping
5
5
  class UpsFreight
6
6
  class ShipmentInformation
7
7
  attr_reader :documents,
8
- :number,
8
+ :pro_number,
9
9
  :pickup_request_number,
10
10
  :total,
11
11
  :bol_id,
@@ -13,10 +13,14 @@ module FriendlyShipping
13
13
  :warnings,
14
14
  :data
15
15
 
16
+ # Backwards compatibility after renaming this attribute
17
+ alias_method :number, :pro_number
18
+
16
19
  def initialize(
17
20
  total:,
18
21
  bol_id:,
19
- number:,
22
+ number: nil,
23
+ pro_number: nil,
20
24
  pickup_request_number: nil,
21
25
  documents: [],
22
26
  shipping_method: nil,
@@ -25,7 +29,7 @@ module FriendlyShipping
25
29
  )
26
30
  @total = total
27
31
  @bol_id = bol_id
28
- @number = number
32
+ @pro_number = pro_number || number
29
33
  @pickup_request_number = pickup_request_number
30
34
  @documents = documents
31
35
  @shipping_method = shipping_method
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsFreight
6
+ class ShipmentOptions < FriendlyShipping::ShipmentOptions
7
+ # @param structure_options [Array<StructureOptions>]
8
+ # @param structure_options_class [Class]
9
+ # @param kwargs [Hash]
10
+ # @option kwargs [Array<PackageOptions>] :package_options
11
+ # @option kwargs [Class] :package_options_class
12
+ def initialize(
13
+ structure_options: Set.new,
14
+ structure_options_class: RatesStructureOptions,
15
+ **kwargs
16
+ )
17
+ @structure_options = structure_options
18
+ @structure_options_class = structure_options_class
19
+ super(**kwargs)
20
+ end
21
+
22
+ # @param structure [#id]
23
+ # @return [StructureOptions]
24
+ def options_for_structure(structure)
25
+ structure_options.detect do |structure_option|
26
+ structure_option.structure_id == structure.id
27
+ end || structure_options_class.new(structure_id: nil)
28
+ end
29
+
30
+ private
31
+
32
+ # @return [Array<StructureOptions>]
33
+ attr_reader :structure_options
34
+
35
+ # @return [Class]
36
+ attr_reader :structure_options_class
37
+
38
+ # @return [Array<PackageOptions>]
39
+ # @deprecated Use {#structure_options} instead.
40
+ def package_options
41
+ warn "[DEPRECATION] `package_options` is deprecated. Please use `structure_options` instead."
42
+ @package_options
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,29 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/monads'
4
- require 'friendly_shipping/http_client'
5
- require 'friendly_shipping/services/ups_freight/shipping_methods'
6
- require 'friendly_shipping/services/ups_freight/rates_options'
7
- require 'friendly_shipping/services/ups_freight/label_options'
8
- require 'friendly_shipping/services/ups_freight/rates_package_options'
9
- require 'friendly_shipping/services/ups_freight/rates_item_options'
10
- require 'friendly_shipping/services/ups_freight/label_package_options'
11
- require 'friendly_shipping/services/ups_freight/label_item_options'
12
- require 'friendly_shipping/services/ups_freight/label_document_options'
13
- require 'friendly_shipping/services/ups_freight/label_email_options'
14
- require 'friendly_shipping/services/ups_freight/label_pickup_options'
15
- require 'friendly_shipping/services/ups_freight/label_delivery_options'
16
- require 'friendly_shipping/services/ups_freight/pickup_request_options'
17
- require 'friendly_shipping/services/ups_freight/parse_freight_label_response'
18
- require 'friendly_shipping/services/ups_freight/parse_freight_rate_response'
19
- require 'friendly_shipping/services/ups_freight/generate_freight_rate_request_hash'
20
- require 'friendly_shipping/services/ups_freight/generate_freight_ship_request_hash'
21
- require 'friendly_shipping/services/ups_freight/api_error'
3
+ require "json"
22
4
 
23
5
  module FriendlyShipping
24
6
  module Services
25
7
  class UpsFreight
26
- include Dry::Monads[:result]
8
+ include Dry::Monads::Result::Mixin
27
9
 
28
10
  attr_reader :test, :key, :login, :password, :client
29
11
 
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ # Represents an access token returned by UPS. The access token can be used to make API requests.
7
+ # Once it expires, a new token must be created.
8
+ class AccessToken < FriendlyShipping::AccessToken
9
+ # @return [Integer] the epoch time in ms when the token was issued
10
+ attr_reader :issued_at
11
+
12
+ # @param issued_at [Integer] the time the token was issued at
13
+ def initialize(issued_at:, **other_kwargs)
14
+ @issued_at = issued_at
15
+ super(**other_kwargs)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class ApiError < FriendlyShipping::ApiError
7
+ # @param [RestClient::Exception] cause
8
+ def initialize(cause)
9
+ super(cause, parse_message(cause))
10
+ end
11
+
12
+ private
13
+
14
+ # @param [RestClient::Exception] error
15
+ # @return [String]
16
+ def parse_message(error)
17
+ return error.message unless error.response
18
+
19
+ parsed_json = JSON.parse(error.response.body)
20
+ parsed_json.dig("response", "errors")&.map { |response_error| response_error["message"] }&.join(", ")
21
+ rescue JSON::ParserError, KeyError => _e
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GenerateAddressClassificationPayload
7
+ def self.call(location:)
8
+ {
9
+ XAVRequest: {
10
+ Request: {
11
+ RequestOption: '2', # Address Classification, duplicated in the url path
12
+ },
13
+ AddressKeyFormat: [
14
+ {
15
+ AddressLine: [location.address1, location.address2, location.address3].compact,
16
+ ConsigneeName: location.company_name || location.name,
17
+ CountryCode: location.country.code,
18
+ PoliticalDivision1: location.region.name,
19
+ PoliticalDivision2: location.city,
20
+ PostcodePrimaryLow: location.zip
21
+ }
22
+ ]
23
+ }
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GenerateAddressHash
7
+ class << self
8
+ def call(location:, international: false, shipper_number: nil)
9
+ snippet = {}
10
+
11
+ attention_name = location.name if international || location.company_name
12
+ snippet[:AttentionName] = attention_name if attention_name
13
+ snippet[:Name] = (location.company_name || location.name)&.slice(0..34)
14
+ snippet[:ShipperNumber] = shipper_number if shipper_number.present?
15
+ snippet[:Phone] = { Number: location.phone } if location.phone
16
+ snippet[:Address] = {
17
+ AddressLine: [location.address1, location.address2, location.address3].compact,
18
+ City: location.city,
19
+ PostalCode: location.zip,
20
+ StateProvinceCode: location.region&.code,
21
+ CountryCode: location.country&.code,
22
+ ResidentialAddressIndicator: location.commercial? ? nil : 'X'
23
+ }.compact
24
+ snippet.compact
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GenerateCityStateLookupPayload
7
+ def self.call(location:)
8
+ {
9
+ XAVRequest: {
10
+ RegionalRequestIndicator: "1",
11
+ AddressKeyFormat: [
12
+ {
13
+ PostcodePrimaryLow: location.zip,
14
+ CountryCode: location.country.code
15
+ }
16
+ ]
17
+ }
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,208 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GenerateLabelsPayload
7
+ class << self
8
+ def call(shipment:, options:)
9
+ payload = initialize_payload(shipment, options)
10
+ apply_options(payload, options)
11
+ apply_international_forms_options(payload, shipment, options) if international?(shipment)
12
+ add_packages(payload, shipment, options)
13
+ add_label_specification(payload, options)
14
+ payload.compact
15
+ end
16
+
17
+ def initialize_payload(shipment, options)
18
+ contents_description = shipment.packages.flat_map do |package|
19
+ package.items.map(&:description)
20
+ end.compact.uniq.join(', ').slice(0, 50)
21
+
22
+ {
23
+ ShipmentRequest: {
24
+ Request: {
25
+ RequestOption: options.validate_address ? "validate" : "nonvalidate",
26
+ SubVersion: options.sub_version
27
+ },
28
+ Shipment: {
29
+ Description: contents_description,
30
+ Service: {
31
+ Code: options.shipping_method.service_code
32
+ },
33
+ Shipper: GenerateAddressHash.call(location: options.shipper || shipment.origin, international: international?(shipment), shipper_number: options.shipper_number),
34
+ ShipTo: GenerateAddressHash.call(location: shipment.destination, international: international?(shipment)),
35
+ ShipmentDate: Time.current.strftime("%Y%m%d"),
36
+ PaymentInformation: {
37
+ ShipmentCharge: [
38
+ {
39
+ Type: "01", # Transportation
40
+ BillShipper: {
41
+ AccountNumber: options.shipper_number
42
+ }
43
+ }
44
+ ]
45
+ }
46
+ }
47
+ }
48
+ }
49
+ end
50
+
51
+ def apply_options(payload, options)
52
+ if options.customer_context.present?
53
+ payload[:ShipmentRequest][:Request][:TransactionReference] = { CustomerContext: options.customer_context }
54
+ end
55
+
56
+ if options.negotiated_rates
57
+ payload[:ShipmentRequest][:Shipment][:ShipmentRatingOptions] = { NegotiatedRatesIndicator: "X" }
58
+ end
59
+
60
+ payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions] = {
61
+ UPScarbonneutralIndicator: options.carbon_neutral ? "X" : nil,
62
+ SaturdayDeliveryIndicator: options.saturday_delivery ? "X" : nil,
63
+ LabelDelivery: {
64
+ LabelLinksIndicator: "X"
65
+ }
66
+ }.compact
67
+ end
68
+
69
+ def apply_international_forms_options(payload, shipment, options)
70
+ if options.terms_of_shipment_code == "DDP"
71
+ payload[:ShipmentRequest][:Shipment][:PaymentInformation][:ShipmentCharge].append(build_ddp_billing_info(options))
72
+ end
73
+
74
+ if options.paperless_invoice
75
+ payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions][:InternationalForms] = international_forms(shipment, options)
76
+ end
77
+
78
+ sold_to_location = options.sold_to || shipment.destination
79
+
80
+ international_forms_additions = {}
81
+ international_forms_additions[:Contacts] = { SoldTo: GenerateAddressHash.call(location: sold_to_location, international: true) }
82
+ international_forms_additions[:Contacts][:SoldTo].merge(
83
+ {
84
+ Phone: { Number: sold_to_location.phone },
85
+ TaxIdentificationNumber: sold_to_location.try(:tax_id_number),
86
+ EmailAddress: sold_to_location.email
87
+ }
88
+ ).compact
89
+ payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions][:InternationalForms].merge!(international_forms_additions)
90
+
91
+ return unless shipment.origin.country.code == "US" && ["CA", "PR"].include?(shipment.destination.country.code)
92
+
93
+ # Required for shipments from the US to Canada or Puerto Rico
94
+ # We'll assume USD as the origin country is the United States here.
95
+ total_value = shipment.packages.inject(Money.new(0, "USD")) do |shipment_sum, package|
96
+ shipment_sum + package.items.inject(Money.new(0, "USD")) do |package_sum, item|
97
+ package_sum + (item.cost || Money.new(0, "USD"))
98
+ end
99
+ end
100
+ payload[:ShipmentRequest][:Shipment][:InvoiceLineTotal] = {
101
+ CurrencyCode: total_value.currency.iso_code,
102
+ MonetaryValue: total_value.to_f.to_s
103
+ }
104
+ end
105
+
106
+ def international?(shipment)
107
+ shipment.origin.country != shipment.destination.country
108
+ end
109
+
110
+ def build_ddp_billing_info(options)
111
+ billing_options = options.billing_options
112
+ {
113
+ Type: "02", # Duties and taxes
114
+ BillThirdParty: {
115
+ AccountNumber: billing_options.billing_account,
116
+ Address: {
117
+ PostalCode: billing_options.billing_zip,
118
+ CountryCode: billing_options.billing_country
119
+ }
120
+ }
121
+ }
122
+ end
123
+
124
+ def international_forms(shipment, options)
125
+ invoice_date = options.invoice_date || Date.current
126
+ result = {
127
+ FormType: "01", # 01 is "Invoice"
128
+ InvoiceDate: invoice_date.strftime("%Y%m%d"),
129
+ ReasonForExport: options.reason_for_export,
130
+ CurrencyCode: options.billing_options.currency || "USD",
131
+ DeclarationStatement: options.declaration_statement,
132
+ TermsOfShipment: options.terms_of_shipment_code,
133
+ Product: []
134
+ }.compact
135
+
136
+ all_items = shipment.packages.map(&:items).map(&:to_a).flatten
137
+ all_item_options = shipment.packages.flat_map do |package|
138
+ package_options = options.options_for_package(package)
139
+ package.items.flat_map do |item|
140
+ package_options.options_for_item(item)
141
+ end
142
+ end
143
+
144
+ all_items.group_by(&:description).each do |description, items|
145
+ # This is a group of identically described items
146
+ reference_item = items.first
147
+ cost = reference_item.cost || Money.new(0, "USD")
148
+ # Get the options for this item
149
+ item_options = all_item_options.detect { |o| o.item_id == reference_item.id } || LabelItemOptions.new(item_id: nil)
150
+
151
+ product_hash = {
152
+ Description: description&.slice(0, 35), # Description of the product. Applies to all International Forms. Optional for Partial Invoice. Must be present at least once and can occur for a maximum of 3 times.
153
+ CommodityCode: item_options.commodity_code,
154
+ OriginCountryCode: item_options.country_of_origin || shipment.origin.country.code,
155
+ Unit: {
156
+ Number: items.length.to_s,
157
+ UnitOfMeasurement: {
158
+ Code: item_options.product_unit_of_measure_code
159
+ },
160
+ Value: cost.to_d
161
+ }
162
+ }
163
+
164
+ result[:Product] << product_hash
165
+ end
166
+ result
167
+ end
168
+
169
+ def add_packages(payload, shipment, options)
170
+ payload[:ShipmentRequest][:Shipment][:Package] = shipment.packages.map do |package|
171
+ package_options = options.options_for_package(package)
172
+ delivery_confirmation_code = package_level_delivery_confirmation?(shipment) ? package_options.delivery_confirmation_code : nil
173
+
174
+ GeneratePackageHash.call(
175
+ package: package,
176
+ delivery_confirmation_code: delivery_confirmation_code,
177
+ shipper_release: package_options.shipper_release,
178
+ declared_value: package_options.declared_value,
179
+ package_flavor: 'labels',
180
+ reference_numbers: package_options.reference_numbers
181
+ )
182
+ end
183
+ end
184
+
185
+ def add_label_specification(payload, options)
186
+ payload[:ShipmentRequest][:LabelSpecification] = {
187
+ LabelImageFormat: {
188
+ Code: options.label_format
189
+ },
190
+ LabelStockSize: {
191
+ Width: options.label_size[0].to_s, # "Valid value is 4."
192
+ Height: options.label_size[1].to_s # "Only valid values are 6 or 8."
193
+ }
194
+ }
195
+ end
196
+
197
+ # For certain origin/destination pairs, UPS allows each package in a shipment to have a specified
198
+ # delivery_confirmation option otherwise the delivery_confirmation option must be specified on the entire shipment.
199
+ # See https://developer.ups.com/api/reference/rating/appendix?loc=en_US for details
200
+ def package_level_delivery_confirmation?(shipment)
201
+ origin, destination = shipment.origin.country.code, shipment.destination.country.code
202
+ origin == destination || [['US', 'PR'], ['PR', 'US']].include?([origin, destination])
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end
208
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GeneratePackageHash
7
+ class << self
8
+ def call(package:,
9
+ delivery_confirmation_code: nil,
10
+ shipper_release: false,
11
+ transmit_dimensions: true,
12
+ declared_value: false,
13
+ package_flavor: nil,
14
+ reference_numbers: nil)
15
+ # UPS consistency across apis is a bit of a mess
16
+ packaging_type_key = package_flavor == "rates" ? :PackagingType : :Packaging
17
+
18
+ package_hash = {
19
+ packaging_type_key => {
20
+ Code: "02"
21
+ },
22
+ PackageWeight: {
23
+ UnitOfMeasurement: {
24
+ Code: "LBS"
25
+ },
26
+ Weight: [package.weight.convert_to(:pounds).value.to_f.round(2), 1].max.to_s
27
+ },
28
+ PackageServiceOptions: {},
29
+ Description: package.items.map(&:description).compact.uniq.join(', ').slice(0, 35) # required for return labels
30
+ }
31
+
32
+ if transmit_dimensions && package.dimensions.all? { |dim| !dim.value.zero? && !dim.value.infinite? }
33
+ package_hash[:Dimensions] = {
34
+ UnitOfMeasurement: {
35
+ Code: "IN"
36
+ },
37
+ Length: package.length.convert_to(:inches).value.to_f.round(2).to_s,
38
+ Width: package.width.convert_to(:inches).value.to_f.round(2).to_s,
39
+ Height: package.height.convert_to(:inches).value.to_f.round(2).to_s
40
+ }
41
+ end
42
+
43
+ package_hash[:PackageServiceOptions][:ShipperReleaseIndicator] = true if shipper_release
44
+ if delivery_confirmation_code
45
+ package_hash[:PackageServiceOptions][:DeliveryConfirmation] = { DCISType: delivery_confirmation_code }
46
+ end
47
+
48
+ total_value = package.items.inject(Money.new(0, "USD")) do |package_sum, item|
49
+ package_sum + (item.cost || Money.new(0, "USD"))
50
+ end
51
+
52
+ # this key is invalid for labels, UPS responds with "Accessory may not be combined with the product."
53
+ if package_flavor == "rates" && declared_value
54
+ package_hash[:PackageServiceOptions][:DeclaredValue] = {
55
+ CurrencyCode: total_value.currency.iso_code,
56
+ MonetaryValue: total_value.to_s
57
+ }
58
+ end
59
+
60
+ if reference_numbers.present?
61
+ package_hash[:ReferenceNumber] = reference_numbers.map do |code, value|
62
+ {
63
+ Code: code.to_s,
64
+ Value: value.to_s
65
+ }
66
+ end
67
+ end
68
+
69
+ package_hash[:PackageServiceOptions].compact_blank!
70
+ package_hash.compact_blank!
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GenerateRatesPayload
7
+ def self.call(shipment:, options:)
8
+ payload =
9
+ {
10
+ RateRequest: {
11
+ PickupType: {
12
+ Code: options.pickup_type_code
13
+ },
14
+ CustomerClassification: {
15
+ Code: options.customer_classification_code
16
+ },
17
+ Shipment: {
18
+ Shipper: GenerateAddressHash.call(location: options.shipper || shipment.origin, international: international?(shipment), shipper_number: options.shipper_number),
19
+ ShipTo: GenerateAddressHash.call(location: shipment.destination, international: international?(shipment)),
20
+ ShipFrom: GenerateAddressHash.call(location: shipment.origin),
21
+ PaymentDetails: {
22
+ ShipmentCharge: [
23
+ {
24
+ Type: "01", # Transportation
25
+ BillShipper: {
26
+ AccountNumber: options.shipper_number
27
+ }
28
+ }
29
+ ]
30
+ },
31
+ NumOfPieces: shipment.packages.count
32
+ }
33
+ }
34
+ }
35
+
36
+ if options.customer_context.present?
37
+ payload[:RateRequest][:Request] = { TransactionReference: { CustomerContext: options.customer_context } }
38
+ end
39
+
40
+ payload[:RateRequest][:Shipment][:Package] = shipment.packages.map do |package|
41
+ package_options = options.options_for_package(package)
42
+ transmit_dimensions = package_options.transmit_dimensions || false
43
+ GeneratePackageHash.call(
44
+ package: package,
45
+ package_flavor: 'rates',
46
+ transmit_dimensions: transmit_dimensions
47
+ )
48
+ end
49
+
50
+ if options.pickup_date && options.sub_version.to_i >= 2205
51
+ payload[:RateRequest][:Shipment][:DeliveryTimeInformation] = {
52
+ PackageBillType: "03", # Non-document
53
+ Pickup: {
54
+ Date: options.pickup_date.strftime('%Y%m%d'),
55
+ Time: options.pickup_date.strftime('%H%M%S')
56
+ }
57
+ }
58
+ end
59
+
60
+ if options.shipping_method
61
+ payload[:RateRequest][:Shipment][:Service] = { Code: options.shipping_method.service_code }
62
+ end
63
+
64
+ payload[:RateRequest][:Shipment][:ShipmentServiceOptions] = {
65
+ UPScarbonneutralIndicator: options.carbon_neutral,
66
+ SaturdayDelivery: options.saturday_delivery,
67
+ SaturdayPickup: options.saturday_pickup
68
+ }.compact
69
+
70
+ if options.negotiated_rates
71
+ payload[:RateRequest][:Shipment][:ShipmentRatingOptions] ||= {}
72
+ payload[:RateRequest][:Shipment][:ShipmentRatingOptions][:NegotiatedRatesIndicator] = "X"
73
+ end
74
+
75
+ payload
76
+ end
77
+
78
+ def self.international?(shipment)
79
+ shipment.origin.country != shipment.destination.country
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GenerateTimingsPayload
7
+ def self.call(shipment:, options:)
8
+ {
9
+ originCountryCode: shipment.origin.country.code,
10
+ originStateProvince: region_name_or_code(shipment.origin, shipment.origin.region),
11
+ originCityName: shipment.origin.city,
12
+ originPostalCode: shipment.origin.zip,
13
+ destinationCountryCode: shipment.destination.country.code,
14
+ destinationStateProvince: region_name_or_code(shipment.destination, shipment.destination.region),
15
+ destinationCityName: shipment.destination.city,
16
+ destinationPostalCode: shipment.destination.zip,
17
+ residentialIndicator: shipment.destination.commercial? ? '02' : '01',
18
+ shipDate: options.pickup.strftime('%Y-%m-%d'),
19
+ shipmentContentsCurrencyCode: 'USD',
20
+ shipmentContentsValue: shipment_contents_value(shipment),
21
+ weight: shipment.packages.sum(&:weight).value.to_s,
22
+ weightUnitOfMeasure: 'LBS',
23
+ billType: '02',
24
+ numberOfPackages: shipment.packages.size.to_s
25
+ }
26
+ end
27
+
28
+ def self.shipment_contents_value(shipment)
29
+ shipment.packages.map do |package|
30
+ package.items.sum { |item| item.cost || 0 }
31
+ end.sum.to_d
32
+ end
33
+
34
+ def self.region_name_or_code(location, region)
35
+ if location.country == "US"
36
+ region.name
37
+ else
38
+ region.code
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class Label < FriendlyShipping::Label
7
+ attr_reader :usps_tracking_number
8
+
9
+ # @param [String] usps_tracking_number The label's usps tracking number. Limited to SUREPOST
10
+ def initialize(
11
+ usps_tracking_number: nil,
12
+ **kwargs
13
+ )
14
+ @usps_tracking_number = usps_tracking_number
15
+ super(**kwargs)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end