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
@@ -4,26 +4,72 @@ module FriendlyShipping
4
4
  module Services
5
5
  class UpsFreight
6
6
  class GenerateCommodityInformation
7
- def self.call(shipment:, options:)
8
- shipment.packages.flat_map do |package|
9
- package_options = options.options_for_package(package)
10
- package.items.map do |item|
11
- item_options = package_options.options_for_item(item)
12
- {
13
- # This is a required field
14
- Description: item.description || 'Commodities',
15
- Weight: {
16
- UnitOfMeasurement: {
17
- Code: 'LBS' # Only Pounds are supported
7
+ class << self
8
+ # @param shipment [Physical::Shipment]
9
+ # @param options [#options_for_package, #options_for_structure]
10
+ # @return [Array<Hash>]
11
+ def call(shipment:, options:)
12
+ if shipment.packages.any?
13
+ warn "[DEPRECATION] `packages` is deprecated. Please use `structures` instead."
14
+ serialize_packages(shipment.packages, options)
15
+ else
16
+ serialize_structures(shipment.structures, options)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ # @param packages [Array<Physical::Package>]
23
+ # @param options [#options_for_package]
24
+ # @return [Array<Hash>]
25
+ def serialize_packages(packages, options)
26
+ packages.flat_map do |package|
27
+ package_options = options.options_for_package(package)
28
+ package.items.map do |item|
29
+ item_options = package_options.options_for_item(item)
30
+ {
31
+ # This is a required field
32
+ Description: item.description || 'Commodities',
33
+ Weight: {
34
+ UnitOfMeasurement: {
35
+ Code: 'LBS' # Only Pounds are supported
36
+ },
37
+ Value: item.weight.convert_to(:pounds).value.to_f.round(2).to_s
38
+ },
39
+ NumberOfPieces: '1', # We won't support this yet.
40
+ PackagingType: {
41
+ Code: item_options.packaging_code
42
+ },
43
+ FreightClass: item_options.freight_class
44
+ }
45
+ end
46
+ end
47
+ end
48
+
49
+ # @param structures [Array<Physical::Structure>]
50
+ # @param options [#options_for_structure]
51
+ # @return [Array<Hash>]
52
+ def serialize_structures(structures, options)
53
+ structures.flat_map do |structure|
54
+ structure_options = options.options_for_structure(structure)
55
+ structure.packages.map do |package|
56
+ package_options = structure_options.options_for_package(package)
57
+ {
58
+ # This is a required field
59
+ Description: package.description || 'Commodities',
60
+ Weight: {
61
+ UnitOfMeasurement: {
62
+ Code: 'LBS' # Only Pounds are supported
63
+ },
64
+ Value: package.weight.convert_to(:pounds).value.to_f.round(2).to_s
65
+ },
66
+ NumberOfPieces: '1', # We won't support this yet.
67
+ PackagingType: {
68
+ Code: package_options.packaging_code
18
69
  },
19
- Value: item.weight.convert_to(:pounds).value.to_f.round(2).to_s
20
- },
21
- NumberOfPieces: '1', # We won't support this yet.
22
- PackagingType: {
23
- Code: item_options.packaging_code
24
- },
25
- FreightClass: item_options.freight_class
26
- }
70
+ FreightClass: package_options.freight_class
71
+ }
72
+ end
27
73
  end
28
74
  end
29
75
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups_freight/generate_location_hash'
4
- require 'friendly_shipping/services/ups_freight/generate_pickup_request_hash'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  class UpsFreight
@@ -22,31 +19,13 @@ module FriendlyShipping
22
19
  Commodity: options.commodity_information_generator.call(shipment: shipment, options: options),
23
20
  TimeInTransitIndicator: 'true',
24
21
  PickupRequest: GeneratePickupRequestHash.call(pickup_request_options: options.pickup_request_options),
25
- }.compact.merge(handling_units(shipment, options).reduce(&:merge).to_h)
22
+ }.compact.
23
+ merge(GenerateHandlingUnitsHash.call(shipment: shipment, options: options))
26
24
  }
27
25
  end
28
26
 
29
27
  private
30
28
 
31
- def handling_units(shipment, options)
32
- all_package_options = shipment.packages.map { |package| options.options_for_package(package) }
33
- all_package_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
34
- [options_group.first, options_group.length]
35
- end.map { |package_options, quantity| handling_unit_hash(package_options, quantity) }
36
- end
37
-
38
- def handling_unit_hash(package_options, quantity)
39
- {
40
- package_options.handling_unit_tag => {
41
- Quantity: quantity.to_s,
42
- Type: {
43
- Code: package_options.handling_unit_code,
44
- Description: package_options.handling_unit_description
45
- }
46
- }
47
- }
48
- end
49
-
50
29
  def request_options(customer_context)
51
30
  return {} unless customer_context
52
31
 
@@ -1,12 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups_freight/generate_location_hash'
4
- require 'friendly_shipping/services/ups_freight/generate_reference_hash'
5
- require 'friendly_shipping/services/ups_freight/generate_document_options_hash'
6
- require 'friendly_shipping/services/ups_freight/generate_email_options_hash'
7
- require 'friendly_shipping/services/ups_freight/generate_pickup_options_hash'
8
- require 'friendly_shipping/services/ups_freight/generate_delivery_options_hash'
9
-
10
3
  module FriendlyShipping
11
4
  module Services
12
5
  class UpsFreight
@@ -33,7 +26,7 @@ module FriendlyShipping
33
26
  DeliveryInstructions: options.delivery_instructions,
34
27
  PickupRequest: GeneratePickupRequestHash.call(pickup_request_options: options.pickup_request_options),
35
28
  }.compact.
36
- merge(handling_units(shipment, options).reduce(&:merge).to_h).
29
+ merge(GenerateHandlingUnitsHash.call(shipment: shipment, options: options)).
37
30
  merge(GenerateReferenceHash.call(reference_numbers: options.reference_numbers))
38
31
  }
39
32
  }
@@ -48,25 +41,6 @@ module FriendlyShipping
48
41
  [email_options, pickup_options, delivery_options].compact.presence
49
42
  end
50
43
 
51
- def handling_units(shipment, options)
52
- all_package_options = shipment.packages.map { |package| options.options_for_package(package) }
53
- all_package_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
54
- [options_group.first, options_group.length]
55
- end.map { |package_options, quantity| handling_unit_hash(package_options, quantity) }
56
- end
57
-
58
- def handling_unit_hash(package_options, quantity)
59
- {
60
- package_options.handling_unit_tag => {
61
- Quantity: quantity.to_s,
62
- Type: {
63
- Code: package_options.handling_unit_code,
64
- Description: package_options.handling_unit_description
65
- }
66
- }
67
- }
68
- end
69
-
70
44
  def payment_information(options)
71
45
  payer_address = GenerateLocationHash.call(location: options.billing_address).
72
46
  merge(ShipperNumber: options.shipper_number)
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsFreight
6
+ # Generates a handling units hash for JSON serialization.
7
+ class GenerateHandlingUnitsHash
8
+ class << self
9
+ # @param shipment [Physical::Shipment]
10
+ # @param options [ShipmentOptions]
11
+ # @return [Hash]
12
+ def call(shipment:, options:)
13
+ handling_units(shipment, options).reduce(&:merge)
14
+ end
15
+
16
+ private
17
+
18
+ # @param shipment [Physical::Shipment]
19
+ # @param options [ShipmentOptions]
20
+ # @return [Array<Hash>]
21
+ def handling_units(shipment, options)
22
+ if shipment.packages.any?
23
+ warn "[DEPRECATION] `packages` is deprecated. Please use `structures` instead."
24
+ all_package_options = shipment.packages.map { |package| options.options_for_package(package) }
25
+ all_package_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
26
+ [options_group.first, options_group.length]
27
+ end.map { |package_options, quantity| handling_unit_hash(package_options, quantity) }
28
+ else
29
+ all_structure_options = shipment.structures.map { |structure| options.options_for_structure(structure) }
30
+ all_structure_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
31
+ [options_group.first, options_group.length]
32
+ end.map { |structure_options, quantity| handling_unit_hash(structure_options, quantity) }
33
+ end
34
+ end
35
+
36
+ # @param options [PackageOptions, StructureOptions]
37
+ # @param quantity [Integer]
38
+ # @return [Hash]
39
+ def handling_unit_hash(options, quantity)
40
+ {
41
+ options.handling_unit_tag.to_sym => {
42
+ Quantity: quantity.to_s,
43
+ Type: {
44
+ Code: options.handling_unit_code,
45
+ Description: options.handling_unit_description
46
+ }
47
+ }
48
+ }
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -3,7 +3,9 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class UpsFreight
6
+ # Options for generating UPS Freight labels for a shipment.
6
7
  class LabelOptions < RatesOptions
8
+ # Maps friendly names to reference number codes.
7
9
  REFERENCE_NUMBER_CODES = {
8
10
  bill_of_lading_number: "57",
9
11
  purchase_order_number: "28",
@@ -18,16 +20,38 @@ module FriendlyShipping
18
20
  other: "OTHER"
19
21
  }.freeze
20
22
 
21
- attr_reader :document_options,
22
- :email_options,
23
- :pickup_options,
24
- :delivery_options,
25
- :pickup_instructions,
26
- :delivery_instructions,
27
- :handling_instructions,
28
- :reference_numbers
23
+ # @return [Array<LabelDocumentOptions>]
24
+ attr_reader :document_options
29
25
 
30
- # @param [Array] reference_numbers Reference numbers for the Bill of Lading
26
+ # @return [Array<LabelEmailOptions>]
27
+ attr_reader :email_options
28
+
29
+ # @return [LabelPickupOptions]
30
+ attr_reader :pickup_options
31
+
32
+ # @return [LabelDeliveryOptions]
33
+ attr_reader :delivery_options
34
+
35
+ # @return [String]
36
+ attr_reader :pickup_instructions
37
+
38
+ # @return [String]
39
+ attr_reader :delivery_instructions
40
+
41
+ # @return [String]
42
+ attr_reader :handling_instructions
43
+
44
+ # @return [Array<Hash>]
45
+ attr_reader :reference_numbers
46
+
47
+ # @param document_options [Array<LabelDocumentOptions>]
48
+ # @param email_options [Array<LabelEmailOptions>]
49
+ # @param pickup_options [LabelPickupOptions]
50
+ # @param delivery_options [LabelDeliveryOptions]
51
+ # @param pickup_instructions [String]
52
+ # @param delivery_instructions [String]
53
+ # @param handling_instructions [String]
54
+ # @param reference_numbers [Array<Hash>] reference numbers for the Bill of Lading
31
55
  def initialize(
32
56
  document_options: [],
33
57
  email_options: [],
@@ -47,11 +71,13 @@ module FriendlyShipping
47
71
  @delivery_instructions = delivery_instructions
48
72
  @handling_instructions = handling_instructions
49
73
  @reference_numbers = fill_codes(reference_numbers)
50
- super(**kwargs)
74
+ super(**kwargs.reverse_merge(package_options_class: LabelPackageOptions))
51
75
  end
52
76
 
53
77
  private
54
78
 
79
+ # @param reference_numbers [Array<Hash>] reference numbers for the Bill of Lading
80
+ # @return [Array<Hash>] reference numbers with codes filled
55
81
  def fill_codes(reference_numbers)
56
82
  reference_numbers.each do |reference_number|
57
83
  reference_number[:code] = REFERENCE_NUMBER_CODES.fetch(reference_number[:code])
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsFreight
6
+ class LabelStructureOptions < RatesStructureOptions
7
+ def initialize(**kwargs)
8
+ super(**kwargs.reverse_merge(package_options_class: LabelPackageOptions))
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups_freight/parse_shipment_document'
4
- require 'friendly_shipping/services/ups_freight/shipment_information'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  class UpsFreight
@@ -33,7 +30,7 @@ module FriendlyShipping
33
30
  images_data = Array.wrap(shipment_results.dig("Documents", "Image"))
34
31
 
35
32
  bol_id = shipment_results["BOLID"]
36
- shipment_number = shipment_results["ShipmentNumber"]
33
+ pro_number = shipment_results["ShipmentNumber"]
37
34
  pickup_request_number = shipment_results["PickupRequestConfirmationNumber"]
38
35
 
39
36
  documents = images_data.map { |image_data| ParseShipmentDocument.call(image_data: image_data) }
@@ -44,7 +41,7 @@ module FriendlyShipping
44
41
  ShipmentInformation.new(
45
42
  total: total_money,
46
43
  bol_id: bol_id,
47
- number: shipment_number,
44
+ pro_number: pro_number,
48
45
  pickup_request_number: pickup_request_number,
49
46
  shipping_method: shipping_method,
50
47
  warnings: warnings,
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/rate'
4
- require 'friendly_shipping/api_result'
5
- require 'friendly_shipping/services/ups_freight/shipping_methods'
6
-
7
3
  module FriendlyShipping
8
4
  module Services
9
5
  class UpsFreight
@@ -1,12 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups_freight/shipment_document'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class UpsFreight
8
6
  class ParseShipmentDocument
9
- REVERSE_DOCUMENT_TYPES = LabelDocumentOptions::DOCUMENT_TYPES.map(&:reverse_each).map(&:to_a).to_h
7
+ REVERSE_DOCUMENT_TYPES = LabelDocumentOptions::DOCUMENT_TYPES.map(&:reverse_each).to_h(&:to_a)
10
8
 
11
9
  def self.call(image_data:)
12
10
  format_code = image_data.dig("Type", "Code")
@@ -3,12 +3,9 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class UpsFreight
6
- # Options for Items in a UPS Freight shipment
7
- #
8
- # @attribute [Symbol] packaging How the item is packaged, one of the keys of `PACKAGING_TYPES`
9
- # @attribute [String] freight_class The freight class of this item, for example '55' or '92.5'
10
- # @attribute [String] nmfc_code The national motor freight corporation code for this item. Something like '13050 sub 4'
6
+ # Options for Items in a UPS Freight shipment.
11
7
  class RatesItemOptions < FriendlyShipping::ItemOptions
8
+ # Maps friendly names to packaging types.
12
9
  PACKAGING_TYPES = {
13
10
  bag: { code: "BAG", description: "Bag" },
14
11
  bale: { code: "BAL", description: "Bale" },
@@ -49,11 +46,23 @@ module FriendlyShipping
49
46
  wrapped: { code: "WRP", description: "Wrapped" }
50
47
  }.freeze
51
48
 
52
- attr_reader :packaging_code,
53
- :packaging_description,
54
- :freight_class,
55
- :nmfc_code
49
+ # @return [String]
50
+ attr_reader :packaging_code
56
51
 
52
+ # @return [String]
53
+ attr_reader :packaging_description
54
+
55
+ # @return [String]
56
+ attr_reader :freight_class
57
+
58
+ # @return [String]
59
+ attr_reader :nmfc_code
60
+
61
+ # @param packaging [Symbol] how the item is packaged (see {PACKAGING_TYPES})
62
+ # @param freight_class [String] the freight class of this item, for example '55' or '92.5'
63
+ # @param nmfc_code [String] the national motor freight corporation code for this item, for example '13050 sub 4'
64
+ # @param kwargs [Hash]
65
+ # @options kwargs [String] :item_id the ID for the item that belongs to these options
57
66
  def initialize(
58
67
  packaging: :carton,
59
68
  freight_class: nil,
@@ -1,39 +1,45 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups_freight/rates_package_options'
4
- require 'friendly_shipping/services/ups_freight/generate_commodity_information'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  class UpsFreight
9
6
  # Options for generating UPS Freight rates for a shipment
10
- #
11
- # @attribute [Physical::Location] billing_address The billing address
12
- # @attribute [String] shipper_number The shipper number associated with the shipper
13
- # @attribute [String] customer_context A reference to match this request with an order or shipment
14
- # @attribute [FriendlyShipping::ShippingMethod] shipping_method The shipping method to use
15
- # @attribute [Callable] commodity_information_generator A callable that takes a shipment
16
- # and an options object to create an Array of commodity fields as per the UPS docs.
17
- # @attribute [Symbol] billing One of the keys in the `BILLING_CODES` constant. How the shipment
18
- # would be billed.
19
- # @attribute [Date] pickup_date Date of the Pickup
20
- # @attribute [String] pickup_comments Additional pickup instructions
21
- # @attribute [RatesPackageOptions] package_options Options for each of the packages/pallets in this shipment
22
- class RatesOptions < FriendlyShipping::ShipmentOptions
7
+ class RatesOptions < ShipmentOptions
23
8
  BILLING_CODES = {
24
9
  prepaid: '10',
25
10
  third_party: '30',
26
11
  freight_collect: '40'
27
12
  }.freeze
28
13
 
29
- attr_reader :shipper_number,
30
- :billing_address,
31
- :billing_code,
32
- :customer_context,
33
- :shipping_method,
34
- :pickup_request_options,
35
- :commodity_information_generator
14
+ # @return [String]
15
+ attr_reader :shipper_number
16
+
17
+ # @return [Physical::Location]
18
+ attr_reader :billing_address
19
+
20
+ # @return [Symbol]
21
+ attr_reader :billing_code
22
+
23
+ # @return [String]
24
+ attr_reader :customer_context
25
+
26
+ # @return [FriendlyShipping::ShippingMethod]
27
+ attr_reader :shipping_method
28
+
29
+ # @return [PickupRequestOptions]
30
+ attr_reader :pickup_request_options
31
+
32
+ # @return [Callable]
33
+ attr_reader :commodity_information_generator
36
34
 
35
+ # @param shipper_number [String] the shipper number associated with the shipper
36
+ # @param billing_address [Physical::Location] the billing address
37
+ # @param shipping_method [FriendlyShipping::ShippingMethod] the shipping method to use
38
+ # @param billing [Symbol] how the shipment is billed (see {BILLING_CODES})
39
+ # @param customer_context [String] a reference to match this request with an order or shipment
40
+ # @param pickup_request_options [PickupRequestOptions] options for the pickup request
41
+ # @param commodity_information_generator [Callable] a callable that takes a shipment
42
+ # and an options object to create an Array of commodity fields as per the UPS docs
37
43
  def initialize(
38
44
  shipper_number:,
39
45
  billing_address:,
@@ -51,7 +57,7 @@ module FriendlyShipping
51
57
  @customer_context = customer_context
52
58
  @pickup_request_options = pickup_request_options
53
59
  @commodity_information_generator = commodity_information_generator
54
- super(**kwargs.merge(package_options_class: RatesPackageOptions))
60
+ super(**kwargs.reverse_merge(package_options_class: RatesPackageOptions))
55
61
  end
56
62
  end
57
63
  end
@@ -1,15 +1,52 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups_freight/rates_item_options'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class UpsFreight
8
- # Options for packages/pallets within a UPS Freight shipment
9
- #
10
- # @attribute [Symbol] handling _unit How this shipment is divided. Any of the keys in `HANDLING_UNIT_TYPES`
11
- # @attribute [RatesItemOptions] item_options Options for each of the items on the pallet/in the carboy/etc.
6
+ # Options for packages/pallets within a UPS Freight shipment.
12
7
  class RatesPackageOptions < FriendlyShipping::PackageOptions
8
+ # Maps friendly names to packaging types.
9
+ PACKAGING_TYPES = {
10
+ bag: { code: "BAG", description: "Bag" },
11
+ bale: { code: "BAL", description: "Bale" },
12
+ barrel: { code: "BAR", description: "Barrel" },
13
+ bundle: { code: "BDL", description: "Bundle" },
14
+ bin: { code: "BIN", description: "Bin" },
15
+ box: { code: "BOX", description: "Box" },
16
+ basket: { code: "BSK", description: "Basket" },
17
+ bunch: { code: "BUN", description: "Bunch" },
18
+ cabinet: { code: "CAB", description: "Cabinet" },
19
+ can: { code: "CAN", description: "Can" },
20
+ carrier: { code: "CAR", description: "Carrier" },
21
+ case: { code: "CAS", description: "Case" },
22
+ carboy: { code: "CBY", description: "Carboy" },
23
+ container: { code: "CON", description: "Container" },
24
+ crate: { code: "CRT", description: "Crate" },
25
+ cask: { code: "CSK", description: "Cask" },
26
+ carton: { code: "CTN", description: "Carton" },
27
+ cylinder: { code: "CYL", description: "Cylinder" },
28
+ drum: { code: "DRM", description: "Drum" },
29
+ loose: { code: "LOO", description: "Loose" },
30
+ other: { code: "OTH", description: "Other" },
31
+ pail: { code: "PAL", description: "Pail" },
32
+ pieces: { code: "PCS", description: "Pieces" },
33
+ package: { code: "PKG", description: "Package" },
34
+ pipe_line: { code: "PLN", description: "Pipe Line" },
35
+ pallet: { code: "PLT", description: "Pallet" },
36
+ rack: { code: "RCK", description: "Rack" },
37
+ reel: { code: "REL", description: "Reel" },
38
+ roll: { code: "ROL", description: "Roll" },
39
+ skid: { code: "SKD", description: "Skid" },
40
+ spool: { code: "SPL", description: "Spool" },
41
+ tube: { code: "TBE", description: "Tube" },
42
+ tank: { code: "TNK", description: "Tank" },
43
+ totes: { code: "TOT", description: "Totes" },
44
+ unit: { code: "UNT", description: "Unit" },
45
+ van_pack: { code: "VPK", description: "Van Pack" },
46
+ wrapped: { code: "WRP", description: "Wrapped" }
47
+ }.freeze
48
+
49
+ # Maps friendly names to handling unit types.
13
50
  HANDLING_UNIT_TYPES = {
14
51
  pallet: { code: "PLT", description: "Pallet", handling_unit_tag: 'One' },
15
52
  skid: { code: "SKD", description: "Skid", handling_unit_tag: 'One' },
@@ -19,18 +56,50 @@ module FriendlyShipping
19
56
  loose: { code: "LOO", description: "Loose", handling_unit_tag: 'Two' }
20
57
  }.freeze
21
58
 
22
- attr_reader :handling_unit_description,
23
- :handling_unit_tag,
24
- :handling_unit_code
59
+ # @return [String]
60
+ attr_reader :packaging_code
61
+
62
+ # @return [String]
63
+ attr_reader :packaging_description
64
+
65
+ # @return [String]
66
+ attr_reader :freight_class
67
+
68
+ # @return [String]
69
+ attr_reader :nmfc_code
70
+
71
+ # @return [String]
72
+ attr_reader :handling_unit_description
73
+
74
+ # @return [String]
75
+ attr_reader :handling_unit_tag
76
+
77
+ # @return [String]
78
+ attr_reader :handling_unit_code
25
79
 
80
+ # @param packaging [Symbol] how this shipment is packaged (see {PACKAGING_TYPES})
81
+ # @param freight_class [String] the freight class
82
+ # @param nmfc_code [String] the NMFC code
83
+ # @param handling_unit [Symbol] how this shipment is divided (see {HANDLING_UNIT_TYPES})
84
+ # @param kwargs [Hash]
85
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
86
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
87
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
26
88
  def initialize(
89
+ packaging: :carton,
90
+ freight_class: nil,
91
+ nmfc_code: nil,
27
92
  handling_unit: :pallet,
28
93
  **kwargs
29
94
  )
95
+ @packaging_code = PACKAGING_TYPES.fetch(packaging).fetch(:code)
96
+ @packaging_description = PACKAGING_TYPES.fetch(packaging).fetch(:description)
97
+ @freight_class = freight_class
98
+ @nmfc_code = nmfc_code
30
99
  @handling_unit_code = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:code)
31
100
  @handling_unit_description = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:description)
32
101
  @handling_unit_tag = "HandlingUnit#{HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:handling_unit_tag)}"
33
- super(**kwargs.merge(item_options_class: RatesItemOptions))
102
+ super(**kwargs.reverse_merge(item_options_class: RatesItemOptions))
34
103
  end
35
104
  end
36
105
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsFreight
6
+ # Options for structures/pallets within a UPS Freight shipment.
7
+ class RatesStructureOptions < FriendlyShipping::StructureOptions
8
+ # Maps friendly names to handling unit types.
9
+ HANDLING_UNIT_TYPES = {
10
+ pallet: { code: "PLT", description: "Pallet", handling_unit_tag: 'One' },
11
+ skid: { code: "SKD", description: "Skid", handling_unit_tag: 'One' },
12
+ carboy: { code: "CBY", description: "Carboy", handling_unit_tag: 'One' },
13
+ totes: { code: "TOT", description: "Totes", handling_unit_tag: 'One' },
14
+ other: { code: "OTH", description: "Other", handling_unit_tag: 'Two' },
15
+ loose: { code: "LOO", description: "Loose", handling_unit_tag: 'Two' }
16
+ }.freeze
17
+
18
+ # @return [String]
19
+ attr_reader :handling_unit_description
20
+
21
+ # @return [String]
22
+ attr_reader :handling_unit_tag
23
+
24
+ # @return [String]
25
+ attr_reader :handling_unit_code
26
+
27
+ # @param handling_unit [Symbol] how this shipment is divided (see {HANDLING_UNIT_TYPES})
28
+ # @param kwargs [Hash]
29
+ # @options kwargs [Object] :structure_id unique identifier for this set of options
30
+ # @options kwargs [Array<PackageOptions>] :package_options the options for packages in this structure
31
+ # @options kwargs [Class] :package_options_class the class to use for package options when none are provided
32
+ def initialize(
33
+ handling_unit: :pallet,
34
+ **kwargs
35
+ )
36
+ @handling_unit_code = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:code)
37
+ @handling_unit_description = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:description)
38
+ @handling_unit_tag = "HandlingUnit#{HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:handling_unit_tag)}"
39
+ super(**kwargs.reverse_merge(package_options_class: RatesPackageOptions))
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end