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
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Generates a pickup request hash for JSON serialization.
7
+ class GeneratePickupRequestHash
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment for which we want to create a pickup request
10
+ # @param options [PickupOptions] options for the pickup request
11
+ # @return [Hash] pickup request hash
12
+ def call(shipment:, options:)
13
+ {
14
+ pickup: pickup(options),
15
+ requester: requester(shipment.origin),
16
+ origin: origin(shipment.origin),
17
+ destination: destination(shipment.destination),
18
+ services: options.service_options,
19
+ lineItems: line_items(shipment.packages, options),
20
+ instructions: instructions(options),
21
+ pomIndicator: false # We don't support this yet
22
+ }.compact
23
+ end
24
+
25
+ private
26
+
27
+ # @param options [PickupOptions]
28
+ # @return [Hash]
29
+ def pickup(options)
30
+ {
31
+ date: options.pickup_at&.strftime("%Y-%m-%d"),
32
+ time: options.pickup_at&.strftime("%H:%M:%S"),
33
+ openTime: options.pickup_time_window&.begin&.strftime("%H:%M:%S"),
34
+ closeTime: options.pickup_time_window&.end&.strftime("%H:%M:%S")
35
+ }.compact
36
+ end
37
+
38
+ # @param location [Physical::Location]
39
+ # @return [Hash]
40
+ def requester(location)
41
+ {
42
+ companyName: location.company_name.presence || location.name,
43
+ contactName: location.name,
44
+ email: location.email,
45
+ phone: { number: location.phone }.compact
46
+ }.compact
47
+ end
48
+
49
+ # @param location [Physical::Location]
50
+ # @return [Hash]
51
+ def origin(location)
52
+ {
53
+ companyName: location.company_name.presence || location.name,
54
+ contactName: location.name,
55
+ email: location.email,
56
+ phone: { number: location.phone },
57
+ address: {
58
+ address1: location.address1,
59
+ address2: location.address2,
60
+ address3: location.address3,
61
+ city: location.city,
62
+ stateProvinceCode: location.region&.code,
63
+ postalCode: location.zip,
64
+ country: location.country&.code
65
+ }.compact
66
+ }.compact
67
+ end
68
+
69
+ # @param location [Physical::Location]
70
+ # @return [Hash]
71
+ def destination(location)
72
+ {
73
+ postalCode: location.zip,
74
+ country: location.country&.code
75
+ }.compact
76
+ end
77
+
78
+ # @param packages [Array<Physical::Package>]
79
+ # @param options [Physical::Location]
80
+ # @return [Array<Hash>]
81
+ def line_items(packages, options)
82
+ packages.flat_map do |package|
83
+ package_options = options.options_for_package(package)
84
+ package.items.map do |item|
85
+ item_options = package_options.options_for_item(item)
86
+ {
87
+ description: item.description.presence || "Commodities",
88
+ weight: item.weight.convert_to(:pounds).value.to_f.ceil,
89
+ weightUnit: "LBS",
90
+ pieces: 1,
91
+ packagingType: item_options.packaging_code,
92
+ hazardous: item_options.hazardous
93
+ }.compact
94
+ end
95
+ end
96
+ end
97
+
98
+ # @param options [PickupOptions]
99
+ # @return [Hash]
100
+ def instructions(options)
101
+ {
102
+ pickup: options.pickup_instructions,
103
+ handling: options.handling_instructions,
104
+ delivery: options.delivery_instructions
105
+ }.compact
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Generates a rates request hash for JSON serialization.
7
+ class GenerateRatesRequestHash
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment for which we want to get rates
10
+ # @param options [RatesOptions] options for obtaining rates for this shipment
11
+ # @return [Hash] rates request hash
12
+ def call(shipment:, options:)
13
+ {
14
+ requestOptions: request_options(options),
15
+ shipFrom: GenerateLocationHash.call(location: shipment.origin),
16
+ shipTo: GenerateLocationHash.call(location: shipment.destination),
17
+ payment: payment(options),
18
+ serviceOptions: service_options(options),
19
+ commodities: options.commodity_information_generator.call(shipment: shipment, options: options)
20
+ }
21
+ end
22
+
23
+ private
24
+
25
+ # @param options [RatesOptions]
26
+ # @return [Hash]
27
+ def request_options(options)
28
+ {
29
+ serviceCode: options.shipping_method.service_code,
30
+ pickupDate: options.pickup_date.strftime('%Y-%m-%d'),
31
+ type: options.type_code,
32
+ densityEligible: options.density_eligible,
33
+ gfpOptions: {
34
+ accessorialRate: options.accessorial_rate,
35
+ },
36
+ timeInTransit: options.time_in_transit,
37
+ quoteNumber: options.quote_number,
38
+ customerContext: options.customer_context
39
+ }.compact
40
+ end
41
+
42
+ # @param options [RatesOptions]
43
+ # @return [Hash]
44
+ def payment(options)
45
+ {
46
+ payer: GenerateLocationHash.call(location: options.billing_address),
47
+ billingCode: options.billing_code
48
+ }
49
+ end
50
+
51
+ # @param options [RatesOptions]
52
+ # @return [Hash]
53
+ def service_options(options)
54
+ {
55
+ pickup: options.pickup_options,
56
+ delivery: options.delivery_options
57
+ }
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Generates a reference hash for JSON serialization.
7
+ class GenerateReferenceHash
8
+ class << self
9
+ # @param reference_numbers [Array] reference numbers for the Bill of Lading
10
+ # @return [Hash] reference hash suitable for JSON request
11
+ def call(reference_numbers:)
12
+ return {} unless reference_numbers
13
+
14
+ references = reference_numbers.map do |reference_number|
15
+ {
16
+ number: reference_number[:value],
17
+ type: reference_number[:code],
18
+ quantity: reference_number[:quantity],
19
+ weight: reference_number[:weight]
20
+ }.compact
21
+ end
22
+ references.any? ? { references: references } : {}
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -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,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Options for packages/pallets in a shipment.
7
+ class PackageOptions < 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.
50
+ HANDLING_UNIT_TYPES = {
51
+ pallet: { code: "PLT", description: "Pallet", handling_unit_tag: 'One' },
52
+ skid: { code: "SKD", description: "Skid", handling_unit_tag: 'One' },
53
+ carboy: { code: "CBY", description: "Carboy", handling_unit_tag: 'One' },
54
+ totes: { code: "TOT", description: "Totes", handling_unit_tag: 'One' },
55
+ other: { code: "OTH", description: "Other", handling_unit_tag: 'Two' },
56
+ loose: { code: "LOO", description: "Loose", handling_unit_tag: 'Two' }
57
+ }.freeze
58
+
59
+ # @return [Symbol] the code for this item's packaging
60
+ attr_reader :packaging_code
61
+
62
+ # @return [String] the description for this item's packaging
63
+ attr_reader :packaging_description
64
+
65
+ # @return [String] the freight class
66
+ attr_reader :freight_class
67
+
68
+ # @return [String] the NMFC primary code
69
+ attr_reader :nmfc_primary_code
70
+
71
+ # @return [String] the NMFC sub code
72
+ attr_reader :nmfc_sub_code
73
+
74
+ # @return [Boolean] whether or not the item is hazardous
75
+ attr_reader :hazardous
76
+
77
+ # @return [String] the handling unit description
78
+ attr_reader :handling_unit_description
79
+
80
+ # @return [String] the handling unit tag
81
+ attr_reader :handling_unit_tag
82
+
83
+ # @return [String] the handling unit code
84
+ attr_reader :handling_unit_code
85
+
86
+ # @param packaging [Symbol] this item's packaging
87
+ # @param freight_class [String] the freight class
88
+ # @param nmfc_primary_code [String] the NMFC primary code
89
+ # @param nmfc_sub_code [String] the NMFC sub code
90
+ # @param hazardous [Boolean] whether or not the item is hazardous
91
+ # @param handling_unit [Symbol] the handling unit for this package/pallet
92
+ # @param kwargs [Hash]
93
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
94
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
95
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
96
+ def initialize(
97
+ packaging: :carton,
98
+ freight_class: nil,
99
+ nmfc_primary_code: nil,
100
+ nmfc_sub_code: nil,
101
+ hazardous: false,
102
+ handling_unit: :pallet,
103
+ **kwargs
104
+ )
105
+ @packaging_code = PACKAGING_TYPES.fetch(packaging).fetch(:code)
106
+ @packaging_description = PACKAGING_TYPES.fetch(packaging).fetch(:description)
107
+ @freight_class = freight_class
108
+ @nmfc_primary_code = nmfc_primary_code
109
+ @nmfc_sub_code = nmfc_sub_code
110
+ @hazardous = hazardous
111
+ @handling_unit_code = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:code)
112
+ @handling_unit_description = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:description)
113
+ @handling_unit_tag = "handlingUnit#{HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:handling_unit_tag)}"
114
+ super(**kwargs.reverse_merge(item_options_class: ItemOptions))
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Parses a Bill of Lading (BOL) response into an `ApiResult`.
7
+ class ParseCreateBOLResponse
8
+ class << self
9
+ # @param request [Request] the original request
10
+ # @param response [RestClient::Response] the response to parse
11
+ # @return [ApiResult<ShipmentInformation>] the parsed result
12
+ def call(request:, response:)
13
+ json = JSON.parse(response.body)
14
+
15
+ bol_id = json.dig("detail", "bolId")
16
+ pro_number = json.dig("detail", "pro")
17
+ pickup_confirmation_number = json.dig("detail", "pickup", "transactionReference", "confirmationNumber")
18
+
19
+ rate_detail = json.dig("detail", "rateDetail")&.first
20
+ if rate_detail
21
+ service_code = rate_detail.dig("service", "code")
22
+ shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_code }
23
+
24
+ rates = rate_detail["rate"].each_with_object({}) do |rate, result|
25
+ result[rate["code"].downcase.to_sym] = rate["value"].to_f
26
+ end
27
+
28
+ total_charges_value = rate_detail.dig("shipmentCharges", "total", "value")
29
+ total_charges_currency = rate_detail.dig("shipmentCharges", "total", "currency")
30
+ total_charges = Money.new(total_charges_value.to_f * 100, total_charges_currency || "USD")
31
+
32
+ billable_weight_value = rate_detail.dig("shipmentWeights", "billable", "value")
33
+ billable_weight_unit = rate_detail.dig("shipmentWeights", "billable", "unit")
34
+ billable_weight = Measured::Weight(billable_weight_value, billable_weight_unit&.downcase || :lb)
35
+
36
+ days_in_transit = rate_detail.dig("timeInTransit", "timeInTransit")&.to_i
37
+ cost_breakdown = build_cost_breakdown(rate_detail)
38
+ end
39
+
40
+ origin_service_center = json.dig("detail", "originServiceCenter")
41
+ email_sent = json.dig("detail", "pickup", "transactionReference", "emailSent") == "true"
42
+ origin_is_rural = json.dig("detail", "pickup", "transactionReference", "originIsRural") == "true"
43
+ destination_is_rural = json.dig("detail", "pickup", "transactionReference", "destinationIsRural") == "true"
44
+
45
+ documents = json.dig("detail", "documents", "image")&.map do |image_data|
46
+ ParseShipmentDocument.call(image_data: image_data)
47
+ end
48
+
49
+ FriendlyShipping::ApiResult.new(
50
+ ShipmentInformation.new(
51
+ bol_id: bol_id,
52
+ pro_number: pro_number,
53
+ pickup_confirmation_number: pickup_confirmation_number,
54
+ origin_service_center: origin_service_center,
55
+ email_sent: email_sent,
56
+ origin_is_rural: origin_is_rural,
57
+ destination_is_rural: destination_is_rural,
58
+ rates: rates,
59
+ total_charges: total_charges,
60
+ billable_weight: billable_weight,
61
+ days_in_transit: days_in_transit,
62
+ shipping_method: shipping_method,
63
+ documents: documents,
64
+ data: {
65
+ cost_breakdown: cost_breakdown
66
+ }
67
+ ),
68
+ original_request: request,
69
+ original_response: response
70
+ )
71
+ end
72
+
73
+ private
74
+
75
+ # @param rate_detail [Hash]
76
+ # @return [Hash]
77
+ def build_cost_breakdown(rate_detail)
78
+ {
79
+ "Rates" => rate_detail["rate"].each_with_object({}) do |rate, hash|
80
+ hash[rate["code"]] = rate["value"]
81
+ end,
82
+ "TotalShipmentCharge" => rate_detail.dig("shipmentCharges", "total", "value"),
83
+ "BillableShipmentWeight" => rate_detail.dig("shipmentWeights", "billable", "value")
84
+ }.compact
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ 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,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Parses a rates response into an `ApiResult`.
7
+ class ParseRatesResponse
8
+ class << self
9
+ # @param request [Request] the original request
10
+ # @param response [RestClient::Response] the response to parse
11
+ # @return [ApiResult<Array<Rate>>] the parsed result
12
+ def call(request:, response:)
13
+ json = JSON.parse(response.body)
14
+ transaction_id = json.dig("summary", "transactionReference", "transactionId")
15
+
16
+ rates = json['detail'].map do |detail|
17
+ service_code = detail.dig("service", "code")
18
+ shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_code }
19
+
20
+ total_amount = detail.dig("shipmentCharges", "total", "value")
21
+ total_currency = detail.dig("shipmentCharges", "total", "currency")
22
+ total = Money.new(total_amount.to_f * 100, total_currency)
23
+
24
+ data = {
25
+ customer_context: transaction_id,
26
+ commodities: Array.wrap(json['commodities']),
27
+ cost_breakdown: detail['rate'].map { |rate| rate.slice(*%w[code description value unit]) }
28
+ }
29
+
30
+ days_in_transit = detail.dig("timeInTransit", "timeInTransit")
31
+ data[:days_in_transit] = days_in_transit.to_i if days_in_transit
32
+
33
+ FriendlyShipping::Rate.new(
34
+ amounts: { total: total },
35
+ shipping_method: shipping_method,
36
+ data: data
37
+ )
38
+ end
39
+
40
+ FriendlyShipping::ApiResult.new(
41
+ rates,
42
+ original_request: request,
43
+ original_response: response
44
+ )
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Parses shipment document JSON into a `ShipmentDocument`.
7
+ class ParseShipmentDocument
8
+ # Maps document types to friendly names.
9
+ REVERSE_DOCUMENT_TYPES = DocumentOptions::DOCUMENT_TYPES.map(&:reverse_each).to_h(&:to_a)
10
+
11
+ # @param image_data [Hash] the shipping document JSON
12
+ # @return [ShipmentDocument] the parsed shipment document
13
+ def self.call(image_data:)
14
+ type_code = image_data["type"]
15
+ format = image_data["format"]
16
+ status = image_data["status"]
17
+ data = image_data["data"]
18
+
19
+ ShipmentDocument.new(
20
+ document_type: REVERSE_DOCUMENT_TYPES.fetch(type_code),
21
+ document_format: format.downcase.to_sym,
22
+ status: status,
23
+ binary: Base64.decode64(data)
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end