friendly_shipping 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +3 -0
  4. data/.env.test.local.template +6 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop-relaxed.yml +7 -23
  7. data/.rubocop.yml +11 -2
  8. data/.rubocop_todo.yml +21 -0
  9. data/.yardopts +6 -0
  10. data/CHANGELOG.md +71 -0
  11. data/Gemfile +17 -0
  12. data/friendly_shipping.gemspec +4 -13
  13. data/lib/friendly_shipping/api_error.rb +6 -4
  14. data/lib/friendly_shipping/api_error_handler.rb +10 -6
  15. data/lib/friendly_shipping/api_failure.rb +4 -0
  16. data/lib/friendly_shipping/api_result.rb +16 -4
  17. data/lib/friendly_shipping/carrier.rb +28 -8
  18. data/lib/friendly_shipping/http_client.rb +25 -2
  19. data/lib/friendly_shipping/inflections.rb +10 -0
  20. data/lib/friendly_shipping/item_options.rb +3 -0
  21. data/lib/friendly_shipping/label.rb +41 -20
  22. data/lib/friendly_shipping/package_options.rb +21 -2
  23. data/lib/friendly_shipping/rate.rb +50 -15
  24. data/lib/friendly_shipping/request.rb +23 -7
  25. data/lib/friendly_shipping/response.rb +21 -6
  26. data/lib/friendly_shipping/services/rl/api_error.rb +33 -0
  27. data/lib/friendly_shipping/services/rl/bol_options.rb +107 -0
  28. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  29. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  30. data/lib/friendly_shipping/services/rl/item_options.rb +38 -0
  31. data/lib/friendly_shipping/services/rl/package_options.rb +40 -0
  32. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +46 -0
  33. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +50 -0
  34. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +47 -0
  35. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +66 -0
  37. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +76 -0
  38. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +86 -0
  39. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  41. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  42. data/lib/friendly_shipping/services/rl/serialize_location.rb +46 -0
  43. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  44. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  45. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  46. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  47. data/lib/friendly_shipping/services/rl/shipment_options.rb +50 -0
  48. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  49. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  50. data/lib/friendly_shipping/services/rl.rb +204 -0
  51. data/lib/friendly_shipping/services/ship_engine/api_error.rb +33 -0
  52. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  53. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  54. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -5
  55. data/lib/friendly_shipping/services/ship_engine/label_options.rb +31 -14
  56. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -11
  57. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +77 -0
  58. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -0
  59. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -0
  60. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +26 -6
  61. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +101 -0
  62. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +4 -0
  63. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -4
  64. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +28 -0
  65. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +61 -0
  66. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +20 -0
  67. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  68. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  70. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  71. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  72. data/lib/friendly_shipping/services/ship_engine.rb +94 -21
  73. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  74. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +24 -5
  75. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +39 -4
  76. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +4 -0
  77. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +10 -0
  78. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +29 -11
  79. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +7 -2
  80. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +50 -16
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  82. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  83. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl.rb +50 -30
  85. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +43 -0
  86. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +42 -0
  87. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  88. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  89. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  90. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +165 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +113 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +65 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  97. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  98. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +121 -0
  99. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +94 -0
  100. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +58 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +31 -0
  103. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  104. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +12 -0
  105. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +162 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +12 -0
  107. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  108. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +49 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  111. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  112. data/lib/friendly_shipping/services/tforce_freight.rb +202 -0
  113. data/lib/friendly_shipping/services/ups/label_options.rb +14 -2
  114. data/lib/friendly_shipping/services/ups/label_package_options.rb +1 -1
  115. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  116. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +12 -0
  117. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  118. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -1
  119. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -1
  120. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  121. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +2 -1
  122. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  123. data/lib/friendly_shipping/services/ups.rb +1 -1
  124. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -5
  125. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  126. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +3 -20
  127. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +2 -20
  128. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  129. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  130. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  131. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -2
  132. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -1
  133. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  134. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +31 -21
  135. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -8
  136. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +46 -0
  137. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  138. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +49 -0
  139. data/lib/friendly_shipping/services/ups_freight.rb +3 -1
  140. data/lib/friendly_shipping/services/ups_json/access_token.rb +29 -0
  141. data/lib/friendly_shipping/services/ups_json/api_error.rb +29 -0
  142. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  143. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  144. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +211 -0
  145. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  146. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +86 -0
  147. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  148. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  149. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  150. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +77 -0
  151. data/lib/friendly_shipping/services/ups_json/label_options.rb +177 -0
  152. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +51 -0
  153. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  154. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +44 -0
  155. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  156. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  157. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  158. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +105 -0
  159. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  160. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  161. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +22 -0
  162. data/lib/friendly_shipping/services/ups_json/rates_options.rb +113 -0
  163. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  164. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  165. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  166. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  167. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  168. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  169. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +6 -6
  170. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +6 -6
  171. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -1
  172. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -1
  173. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -2
  174. data/lib/friendly_shipping/services/usps/shipping_methods.rb +4 -3
  175. data/lib/friendly_shipping/services/usps.rb +1 -1
  176. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +3 -3
  177. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -1
  178. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +1 -1
  179. data/lib/friendly_shipping/services/usps_international.rb +1 -1
  180. data/lib/friendly_shipping/services/usps_ship/access_token.rb +37 -0
  181. data/lib/friendly_shipping/services/usps_ship/api_error.rb +29 -0
  182. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +53 -0
  183. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  184. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +80 -0
  185. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  186. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +124 -0
  187. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +55 -0
  188. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  189. data/lib/friendly_shipping/services/{ship_engine_ltl/bad_request.rb → usps_ship/timing_options.rb} +2 -2
  190. data/lib/friendly_shipping/services/usps_ship.rb +199 -0
  191. data/lib/friendly_shipping/shipment_options.rb +13 -1
  192. data/lib/friendly_shipping/shipping_method.rb +38 -11
  193. data/lib/friendly_shipping/structure_options.rb +38 -0
  194. data/lib/friendly_shipping/timing.rb +42 -7
  195. data/lib/friendly_shipping/version.rb +1 -1
  196. data/lib/friendly_shipping.rb +6 -0
  197. metadata +140 -174
  198. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  199. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
  200. data/lib/friendly_shipping/services/ship_engine_ltl/bad_request_handler.rb +0 -33
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Options for items in a shipment.
7
+ class ItemOptions < FriendlyShipping::ItemOptions
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
+ # @return [Symbol] the code for this item's packaging
50
+ attr_reader :packaging_code
51
+
52
+ # @return [String] the description for this item's packaging
53
+ attr_reader :packaging_description
54
+
55
+ # @return [String] the freight class
56
+ attr_reader :freight_class
57
+
58
+ # @return [String] the NMFC primary code
59
+ attr_reader :nmfc_primary_code
60
+
61
+ # @return [String] the NMFC sub code
62
+ attr_reader :nmfc_sub_code
63
+
64
+ # @return [Boolean] whether or not the item is hazardous
65
+ attr_reader :hazardous
66
+
67
+ # @param packaging [Symbol] this item's packaging
68
+ # @param freight_class [String] the freight class
69
+ # @param nmfc_primary_code [String] the NMFC primary code
70
+ # @param nmfc_sub_code [String] the NMFC sub code
71
+ # @param hazardous [Boolean] whether or not the item is hazardous
72
+ # @param kwargs [Hash]
73
+ # @option kwargs [String] :item_id the ID for the item that belongs to these options
74
+ def initialize(
75
+ packaging: :carton,
76
+ freight_class: nil,
77
+ nmfc_primary_code: nil,
78
+ nmfc_sub_code: nil,
79
+ hazardous: false,
80
+ **kwargs
81
+ )
82
+ @packaging_code = PACKAGING_TYPES.fetch(packaging).fetch(:code)
83
+ @packaging_description = PACKAGING_TYPES.fetch(packaging).fetch(:description)
84
+ @freight_class = freight_class
85
+ @nmfc_primary_code = nmfc_primary_code
86
+ @nmfc_sub_code = nmfc_sub_code
87
+ @hazardous = hazardous
88
+ super(**kwargs)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/tforce_freight/rates_item_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class TForceFreight
8
+ # Options for packages/pallets in a shipment.
9
+ class PackageOptions < 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.
52
+ HANDLING_UNIT_TYPES = {
53
+ pallet: { code: "PLT", description: "Pallet", handling_unit_tag: 'One' },
54
+ skid: { code: "SKD", description: "Skid", handling_unit_tag: 'One' },
55
+ carboy: { code: "CBY", description: "Carboy", handling_unit_tag: 'One' },
56
+ totes: { code: "TOT", description: "Totes", handling_unit_tag: 'One' },
57
+ other: { code: "OTH", description: "Other", handling_unit_tag: 'Two' },
58
+ loose: { code: "LOO", description: "Loose", handling_unit_tag: 'Two' }
59
+ }.freeze
60
+
61
+ # @return [Symbol] the code for this item's packaging
62
+ attr_reader :packaging_code
63
+
64
+ # @return [String] the description for this item's packaging
65
+ attr_reader :packaging_description
66
+
67
+ # @return [String] the freight class
68
+ attr_reader :freight_class
69
+
70
+ # @return [String] the NMFC primary code
71
+ attr_reader :nmfc_primary_code
72
+
73
+ # @return [String] the NMFC sub code
74
+ attr_reader :nmfc_sub_code
75
+
76
+ # @return [Boolean] whether or not the item is hazardous
77
+ attr_reader :hazardous
78
+
79
+ # @return [String] the handling unit description
80
+ attr_reader :handling_unit_description
81
+
82
+ # @return [String] the handling unit tag
83
+ attr_reader :handling_unit_tag
84
+
85
+ # @return [String] the handling unit code
86
+ attr_reader :handling_unit_code
87
+
88
+ # @param packaging [Symbol] this item's packaging
89
+ # @param freight_class [String] the freight class
90
+ # @param nmfc_primary_code [String] the NMFC primary code
91
+ # @param nmfc_sub_code [String] the NMFC sub code
92
+ # @param hazardous [Boolean] whether or not the item is hazardous
93
+ # @param handling_unit [Symbol] the handling unit for this package/pallet
94
+ # @param kwargs [Hash]
95
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
96
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
97
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
98
+ def initialize(
99
+ packaging: :carton,
100
+ freight_class: nil,
101
+ nmfc_primary_code: nil,
102
+ nmfc_sub_code: nil,
103
+ hazardous: false,
104
+ handling_unit: :pallet,
105
+ **kwargs
106
+ )
107
+ @packaging_code = PACKAGING_TYPES.fetch(packaging).fetch(:code)
108
+ @packaging_description = PACKAGING_TYPES.fetch(packaging).fetch(:description)
109
+ @freight_class = freight_class
110
+ @nmfc_primary_code = nmfc_primary_code
111
+ @nmfc_sub_code = nmfc_sub_code
112
+ @hazardous = hazardous
113
+ @handling_unit_code = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:code)
114
+ @handling_unit_description = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:description)
115
+ @handling_unit_tag = "handlingUnit#{HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:handling_unit_tag)}"
116
+ super(**kwargs.reverse_merge(item_options_class: ItemOptions))
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/rate'
4
+ require 'friendly_shipping/api_result'
5
+ require 'friendly_shipping/services/tforce_freight/shipping_methods'
6
+
7
+ module FriendlyShipping
8
+ module Services
9
+ class TForceFreight
10
+ # Parses a Bill of Lading (BOL) response into an `ApiResult`.
11
+ class ParseCreateBOLResponse
12
+ class << self
13
+ # @param request [Request] the original request
14
+ # @param response [RestClient::Response] the response to parse
15
+ # @return [ApiResult<ShipmentInformation>] the parsed result
16
+ def call(request:, response:)
17
+ json = JSON.parse(response.body)
18
+
19
+ bol_id = json.dig("detail", "bolId")
20
+ pro_number = json.dig("detail", "pro")
21
+ pickup_confirmation_number = json.dig("detail", "pickup", "transactionReference", "confirmationNumber")
22
+
23
+ rate_detail = json.dig("detail", "rateDetail")&.first
24
+ if rate_detail
25
+ service_code = rate_detail.dig("service", "code")
26
+ shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_code }
27
+
28
+ rates = rate_detail["rate"].each_with_object({}) do |rate, result|
29
+ result[rate["code"].downcase.to_sym] = rate["value"].to_f
30
+ end
31
+
32
+ total_charges_value = rate_detail.dig("shipmentCharges", "total", "value")
33
+ total_charges_currency = rate_detail.dig("shipmentCharges", "total", "currency")
34
+ total_charges = Money.new(total_charges_value.to_f * 100, total_charges_currency || "USD")
35
+
36
+ billable_weight_value = rate_detail.dig("shipmentWeights", "billable", "value")
37
+ billable_weight_unit = rate_detail.dig("shipmentWeights", "billable", "unit")
38
+ billable_weight = Measured::Weight(billable_weight_value, billable_weight_unit&.downcase || :lb)
39
+
40
+ days_in_transit = rate_detail.dig("timeInTransit", "timeInTransit")&.to_i
41
+ cost_breakdown = build_cost_breakdown(rate_detail)
42
+ end
43
+
44
+ origin_service_center = json.dig("detail", "originServiceCenter")
45
+ email_sent = json.dig("detail", "pickup", "transactionReference", "emailSent") == "true"
46
+ origin_is_rural = json.dig("detail", "pickup", "transactionReference", "originIsRural") == "true"
47
+ destination_is_rural = json.dig("detail", "pickup", "transactionReference", "destinationIsRural") == "true"
48
+
49
+ documents = json.dig("detail", "documents", "image")&.map do |image_data|
50
+ ParseShipmentDocument.call(image_data: image_data)
51
+ end
52
+
53
+ FriendlyShipping::ApiResult.new(
54
+ ShipmentInformation.new(
55
+ bol_id: bol_id,
56
+ pro_number: pro_number,
57
+ pickup_confirmation_number: pickup_confirmation_number,
58
+ origin_service_center: origin_service_center,
59
+ email_sent: email_sent,
60
+ origin_is_rural: origin_is_rural,
61
+ destination_is_rural: destination_is_rural,
62
+ rates: rates,
63
+ total_charges: total_charges,
64
+ billable_weight: billable_weight,
65
+ days_in_transit: days_in_transit,
66
+ shipping_method: shipping_method,
67
+ documents: documents,
68
+ data: {
69
+ cost_breakdown: cost_breakdown
70
+ }
71
+ ),
72
+ original_request: request,
73
+ original_response: response
74
+ )
75
+ end
76
+
77
+ private
78
+
79
+ # @param rate_detail [Hash]
80
+ # @return [Hash]
81
+ def build_cost_breakdown(rate_detail)
82
+ {
83
+ "Rates" => rate_detail["rate"].each_with_object({}) do |rate, hash|
84
+ hash[rate["code"]] = rate["value"]
85
+ end,
86
+ "TotalShipmentCharge" => rate_detail.dig("shipmentCharges", "total", "value"),
87
+ "BillableShipmentWeight" => rate_detail.dig("shipmentWeights", "billable", "value")
88
+ }.compact
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/rate'
4
+ require 'friendly_shipping/api_result'
5
+ require 'friendly_shipping/services/tforce_freight/shipping_methods'
6
+
7
+ module FriendlyShipping
8
+ module Services
9
+ class TForceFreight
10
+ # Parses a pickup response into an `ApiResult`.
11
+ class ParsePickupResponse
12
+ class << self
13
+ # @param request [Request] the original request
14
+ # @param response [RestClient::Response] the response to parse
15
+ # @return [ApiResult<Hash>] the parsed result
16
+ def call(request:, response:)
17
+ json = JSON.parse(response.body)
18
+
19
+ response_status_code = json.dig("responseStatus", "code")
20
+ response_status_description = json.dig("responseStatus", "description")
21
+ transaction_id = json.dig("transactionReference", "transactionId")
22
+ confirmation_number = json.dig("transactionReference", "confirmationNumber")
23
+ email_sent = json.dig("transactionReference", "emailSent")
24
+ origin_is_rural = json.dig("transactionReference", "originIsRural")
25
+ destination_is_rural = json.dig("transactionReference", "destinationIsRural")
26
+
27
+ FriendlyShipping::ApiResult.new(
28
+ {
29
+ response_status_code: response_status_code,
30
+ response_status_description: response_status_description,
31
+ transaction_id: transaction_id,
32
+ confirmation_number: confirmation_number,
33
+ email_sent: email_sent,
34
+ origin_is_rural: origin_is_rural,
35
+ destination_is_rural: destination_is_rural
36
+ },
37
+ original_request: request,
38
+ original_response: response
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/rate'
4
+ require 'friendly_shipping/api_result'
5
+ require 'friendly_shipping/services/tforce_freight/shipping_methods'
6
+
7
+ module FriendlyShipping
8
+ module Services
9
+ class TForceFreight
10
+ # Parses a rates response into an `ApiResult`.
11
+ class ParseRatesResponse
12
+ class << self
13
+ # @param request [Request] the original request
14
+ # @param response [RestClient::Response] the response to parse
15
+ # @return [ApiResult<Array<Rate>>] the parsed result
16
+ def call(request:, response:)
17
+ json = JSON.parse(response.body)
18
+ transaction_id = json.dig("summary", "transactionReference", "transactionId")
19
+
20
+ rates = json['detail'].map do |detail|
21
+ service_code = detail.dig("service", "code")
22
+ shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_code }
23
+
24
+ total_amount = detail.dig("shipmentCharges", "total", "value")
25
+ total_currency = detail.dig("shipmentCharges", "total", "currency")
26
+ total = Money.new(total_amount.to_f * 100, total_currency)
27
+
28
+ rates = detail['rate']
29
+ data = rates.each_with_object({}) do |rate, result|
30
+ result[rate['code'].downcase.to_sym] = rate['value'].to_f
31
+ end
32
+
33
+ data.merge(
34
+ customer_context: transaction_id,
35
+ commodities: Array.wrap(json['commodities'])
36
+ )
37
+
38
+ days_in_transit = detail.dig("timeInTransit", "timeInTransit")
39
+ data[:days_in_transit] = days_in_transit.to_i if days_in_transit
40
+
41
+ FriendlyShipping::Rate.new(
42
+ amounts: { total: total },
43
+ shipping_method: shipping_method,
44
+ data: data
45
+ )
46
+ end
47
+
48
+ FriendlyShipping::ApiResult.new(
49
+ rates,
50
+ original_request: request,
51
+ original_response: response
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/ups_freight/shipment_document'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class TForceFreight
8
+ # Parses shipment document JSON into a `ShipmentDocument`.
9
+ class ParseShipmentDocument
10
+ # Maps document types to friendly names.
11
+ REVERSE_DOCUMENT_TYPES = DocumentOptions::DOCUMENT_TYPES.map(&:reverse_each).to_h(&:to_a)
12
+
13
+ # @param image_data [Hash] the shipping document JSON
14
+ # @return [ShipmentDocument] the parsed shipment document
15
+ def self.call(image_data:)
16
+ type_code = image_data["type"]
17
+ format = image_data["format"]
18
+ status = image_data["status"]
19
+ data = image_data["data"]
20
+
21
+ ShipmentDocument.new(
22
+ document_type: REVERSE_DOCUMENT_TYPES.fetch(type_code),
23
+ document_format: format.downcase.to_sym,
24
+ status: status,
25
+ binary: Base64.decode64(data)
26
+ )
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Options for creating a pickup.
7
+ class PickupOptions < ShipmentOptions
8
+ # Maps friendly names to service options.
9
+ SERVICE_OPTIONS = {
10
+ inside: "INPU",
11
+ liftgate: "LIFO",
12
+ freezable: "PFFF",
13
+ residential: "RESP",
14
+ extreme_length: "EXLT",
15
+ trade_show: "TRPU"
16
+ }.freeze
17
+
18
+ # @return [Time] date/time of pickup
19
+ attr_reader :pickup_at
20
+
21
+ # @return [Range] time window for pickup
22
+ attr_reader :pickup_time_window
23
+
24
+ # @return [Array<String>] shipment pickup service options (see {SERVICE_OPTIONS})
25
+ attr_reader :service_options
26
+
27
+ # @return [String] instructions for pickup
28
+ attr_reader :pickup_instructions
29
+
30
+ # @return [String] instructions for handling
31
+ attr_reader :handling_instructions
32
+
33
+ # @return [String] instructions for delivery
34
+ attr_reader :delivery_instructions
35
+
36
+ # @param pickup_at [Time] date/time of pickup (defaults to now)
37
+ # @param pickup_time_window [Range] time window for pickup (defaults to start/end of today)
38
+ # @param service_options [Array<String>] shipment pickup service options (see {SERVICE_OPTIONS})
39
+ # @param pickup_instructions [String] instructions for pickup
40
+ # @param handling_instructions [String] instructions for handling
41
+ # @param delivery_instructions [String] instructions for delivery
42
+ # @param kwargs [Hash]
43
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
44
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
45
+ def initialize(
46
+ pickup_at: Time.now,
47
+ pickup_time_window: Time.now.beginning_of_day..Time.now.end_of_day,
48
+ service_options: [],
49
+ pickup_instructions: nil,
50
+ handling_instructions: nil,
51
+ delivery_instructions: nil,
52
+ **kwargs
53
+ )
54
+ @pickup_at = pickup_at
55
+ @pickup_time_window = pickup_time_window
56
+ @service_options = service_options
57
+ @pickup_instructions = pickup_instructions
58
+ @handling_instructions = handling_instructions
59
+ @delivery_instructions = delivery_instructions
60
+
61
+ validate_service_options!
62
+
63
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
64
+ end
65
+
66
+ private
67
+
68
+ # Raises an exception if the service options passed into the initializer
69
+ # don't correspond to valid codes from {SERVICE_OPTIONS}.
70
+ #
71
+ # @raise [ArgumentError] invalid service options
72
+ # @return [nil]
73
+ def validate_service_options!
74
+ invalid_options = (service_options - SERVICE_OPTIONS.values)
75
+ return unless invalid_options.any?
76
+
77
+ raise ArgumentError, "Invalid service option(s): #{invalid_options.join(', ')}"
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/tforce_freight/item_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class TForceFreight
8
+ # @deprecated use {FriendlyShipping::Services::TForceFreight::ItemOptions} instead
9
+ RatesItemOptions = ItemOptions
10
+ end
11
+ end
12
+ end