friendly_shipping 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +7 -0
  4. data/.env.test +4 -0
  5. data/.env.test.local.template +6 -0
  6. data/.gitignore +1 -0
  7. data/.rubocop-relaxed.yml +7 -23
  8. data/.rubocop.yml +11 -2
  9. data/.rubocop_todo.yml +21 -0
  10. data/.yardopts +6 -0
  11. data/CHANGELOG.md +82 -0
  12. data/Gemfile +17 -0
  13. data/friendly_shipping.gemspec +4 -13
  14. data/lib/friendly_shipping/api_error.rb +6 -4
  15. data/lib/friendly_shipping/api_error_handler.rb +10 -6
  16. data/lib/friendly_shipping/api_failure.rb +4 -0
  17. data/lib/friendly_shipping/api_result.rb +16 -4
  18. data/lib/friendly_shipping/carrier.rb +28 -8
  19. data/lib/friendly_shipping/http_client.rb +25 -2
  20. data/lib/friendly_shipping/inflections.rb +10 -0
  21. data/lib/friendly_shipping/item_options.rb +3 -0
  22. data/lib/friendly_shipping/label.rb +41 -20
  23. data/lib/friendly_shipping/package_options.rb +21 -2
  24. data/lib/friendly_shipping/rate.rb +50 -15
  25. data/lib/friendly_shipping/request.rb +23 -7
  26. data/lib/friendly_shipping/response.rb +21 -6
  27. data/lib/friendly_shipping/services/rl/api_error.rb +33 -0
  28. data/lib/friendly_shipping/services/rl/bol_options.rb +107 -0
  29. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  30. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  31. data/lib/friendly_shipping/services/rl/item_options.rb +38 -0
  32. data/lib/friendly_shipping/services/rl/package_options.rb +40 -0
  33. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +46 -0
  34. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +50 -0
  35. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +47 -0
  37. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +66 -0
  38. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +76 -0
  39. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +86 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  41. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  42. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  43. data/lib/friendly_shipping/services/rl/serialize_location.rb +46 -0
  44. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  45. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  46. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  47. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  48. data/lib/friendly_shipping/services/rl/shipment_options.rb +50 -0
  49. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  50. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  51. data/lib/friendly_shipping/services/rl.rb +204 -0
  52. data/lib/friendly_shipping/services/ship_engine/api_error.rb +33 -0
  53. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  54. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  55. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -5
  56. data/lib/friendly_shipping/services/ship_engine/label_options.rb +31 -14
  57. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -11
  58. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +77 -0
  59. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -0
  60. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -0
  61. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +26 -6
  62. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +101 -0
  63. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +4 -0
  64. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -4
  65. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +28 -0
  66. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +61 -0
  67. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +20 -0
  68. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  70. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  71. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  72. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  73. data/lib/friendly_shipping/services/ship_engine.rb +94 -21
  74. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  75. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +50 -0
  76. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +50 -0
  77. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +53 -0
  78. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +82 -0
  79. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +52 -0
  80. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +46 -0
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +143 -0
  82. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  83. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  85. data/lib/friendly_shipping/services/ship_engine_ltl.rb +153 -0
  86. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +43 -0
  87. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +42 -0
  88. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  89. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  90. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +165 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +113 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +65 -0
  97. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  98. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  99. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +121 -0
  100. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +94 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +58 -0
  103. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +31 -0
  104. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  105. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +12 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +162 -0
  107. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +12 -0
  108. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +49 -0
  111. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  112. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  113. data/lib/friendly_shipping/services/tforce_freight.rb +202 -0
  114. data/lib/friendly_shipping/services/ups/label_options.rb +14 -2
  115. data/lib/friendly_shipping/services/ups/label_package_options.rb +8 -4
  116. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  117. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +12 -0
  118. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  119. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -1
  120. data/lib/friendly_shipping/services/ups/serialize_package_node.rb +11 -1
  121. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -1
  122. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  123. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +8 -4
  124. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  125. data/lib/friendly_shipping/services/ups.rb +1 -1
  126. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -5
  127. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  128. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +3 -20
  129. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +2 -20
  130. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  131. data/lib/friendly_shipping/services/ups_freight/generate_location_hash.rb +10 -6
  132. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  133. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  134. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -2
  135. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -1
  136. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  137. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +31 -21
  138. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -8
  139. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +46 -0
  140. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  141. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +49 -0
  142. data/lib/friendly_shipping/services/ups_freight.rb +3 -1
  143. data/lib/friendly_shipping/services/ups_json/access_token.rb +29 -0
  144. data/lib/friendly_shipping/services/ups_json/api_error.rb +29 -0
  145. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  146. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  147. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +211 -0
  148. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  149. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +86 -0
  150. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  151. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  152. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  153. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +77 -0
  154. data/lib/friendly_shipping/services/ups_json/label_options.rb +177 -0
  155. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +51 -0
  156. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  157. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +44 -0
  158. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  159. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  160. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  161. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +105 -0
  162. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  163. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  164. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +22 -0
  165. data/lib/friendly_shipping/services/ups_json/rates_options.rb +113 -0
  166. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  167. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  168. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  169. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  170. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  171. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  172. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +8 -7
  173. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +12 -8
  174. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -1
  175. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -1
  176. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -2
  177. data/lib/friendly_shipping/services/usps/shipping_methods.rb +8 -5
  178. data/lib/friendly_shipping/services/usps.rb +1 -1
  179. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +5 -4
  180. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -1
  181. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +3 -3
  182. data/lib/friendly_shipping/services/usps_international.rb +1 -1
  183. data/lib/friendly_shipping/services/usps_ship/access_token.rb +37 -0
  184. data/lib/friendly_shipping/services/usps_ship/api_error.rb +29 -0
  185. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +53 -0
  186. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  187. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +80 -0
  188. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  189. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +124 -0
  190. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +55 -0
  191. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  192. data/lib/friendly_shipping/services/usps_ship/timing_options.rb +9 -0
  193. data/lib/friendly_shipping/services/usps_ship.rb +199 -0
  194. data/lib/friendly_shipping/shipment_options.rb +13 -1
  195. data/lib/friendly_shipping/shipping_method.rb +38 -11
  196. data/lib/friendly_shipping/structure_options.rb +38 -0
  197. data/lib/friendly_shipping/timing.rb +42 -7
  198. data/lib/friendly_shipping/version.rb +1 -1
  199. data/lib/friendly_shipping.rb +7 -0
  200. metadata +149 -172
  201. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  202. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
@@ -7,17 +7,17 @@ module FriendlyShipping
7
7
  class << self
8
8
  def call(location:)
9
9
  {
10
- Name: location.company_name.presence || location.name,
10
+ Name: truncate(location.company_name.presence || location.name),
11
11
  Address: {
12
12
  AddressLine: address_line(location),
13
- City: location.city,
13
+ City: truncate(location.city, length: 29),
14
14
  StateProvinceCode: location.region&.code,
15
15
  PostalCode: location.zip,
16
16
  CountryCode: location.country&.code
17
17
  },
18
- AttentionName: location.name,
18
+ AttentionName: truncate(location.name),
19
19
  Phone: {
20
- Number: location.phone
20
+ Number: truncate(location.phone, length: 14)
21
21
  }.compact.presence
22
22
  }.compact
23
23
  end
@@ -29,8 +29,12 @@ module FriendlyShipping
29
29
  location.address1,
30
30
  location.address2,
31
31
  location.address3
32
- ].compact.reject(&:empty?)
33
- address_lines.size > 1 ? address_lines : address_lines.first
32
+ ].compact.reject(&:empty?).map { |e| truncate(e) }
33
+ address_lines.size > 1 ? address_lines : truncate(address_lines.first)
34
+ end
35
+
36
+ def truncate(value, length: 35)
37
+ value && value[0..(length - 1)]
34
38
  end
35
39
  end
36
40
  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
@@ -33,7 +33,7 @@ module FriendlyShipping
33
33
  images_data = Array.wrap(shipment_results.dig("Documents", "Image"))
34
34
 
35
35
  bol_id = shipment_results["BOLID"]
36
- shipment_number = shipment_results["ShipmentNumber"]
36
+ pro_number = shipment_results["ShipmentNumber"]
37
37
  pickup_request_number = shipment_results["PickupRequestConfirmationNumber"]
38
38
 
39
39
  documents = images_data.map { |image_data| ParseShipmentDocument.call(image_data: image_data) }
@@ -44,7 +44,7 @@ module FriendlyShipping
44
44
  ShipmentInformation.new(
45
45
  total: total_money,
46
46
  bol_id: bol_id,
47
- number: shipment_number,
47
+ pro_number: pro_number,
48
48
  pickup_request_number: pickup_request_number,
49
49
  shipping_method: shipping_method,
50
50
  warnings: warnings,
@@ -6,7 +6,7 @@ module FriendlyShipping
6
6
  module Services
7
7
  class UpsFreight
8
8
  class ParseShipmentDocument
9
- REVERSE_DOCUMENT_TYPES = LabelDocumentOptions::DOCUMENT_TYPES.map(&:reverse_each).map(&:to_a).to_h
9
+ REVERSE_DOCUMENT_TYPES = LabelDocumentOptions::DOCUMENT_TYPES.map(&:reverse_each).to_h(&:to_a)
10
10
 
11
11
  def self.call(image_data:)
12
12
  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,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'friendly_shipping/services/ups_freight/shipment_options'
3
4
  require 'friendly_shipping/services/ups_freight/rates_package_options'
4
5
  require 'friendly_shipping/services/ups_freight/generate_commodity_information'
5
6
 
@@ -7,33 +8,42 @@ module FriendlyShipping
7
8
  module Services
8
9
  class UpsFreight
9
10
  # 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
11
+ class RatesOptions < ShipmentOptions
23
12
  BILLING_CODES = {
24
13
  prepaid: '10',
25
14
  third_party: '30',
26
15
  freight_collect: '40'
27
16
  }.freeze
28
17
 
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
18
+ # @return [String]
19
+ attr_reader :shipper_number
36
20
 
21
+ # @return [Physical::Location]
22
+ attr_reader :billing_address
23
+
24
+ # @return [Symbol]
25
+ attr_reader :billing_code
26
+
27
+ # @return [String]
28
+ attr_reader :customer_context
29
+
30
+ # @return [FriendlyShipping::ShippingMethod]
31
+ attr_reader :shipping_method
32
+
33
+ # @return [PickupRequestOptions]
34
+ attr_reader :pickup_request_options
35
+
36
+ # @return [Callable]
37
+ attr_reader :commodity_information_generator
38
+
39
+ # @param shipper_number [String] the shipper number associated with the shipper
40
+ # @param billing_address [Physical::Location] the billing address
41
+ # @param shipping_method [FriendlyShipping::ShippingMethod] the shipping method to use
42
+ # @param billing [Symbol] how the shipment is billed (see {BILLING_CODES})
43
+ # @param customer_context [String] a reference to match this request with an order or shipment
44
+ # @param pickup_request_options [PickupRequestOptions] options for the pickup request
45
+ # @param commodity_information_generator [Callable] a callable that takes a shipment
46
+ # and an options object to create an Array of commodity fields as per the UPS docs
37
47
  def initialize(
38
48
  shipper_number:,
39
49
  billing_address:,
@@ -51,7 +61,7 @@ module FriendlyShipping
51
61
  @customer_context = customer_context
52
62
  @pickup_request_options = pickup_request_options
53
63
  @commodity_information_generator = commodity_information_generator
54
- super(**kwargs.merge(package_options_class: RatesPackageOptions))
64
+ super(**kwargs.reverse_merge(package_options_class: RatesPackageOptions))
55
65
  end
56
66
  end
57
67
  end
@@ -5,11 +5,50 @@ require 'friendly_shipping/services/ups_freight/rates_item_options'
5
5
  module FriendlyShipping
6
6
  module Services
7
7
  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.
8
+ # Options for packages/pallets within a UPS Freight shipment.
12
9
  class RatesPackageOptions < FriendlyShipping::PackageOptions
10
+ # Maps friendly names to packaging types.
11
+ PACKAGING_TYPES = {
12
+ bag: { code: "BAG", description: "Bag" },
13
+ bale: { code: "BAL", description: "Bale" },
14
+ barrel: { code: "BAR", description: "Barrel" },
15
+ bundle: { code: "BDL", description: "Bundle" },
16
+ bin: { code: "BIN", description: "Bin" },
17
+ box: { code: "BOX", description: "Box" },
18
+ basket: { code: "BSK", description: "Basket" },
19
+ bunch: { code: "BUN", description: "Bunch" },
20
+ cabinet: { code: "CAB", description: "Cabinet" },
21
+ can: { code: "CAN", description: "Can" },
22
+ carrier: { code: "CAR", description: "Carrier" },
23
+ case: { code: "CAS", description: "Case" },
24
+ carboy: { code: "CBY", description: "Carboy" },
25
+ container: { code: "CON", description: "Container" },
26
+ crate: { code: "CRT", description: "Crate" },
27
+ cask: { code: "CSK", description: "Cask" },
28
+ carton: { code: "CTN", description: "Carton" },
29
+ cylinder: { code: "CYL", description: "Cylinder" },
30
+ drum: { code: "DRM", description: "Drum" },
31
+ loose: { code: "LOO", description: "Loose" },
32
+ other: { code: "OTH", description: "Other" },
33
+ pail: { code: "PAL", description: "Pail" },
34
+ pieces: { code: "PCS", description: "Pieces" },
35
+ package: { code: "PKG", description: "Package" },
36
+ pipe_line: { code: "PLN", description: "Pipe Line" },
37
+ pallet: { code: "PLT", description: "Pallet" },
38
+ rack: { code: "RCK", description: "Rack" },
39
+ reel: { code: "REL", description: "Reel" },
40
+ roll: { code: "ROL", description: "Roll" },
41
+ skid: { code: "SKD", description: "Skid" },
42
+ spool: { code: "SPL", description: "Spool" },
43
+ tube: { code: "TBE", description: "Tube" },
44
+ tank: { code: "TNK", description: "Tank" },
45
+ totes: { code: "TOT", description: "Totes" },
46
+ unit: { code: "UNT", description: "Unit" },
47
+ van_pack: { code: "VPK", description: "Van Pack" },
48
+ wrapped: { code: "WRP", description: "Wrapped" }
49
+ }.freeze
50
+
51
+ # Maps friendly names to handling unit types.
13
52
  HANDLING_UNIT_TYPES = {
14
53
  pallet: { code: "PLT", description: "Pallet", handling_unit_tag: 'One' },
15
54
  skid: { code: "SKD", description: "Skid", handling_unit_tag: 'One' },
@@ -19,18 +58,50 @@ module FriendlyShipping
19
58
  loose: { code: "LOO", description: "Loose", handling_unit_tag: 'Two' }
20
59
  }.freeze
21
60
 
22
- attr_reader :handling_unit_description,
23
- :handling_unit_tag,
24
- :handling_unit_code
61
+ # @return [String]
62
+ attr_reader :packaging_code
63
+
64
+ # @return [String]
65
+ attr_reader :packaging_description
66
+
67
+ # @return [String]
68
+ attr_reader :freight_class
69
+
70
+ # @return [String]
71
+ attr_reader :nmfc_code
72
+
73
+ # @return [String]
74
+ attr_reader :handling_unit_description
75
+
76
+ # @return [String]
77
+ attr_reader :handling_unit_tag
78
+
79
+ # @return [String]
80
+ attr_reader :handling_unit_code
25
81
 
82
+ # @param packaging [Symbol] how this shipment is packaged (see {PACKAGING_TYPES})
83
+ # @param freight_class [String] the freight class
84
+ # @param nmfc_code [String] the NMFC code
85
+ # @param handling_unit [Symbol] how this shipment is divided (see {HANDLING_UNIT_TYPES})
86
+ # @param kwargs [Hash]
87
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
88
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
89
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
26
90
  def initialize(
91
+ packaging: :carton,
92
+ freight_class: nil,
93
+ nmfc_code: nil,
27
94
  handling_unit: :pallet,
28
95
  **kwargs
29
96
  )
97
+ @packaging_code = PACKAGING_TYPES.fetch(packaging).fetch(:code)
98
+ @packaging_description = PACKAGING_TYPES.fetch(packaging).fetch(:description)
99
+ @freight_class = freight_class
100
+ @nmfc_code = nmfc_code
30
101
  @handling_unit_code = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:code)
31
102
  @handling_unit_description = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:description)
32
103
  @handling_unit_tag = "HandlingUnit#{HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:handling_unit_tag)}"
33
- super(**kwargs.merge(item_options_class: RatesItemOptions))
104
+ super(**kwargs.reverse_merge(item_options_class: RatesItemOptions))
34
105
  end
35
106
  end
36
107
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/ups_freight/rates_package_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class UpsFreight
8
+ # Options for structures/pallets within a UPS Freight shipment.
9
+ class RatesStructureOptions < FriendlyShipping::StructureOptions
10
+ # Maps friendly names to handling unit types.
11
+ HANDLING_UNIT_TYPES = {
12
+ pallet: { code: "PLT", description: "Pallet", handling_unit_tag: 'One' },
13
+ skid: { code: "SKD", description: "Skid", handling_unit_tag: 'One' },
14
+ carboy: { code: "CBY", description: "Carboy", handling_unit_tag: 'One' },
15
+ totes: { code: "TOT", description: "Totes", handling_unit_tag: 'One' },
16
+ other: { code: "OTH", description: "Other", handling_unit_tag: 'Two' },
17
+ loose: { code: "LOO", description: "Loose", handling_unit_tag: 'Two' }
18
+ }.freeze
19
+
20
+ # @return [String]
21
+ attr_reader :handling_unit_description
22
+
23
+ # @return [String]
24
+ attr_reader :handling_unit_tag
25
+
26
+ # @return [String]
27
+ attr_reader :handling_unit_code
28
+
29
+ # @param handling_unit [Symbol] how this shipment is divided (see {HANDLING_UNIT_TYPES})
30
+ # @param kwargs [Hash]
31
+ # @options kwargs [Object] :structure_id unique identifier for this set of options
32
+ # @options kwargs [Array<PackageOptions>] :package_options the options for packages in this structure
33
+ # @options kwargs [Class] :package_options_class the class to use for package options when none are provided
34
+ def initialize(
35
+ handling_unit: :pallet,
36
+ **kwargs
37
+ )
38
+ @handling_unit_code = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:code)
39
+ @handling_unit_description = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:description)
40
+ @handling_unit_tag = "HandlingUnit#{HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:handling_unit_tag)}"
41
+ super(**kwargs.reverse_merge(package_options_class: RatesPackageOptions))
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -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,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/shipment_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class UpsFreight
8
+ class ShipmentOptions < FriendlyShipping::ShipmentOptions
9
+ # @param structure_options [Array<StructureOptions>]
10
+ # @param structure_options_class [Class]
11
+ # @param kwargs [Hash]
12
+ # @option kwargs [Array<PackageOptions>] :package_options
13
+ # @option kwargs [Class] :package_options_class
14
+ def initialize(
15
+ structure_options: Set.new,
16
+ structure_options_class: RatesStructureOptions,
17
+ **kwargs
18
+ )
19
+ @structure_options = structure_options
20
+ @structure_options_class = structure_options_class
21
+ super(**kwargs)
22
+ end
23
+
24
+ # @param structure [#id]
25
+ # @return [StructureOptions]
26
+ def options_for_structure(structure)
27
+ structure_options.detect do |structure_option|
28
+ structure_option.structure_id == structure.id
29
+ end || structure_options_class.new(structure_id: nil)
30
+ end
31
+
32
+ private
33
+
34
+ # @return [Array<StructureOptions>]
35
+ attr_reader :structure_options
36
+
37
+ # @return [Class]
38
+ attr_reader :structure_options_class
39
+
40
+ # @return [Array<PackageOptions>]
41
+ # @deprecated Use {#structure_options} instead.
42
+ def package_options
43
+ warn "[DEPRECATION] `package_options` is deprecated. Please use `structure_options` instead."
44
+ @package_options
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -5,8 +5,10 @@ require 'friendly_shipping/http_client'
5
5
  require 'friendly_shipping/services/ups_freight/shipping_methods'
6
6
  require 'friendly_shipping/services/ups_freight/rates_options'
7
7
  require 'friendly_shipping/services/ups_freight/label_options'
8
+ require 'friendly_shipping/services/ups_freight/rates_structure_options'
8
9
  require 'friendly_shipping/services/ups_freight/rates_package_options'
9
10
  require 'friendly_shipping/services/ups_freight/rates_item_options'
11
+ require 'friendly_shipping/services/ups_freight/label_structure_options'
10
12
  require 'friendly_shipping/services/ups_freight/label_package_options'
11
13
  require 'friendly_shipping/services/ups_freight/label_item_options'
12
14
  require 'friendly_shipping/services/ups_freight/label_document_options'
@@ -23,7 +25,7 @@ require 'friendly_shipping/services/ups_freight/api_error'
23
25
  module FriendlyShipping
24
26
  module Services
25
27
  class UpsFreight
26
- include Dry::Monads[:result]
28
+ include Dry::Monads::Result::Mixin
27
29
 
28
30
  attr_reader :test, :key, :login, :password, :client
29
31
 
@@ -0,0 +1,29 @@
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
9
+ # @return [Integer] the token's expiration
10
+ attr_reader :expires_in
11
+
12
+ # @return [Integer] the epoch time in ms when the token was issued
13
+ attr_reader :issued_at
14
+
15
+ # @return [String] the raw JWT token
16
+ attr_reader :raw_token
17
+
18
+ # @param expires_in [Integer] the token's expiration
19
+ # @param issued_at [Integer] the time the token was issued at
20
+ # @param raw_token [String] the raw JWT token
21
+ def initialize(expires_in:, issued_at:, raw_token:)
22
+ @expires_in = expires_in
23
+ @issued_at = issued_at
24
+ @raw_token = raw_token
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/api_error'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class UpsJson
8
+ class ApiError < FriendlyShipping::ApiError
9
+ # @param [RestClient::Exception] cause
10
+ def initialize(cause)
11
+ super(cause, parse_message(cause))
12
+ end
13
+
14
+ private
15
+
16
+ # @param [RestClient::Exception] error
17
+ # @return [String]
18
+ def parse_message(error)
19
+ return error.message unless error.response
20
+
21
+ parsed_json = JSON.parse(error.response.body)
22
+ parsed_json.dig("response", "errors")&.map { |response_error| response_error["message"] }&.join(", ")
23
+ rescue JSON::ParserError, KeyError => _e
24
+ nil
25
+ end
26
+ end
27
+ end
28
+ end
29
+ 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