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
@@ -0,0 +1,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/tforce_freight/rates_package_options'
4
+ require 'friendly_shipping/services/tforce_freight/generate_commodity_information'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class TForceFreight
9
+ # Options for getting rates for a shipment.
10
+ class RatesOptions < ShipmentOptions
11
+ # Maps friendly names to billing codes.
12
+ BILLING_CODES = {
13
+ prepaid: "10",
14
+ third_party: "30",
15
+ freight_collect: "40"
16
+ }.freeze
17
+
18
+ # Maps friendly names to type codes.
19
+ TYPE_CODES = {
20
+ l: "L", # LTL (Less Than Truckload) only
21
+ f: "F", # GFP (Ground w/Freight Pricing) only
22
+ b: "B" # Both (LTL and GFP)
23
+ }.freeze
24
+
25
+ # Maps friendly names to pickup options.
26
+ PICKUP_OPTIONS = {
27
+ inside: "INPU",
28
+ liftgate: "LIFO",
29
+ limited_access: "LAPU",
30
+ holiday: "WHPU",
31
+ weekend: "WHPU",
32
+ tradeshow: "TRPU",
33
+ residential: "RESP"
34
+ }.freeze
35
+
36
+ # Maps friendly names to delivery options.
37
+ DELIVERY_OPTIONS = {
38
+ inside: "INDE",
39
+ liftgate: "LIFD",
40
+ limited_access: "LADL",
41
+ holiday: "WHDL",
42
+ weekend: "WHDL",
43
+ call_consignee: "NTFN",
44
+ tradeshow: "TRDS",
45
+ residential: "RESD"
46
+ }.freeze
47
+
48
+ # @return [Physical::Location] the billing address
49
+ attr_reader :billing_address
50
+
51
+ # @return [ShippingMethod] the shipping method
52
+ attr_reader :shipping_method
53
+
54
+ # @return [Date] date of the pickup
55
+ attr_reader :pickup_date
56
+
57
+ # @return [Symbol] how the shipment is billed (see {BILLING_CODES})
58
+ attr_reader :billing_code
59
+
60
+ # @return [String] rating call type (see {TYPE_CODES})
61
+ attr_reader :type_code
62
+
63
+ # @return [Boolean] indicates that the rate request is density based
64
+ attr_reader :density_eligible
65
+
66
+ # @return [Boolean] indicates that the rate is accessorial
67
+ attr_reader :accessorial_rate
68
+
69
+ # @return [Boolean] requests transit timing information be included in the response
70
+ attr_reader :time_in_transit
71
+
72
+ # @return [Boolean] requests a quote number be included in the response
73
+ attr_reader :quote_number
74
+
75
+ # @return [Array<String>] shipment pick up service options (see {PICKUP_OPTIONS})
76
+ attr_reader :pickup_options
77
+
78
+ # @return [Array<String] shipment delivery service options (see {DELIVERY_OPTIONS})
79
+ attr_reader :delivery_options
80
+
81
+ # @return [String] customer context
82
+ attr_reader :customer_context
83
+
84
+ # @return [Callable] a callable that takes a shipment
85
+ attr_reader :commodity_information_generator
86
+
87
+ # @param billing_address [Physical::Location] the billing address
88
+ # @param shipping_method [ShippingMethod] the shipping method to use
89
+ # @param pickup_date [Date] date of the pickup (defaults to today)
90
+ # @param billing [Symbol] how the shipment is billed (see {BILLING_CODES})
91
+ # @param type [String] rating call type (defaults to L, see {TYPE_CODES})
92
+ # @param density_eligible [Boolean] indicates that the rate request is density based (defaults to `false`)
93
+ # @param accessorial_rate [Boolean] indicates that the rate is accessorial (defaults to `false`)
94
+ # @param time_in_transit [Boolean] requests transit timing information be included in the response (defaults to `true`)
95
+ # @param quote_number [Boolean] requests a quote number be included in the response (defaults to `false`)
96
+ # @param pickup_options [Array<String>] shipment pick up service options (see {PICKUP_OPTIONS})
97
+ # @param delivery_options [Array<String] shipment delivery service options (see {DELIVERY_OPTIONS})
98
+ # @param customer_context [String] a reference to match this request with an order or shipment (defaults to `nil`)
99
+ # @param commodity_information_generator [Callable] a callable that takes a shipment
100
+ # and an options object to create an `Array` of commodity fields as per the UPS docs
101
+ # @param kwargs [Hash]
102
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
103
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
104
+ def initialize(
105
+ billing_address:,
106
+ shipping_method:,
107
+ pickup_date: Date.today,
108
+ billing: :prepaid,
109
+ type: :l,
110
+ density_eligible: false,
111
+ accessorial_rate: false,
112
+ time_in_transit: true,
113
+ quote_number: false,
114
+ pickup_options: [],
115
+ delivery_options: [],
116
+ customer_context: nil,
117
+ commodity_information_generator: GenerateCommodityInformation,
118
+ **kwargs
119
+ )
120
+ @pickup_date = pickup_date
121
+ @billing_address = billing_address
122
+ @billing_code = BILLING_CODES.fetch(billing)
123
+ @shipping_method = shipping_method
124
+ @type_code = TYPE_CODES.fetch(type)
125
+ @density_eligible = density_eligible
126
+ @accessorial_rate = accessorial_rate
127
+ @time_in_transit = time_in_transit
128
+ @quote_number = quote_number
129
+ @pickup_options = pickup_options
130
+ @delivery_options = delivery_options
131
+ @customer_context = customer_context
132
+ @commodity_information_generator = commodity_information_generator
133
+
134
+ validate_pickup_options!
135
+ validate_delivery_options!
136
+
137
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
138
+ end
139
+
140
+ private
141
+
142
+ # @raise [ArgumentError] invalid pickup options
143
+ # @return [nil]
144
+ def validate_pickup_options!
145
+ invalid_options = (pickup_options - PICKUP_OPTIONS.values)
146
+ return unless invalid_options.any?
147
+
148
+ raise ArgumentError, "Invalid pickup option(s): #{invalid_options.join(', ')}"
149
+ end
150
+
151
+ # @raise [ArgumentError] invalid delivery options
152
+ # @return [nil]
153
+ def validate_delivery_options!
154
+ invalid_options = (delivery_options - DELIVERY_OPTIONS.values)
155
+ return unless invalid_options.any?
156
+
157
+ raise ArgumentError, "Invalid delivery option(s): #{invalid_options.join(', ')}"
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/tforce_freight/package_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class TForceFreight
8
+ # @deprecated use {FriendlyShipping::Services::TForceFreight::PackageOptions} instead
9
+ RatesPackageOptions = PackageOptions
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # A shipping document (BOL, label, etc.) returned by the API.
7
+ class ShipmentDocument
8
+ # @return [String] the type of document
9
+ attr_reader :document_type
10
+
11
+ # @return [String] the format of the document
12
+ attr_reader :document_format
13
+
14
+ # @return [String] the document's binary data
15
+ attr_reader :binary
16
+
17
+ # @return [String] the status of the document
18
+ attr_reader :status
19
+
20
+ # @param document_type [String] the type of document
21
+ # @param document_format [String] the format of the document
22
+ # @param binary [String] the document's binary data
23
+ # @param status [String] the status of the document (defaults to `nil`)
24
+ def initialize(
25
+ document_type:,
26
+ document_format:,
27
+ binary:,
28
+ status: nil
29
+ )
30
+ @document_type = document_type
31
+ @document_format = document_format
32
+ @binary = binary
33
+ @status = status
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Information for a specific shipment returned by the API.
7
+ class ShipmentInformation
8
+ # @return [String] the shipment's BOL ID number
9
+ attr_reader :bol_id
10
+
11
+ # @return [String] the shipment's PRO number
12
+ attr_reader :pro_number
13
+
14
+ # @return [String] the shipment's pickup confirmation number
15
+ attr_reader :pickup_confirmation_number
16
+
17
+ # @return [String] the origin service center
18
+ attr_reader :origin_service_center
19
+
20
+ # @return [Boolean] whether or not the email was sent
21
+ attr_reader :email_sent
22
+
23
+ # @return [Boolean] whether or not the origin is rural
24
+ attr_reader :origin_is_rural
25
+
26
+ # @return [Boolean] whether or not the destination is rural
27
+ attr_reader :destination_is_rural
28
+
29
+ # @return [Array<Hash>] the rates
30
+ attr_reader :rates
31
+
32
+ # @return [Money] the total charges for this shipment
33
+ attr_reader :total_charges
34
+
35
+ # @return [Measured::Weight] the billable weight for this shipment
36
+ attr_reader :billable_weight
37
+
38
+ # @return [Integer] the number of days in transit
39
+ attr_reader :days_in_transit
40
+
41
+ # @return [Array<ShipmentDocument>] the shipment's documents (BOL, labels, etc)
42
+ attr_reader :documents
43
+
44
+ # @return [ShippingMethod] the shipping method
45
+ attr_reader :shipping_method
46
+
47
+ # @return [Array<String>] any warnings
48
+ attr_reader :warnings
49
+
50
+ # @return [Hash] any additional data
51
+ attr_reader :data
52
+
53
+ # @param bol_id [String] the shipment's BOL ID number
54
+ # @param pro_number [String] the shipment's PRO number
55
+ # @param pickup_confirmation_number [String] the shipment's pickup confirmation number
56
+ # @param origin_service_center [String] the origin service center
57
+ # @param email_sent [Boolean] whether or not the email was sent
58
+ # @param origin_is_rural [Boolean] whether or not the origin is rural
59
+ # @param destination_is_rural [Boolean] whether or not the destination is rural
60
+ # @param rates [Array<Hash>] the rates
61
+ # @param total_charges [Money] the total charges for this shipment
62
+ # @param billable_weight [Measured::Weight] the billable weight for this shipment
63
+ # @param days_in_transit [Integer] the number of days in transit
64
+ # @param documents [Array<ShipmentDocument>] the shipment's documents (BOL, labels, etc)
65
+ # @param shipping_method [ShippingMethod] the shipping method
66
+ # @param warnings [Array<String>] any warnings
67
+ # @param data [Hash] any additional data
68
+ def initialize(
69
+ bol_id:,
70
+ pro_number: nil,
71
+ pickup_confirmation_number: nil,
72
+ origin_service_center: nil,
73
+ email_sent: nil,
74
+ origin_is_rural: nil,
75
+ destination_is_rural: nil,
76
+ rates: [],
77
+ total_charges: nil,
78
+ billable_weight: nil,
79
+ days_in_transit: nil,
80
+ documents: [],
81
+ shipping_method: nil,
82
+ warnings: nil,
83
+ data: {}
84
+ )
85
+ @bol_id = bol_id
86
+ @pro_number = pro_number
87
+ @pickup_confirmation_number = pickup_confirmation_number
88
+ @origin_service_center = origin_service_center
89
+ @email_sent = email_sent
90
+ @origin_is_rural = origin_is_rural
91
+ @destination_is_rural = destination_is_rural
92
+ @rates = rates
93
+ @total_charges = total_charges
94
+ @billable_weight = billable_weight
95
+ @days_in_transit = days_in_transit
96
+ @documents = documents
97
+ @shipping_method = shipping_method
98
+ @warnings = warnings
99
+ @data = data
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -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 TForceFreight
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: StructureOptions,
17
+ **kwargs
18
+ )
19
+ @structure_options = structure_options
20
+ @structure_options_class = structure_options_class
21
+ super(**kwargs.reverse_merge(package_options_class: RatesPackageOptions))
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
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ ORIGIN_COUNTRIES = %w(
7
+ CA MX PR US
8
+ ).map { |country_code| Carmen::Country.coded(country_code) }.freeze
9
+
10
+ SHIPPING_METHODS = [
11
+ ['308', 'TForce Freight LTL'],
12
+ ['309', 'TForce Freight LTL - Guaranteed'],
13
+ ['334', 'TForce Freight LTL - Guaranteed A.M.'],
14
+ ['349', 'TForce Standard LTL']
15
+ ].freeze.map do |code, name|
16
+ FriendlyShipping::ShippingMethod.new(
17
+ name: name,
18
+ service_code: code,
19
+ origin_countries: ORIGIN_COUNTRIES,
20
+ multi_package: true
21
+ ).freeze
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Options for structures/pallets within a TForce Freight shipment.
7
+ class StructureOptions < FriendlyShipping::StructureOptions
8
+ # Maps friendly names to handling unit types.
9
+ HANDLING_UNIT_TYPES = {
10
+ pallet: { code: "PLT", description: "Pallet", handling_unit_tag: 'One' },
11
+ skid: { code: "SKD", description: "Skid", handling_unit_tag: 'One' },
12
+ carboy: { code: "CBY", description: "Carboy", handling_unit_tag: 'One' },
13
+ totes: { code: "TOT", description: "Totes", handling_unit_tag: 'One' },
14
+ other: { code: "OTH", description: "Other", handling_unit_tag: 'Two' },
15
+ loose: { code: "LOO", description: "Loose", handling_unit_tag: 'Two' }
16
+ }.freeze
17
+
18
+ # @return [String]
19
+ attr_reader :handling_unit_description
20
+
21
+ # @return [String]
22
+ attr_reader :handling_unit_tag
23
+
24
+ # @return [String]
25
+ attr_reader :handling_unit_code
26
+
27
+ # @param handling_unit [Symbol] how this shipment is divided (see {HANDLING_UNIT_TYPES})
28
+ # @param kwargs [Hash]
29
+ # @options kwargs [Object] :structure_id unique identifier for this set of options
30
+ # @options kwargs [Array<PackageOptions>] :package_options the options for packages in this structure
31
+ # @options kwargs [Class] :package_options_class the class to use for package options when none are provided
32
+ def initialize(
33
+ handling_unit: :pallet,
34
+ **kwargs
35
+ )
36
+ @handling_unit_code = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:code)
37
+ @handling_unit_description = HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:description)
38
+ @handling_unit_tag = "handlingUnit#{HANDLING_UNIT_TYPES.fetch(handling_unit).fetch(:handling_unit_tag)}"
39
+ super(**kwargs.reverse_merge(package_options_class: RatesPackageOptions))
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,202 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads'
4
+ require 'friendly_shipping/http_client'
5
+ require 'friendly_shipping/services/tforce_freight/access_token'
6
+ require 'friendly_shipping/services/tforce_freight/shipping_methods'
7
+ require 'friendly_shipping/services/tforce_freight/shipment_document'
8
+ require 'friendly_shipping/services/tforce_freight/shipment_information'
9
+ require 'friendly_shipping/services/tforce_freight/shipment_options'
10
+ require 'friendly_shipping/services/tforce_freight/rates_options'
11
+ require 'friendly_shipping/services/tforce_freight/structure_options'
12
+ require 'friendly_shipping/services/tforce_freight/package_options'
13
+ require 'friendly_shipping/services/tforce_freight/rates_package_options'
14
+ require 'friendly_shipping/services/tforce_freight/item_options'
15
+ require 'friendly_shipping/services/tforce_freight/rates_item_options'
16
+ require 'friendly_shipping/services/tforce_freight/pickup_options'
17
+ require 'friendly_shipping/services/tforce_freight/bol_options'
18
+ require 'friendly_shipping/services/tforce_freight/document_options'
19
+ require 'friendly_shipping/services/tforce_freight/parse_rates_response'
20
+ require 'friendly_shipping/services/tforce_freight/parse_pickup_response'
21
+ require 'friendly_shipping/services/tforce_freight/parse_create_bol_response'
22
+ require 'friendly_shipping/services/tforce_freight/parse_shipment_document'
23
+ require 'friendly_shipping/services/tforce_freight/generate_rates_request_hash'
24
+ require 'friendly_shipping/services/tforce_freight/generate_pickup_request_hash'
25
+ require 'friendly_shipping/services/tforce_freight/generate_create_bol_request_hash'
26
+ require 'friendly_shipping/services/tforce_freight/generate_handling_units_hash'
27
+ require 'friendly_shipping/services/tforce_freight/generate_reference_hash'
28
+ require 'friendly_shipping/services/tforce_freight/generate_document_options_hash'
29
+ require 'friendly_shipping/services/tforce_freight/api_error'
30
+
31
+ module FriendlyShipping
32
+ module Services
33
+ class TForceFreight
34
+ include Dry::Monads::Result::Mixin
35
+
36
+ # @return [AccessToken] the access token
37
+ attr_reader :access_token
38
+
39
+ # @return [Boolean] whether to use the test API version
40
+ attr_reader :test
41
+
42
+ # @return [HttpClient] the HTTP client
43
+ attr_reader :client
44
+
45
+ # The TForce Freight carrier
46
+ CARRIER = FriendlyShipping::Carrier.new(
47
+ id: 'tforce_freight',
48
+ name: 'TForce Freight',
49
+ code: 'tforce_freight-freight',
50
+ shipping_methods: SHIPPING_METHODS
51
+ )
52
+
53
+ # The base URL for TForce API requests
54
+ BASE_URL = 'https://api.tforcefreight.com'
55
+
56
+ # The TForce API endpoints
57
+ RESOURCES = {
58
+ rates: '/rating/getRate',
59
+ create_pickup: '/pickup/request',
60
+ create_bol: '/shipping/bol/create'
61
+ }.freeze
62
+
63
+ # @param access_token [AccessToken] the access token
64
+ # @param test [Boolean] whether to use the test API version
65
+ # @param client [HttpClient] optional HTTP client to use for requests
66
+ def initialize(access_token:, test: true, client: nil)
67
+ @access_token = access_token
68
+ @test = test
69
+
70
+ error_handler = ApiErrorHandler.new(api_error_class: TForceFreight::ApiError)
71
+ @client = client || HttpClient.new(error_handler: error_handler)
72
+ end
73
+
74
+ # @return [Array<Carrier>]
75
+ def carriers
76
+ Success([CARRIER])
77
+ end
78
+
79
+ # Creates an access token that can be used for future API requests.
80
+ # @see https://developer.tforcefreight.com/resources/integration-end-user End User Integration Guide
81
+ #
82
+ # @param token_endpoint [String] the Token Endpoint from the Application Integration section of your OAuth Client dialog
83
+ # @param client_id [String] the Client ID of your application from the OAuth Client dialog
84
+ # @param client_secret [String] the secret you created in the OAuth Client Secrets section of the OAuth Client dialog
85
+ # @param scope [String] the Scope value from the Application Integration section your OAuth Client dialog
86
+ # @param grant_type [String] defaults to "client_credentials" to indicate you wish to obtain a token representing your confidential client application
87
+ # @param debug [Boolean] whether to append debug information to the API result
88
+ # @return [ApiResult<AccessToken>] the access token
89
+ def create_access_token(
90
+ token_endpoint:,
91
+ client_id:,
92
+ client_secret:,
93
+ scope:,
94
+ grant_type: "client_credentials",
95
+ debug: false
96
+ )
97
+ request = FriendlyShipping::Request.new(
98
+ url: token_endpoint,
99
+ http_method: "POST",
100
+ body: "client_id=#{client_id}&" \
101
+ "client_secret=#{client_secret}&" \
102
+ "grant_type=#{grant_type}&" \
103
+ "scope=#{scope}",
104
+ headers: {
105
+ Content_Type: "application/x-www-form-urlencoded",
106
+ Accept: "application/json"
107
+ },
108
+ debug: debug
109
+ )
110
+ client.post(request).fmap do |response|
111
+ hash = JSON.parse(response.body)
112
+ FriendlyShipping::ApiResult.new(
113
+ AccessToken.new(
114
+ token_type: hash['token_type'],
115
+ expires_in: hash['expires_in'],
116
+ ext_expires_in: hash['ext_expires_in'],
117
+ raw_token: hash['access_token']
118
+ ),
119
+ original_request: request,
120
+ original_response: response
121
+ )
122
+ end
123
+ end
124
+
125
+ # Get rates for a shipment
126
+ # @see https://developer.tforcefreight.com/api-details#api=rating-v1&operation=get-rate API documentation
127
+ #
128
+ # @param shipment [Physical::Shipment] the shipment for which we want to get rates
129
+ # @param options [RatesOptions] options for obtaining rates for this shipment
130
+ # @param debug [Boolean] whether to append debug information to the API result
131
+ # @return [Result<ApiResult<Array<Rate>>>] the rates returned from TForce encoded in a `ApiResult` object
132
+ def rates(shipment, options:, debug: false)
133
+ freight_rate_request_hash = GenerateRatesRequestHash.call(shipment: shipment, options: options)
134
+ request = build_request(:rates, freight_rate_request_hash, debug)
135
+
136
+ client.post(request).fmap do |response|
137
+ ParseRatesResponse.call(response: response, request: request)
138
+ end
139
+ end
140
+
141
+ # Create a pickup request
142
+ # @see https://developer.tforcefreight.com/api-details#api=pickup-v1&operation=create-request API documentation
143
+ #
144
+ # @param shipment [Physical::Shipment] the shipment for which to create a pickup request
145
+ # @param options [PickupOptions] options for the pickup request
146
+ # @param debug [Boolean] whether to append debug information to the API result
147
+ # @return [Result<ApiResult>] the pickup returned from TForce encoded in a `ApiResult` object
148
+ def create_pickup(shipment, options:, debug: false)
149
+ pickup_request_hash = GeneratePickupRequestHash.call(shipment: shipment, options: options)
150
+ request = build_request(:create_pickup, pickup_request_hash, debug)
151
+
152
+ client.post(request).fmap do |response|
153
+ ParsePickupResponse.call(response: response, request: request)
154
+ end
155
+ end
156
+
157
+ # Create a Bill of Lading (BOL)
158
+ # @see https://developer.tforcefreight.com/api-details#api=shipping-cie-vnext&operation=shipping-create-bol API documentation
159
+ #
160
+ # @param shipment [Physical::Shipment] the shipment for which to create a BOL
161
+ # @param options [BOLOptions] options for the BOL
162
+ # @param debug [Boolean] whether to append debug information to the API result
163
+ # @return [Result<ApiResult>] the BOL returned from TForce encoded in a `ApiResult` object
164
+ def create_bol(shipment, options:, debug: false)
165
+ bol_request_hash = GenerateCreateBOLRequestHash.call(shipment: shipment, options: options)
166
+ request = build_request(:create_bol, bol_request_hash, debug)
167
+
168
+ client.post(request).fmap do |response|
169
+ ParseCreateBOLResponse.call(response: response, request: request)
170
+ end
171
+ end
172
+
173
+ alias_method :rate_estimates, :rates
174
+
175
+ private
176
+
177
+ # @param action [Symbol] the desired action key from {RESOURCES}
178
+ # @param payload [Hash] the payload to send to the API
179
+ # @param debug [Boolean] whether to append debug information to the API result
180
+ # @return [Request]
181
+ def build_request(action, payload, debug)
182
+ url = BASE_URL + RESOURCES[action] + "?api-version=#{api_version}"
183
+ FriendlyShipping::Request.new(
184
+ url: url,
185
+ http_method: "POST",
186
+ body: payload.to_json,
187
+ headers: {
188
+ Content_Type: "application/json",
189
+ Accept: "application/json",
190
+ Authorization: "Bearer #{access_token.raw_token}"
191
+ },
192
+ debug: debug
193
+ )
194
+ end
195
+
196
+ # @return [String] the API version to use
197
+ def api_version
198
+ test ? "cie-v1" : "v1"
199
+ end
200
+ end
201
+ end
202
+ end