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,180 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Options for creating a Bill of Lading (BOL).
7
+ class BOLOptions < ShipmentOptions
8
+ # Maps friendly names to billing codes.
9
+ BILLING_CODES = {
10
+ prepaid: "10",
11
+ third_party: "30",
12
+ freight_collect: "40"
13
+ }.freeze
14
+
15
+ # Maps friendly names to reference number codes.
16
+ REFERENCE_NUMBER_CODES = {
17
+ bill_of_lading_number: "BL",
18
+ purchase_order_number: "PO",
19
+ shipper_reference: "SH",
20
+ consignee_reference: "CO",
21
+ pm: "PM",
22
+ proj: "PROJ",
23
+ quote: "QUOTE",
24
+ sid: "SID",
25
+ task: "TASK",
26
+ vprc: "VPRC",
27
+ other: "OTHER"
28
+ }.freeze
29
+
30
+ # Maps friendly names to pickup options.
31
+ PICKUP_OPTIONS = {
32
+ inside: "INPU",
33
+ liftgate: "LIFO",
34
+ limited_access: "LAPU",
35
+ holiday: "WHPU",
36
+ weekend: "WHPU",
37
+ tradeshow: "TRPU",
38
+ residential: "RESP"
39
+ }.freeze
40
+
41
+ # Maps friendly names to delivery options.
42
+ DELIVERY_OPTIONS = {
43
+ inside: "INDE",
44
+ liftgate: "LIFD",
45
+ limited_access: "LADL",
46
+ holiday: "WHDL",
47
+ weekend: "WHDL",
48
+ call_consignee: "NTFN",
49
+ tradeshow: "TRDS",
50
+ residential: "RESD"
51
+ }.freeze
52
+
53
+ # @return [String] the service code to use
54
+ attr_reader :service_code
55
+
56
+ # @return [Time] date/time of pickup
57
+ attr_reader :pickup_at
58
+
59
+ # @return [Range] time window for pickup
60
+ attr_reader :pickup_time_window
61
+
62
+ # @return [Boolean] whether to preview rates in the response
63
+ attr_reader :preview_rate
64
+
65
+ # @return [Boolean] whether to include time in transit in the response
66
+ attr_reader :time_in_transit
67
+
68
+ # @return [String] code indicating how to bill this shipment (see {BILLING_CODES})
69
+ attr_reader :billing_code
70
+
71
+ # @return [Array<Hash>] reference numbers for this shipment (see {REFERENCE_NUMBER_CODES})
72
+ attr_reader :reference_numbers
73
+
74
+ # @return [String] instructions for pickup
75
+ attr_reader :pickup_instructions
76
+
77
+ # @return [String] instructions for handling
78
+ attr_reader :handling_instructions
79
+
80
+ # @return [String] instructions for delivery
81
+ attr_reader :delivery_instructions
82
+
83
+ # @return [Array<String>] options for pickup (see {PICKUP_OPTIONS})
84
+ attr_reader :pickup_options
85
+
86
+ # @return [Array<String>] options for delivery (see {DELIVERY_OPTIONS})
87
+ attr_reader :delivery_options
88
+
89
+ # @return [Array<DocumentOptions>] options for documents
90
+ attr_reader :document_options
91
+
92
+ # @return [Proc, #call] the callable used generate commodity information
93
+ attr_reader :commodity_information_generator
94
+
95
+ # @param service_code [String] the service code to use
96
+ # @param pickup_at [Time] date/time of pickup (defaults to now)
97
+ # @param pickup_time_window [Range] time window for pickup (defaults to start/end of today)
98
+ # @param preview_rate [Boolean] whether to preview rates in the response
99
+ # @param time_in_transit [Boolean] whether to include time in transit in the response
100
+ # @param billing [Symbol] how to bill this shipment (see {BILLING_CODES})
101
+ # @param reference_numbers [Array<Hash>] reference numbers for this shipment (see {REFERENCE_NUMBER_CODES})
102
+ # @param pickup_instructions [String] instructions for pickup
103
+ # @param handling_instructions [String] instructions for handling
104
+ # @param delivery_instructions [String] instructions for delivery
105
+ # @param pickup_options [Array<String>] options for pickup (see {PICKUP_OPTIONS})
106
+ # @param delivery_options [Array<String>] options for delivery (see {DELIVERY_OPTIONS})
107
+ # @param document_options [Array<DocumentOptions>] options for documents
108
+ # @param commodity_information_generator [Proc, #call] the callable used generate commodity information
109
+ # @param kwargs [Hash]
110
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
111
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
112
+ def initialize(
113
+ service_code: FriendlyShipping::Services::TForceFreight::SHIPPING_METHODS.first.service_code,
114
+ pickup_at: Time.now,
115
+ pickup_time_window: Time.now.beginning_of_day..Time.now.end_of_day,
116
+ preview_rate: false,
117
+ time_in_transit: false,
118
+ billing: :prepaid,
119
+ reference_numbers: [],
120
+ pickup_instructions: nil,
121
+ handling_instructions: nil,
122
+ delivery_instructions: nil,
123
+ pickup_options: [],
124
+ delivery_options: [],
125
+ document_options: [],
126
+ commodity_information_generator: GenerateCommodityInformation,
127
+ **kwargs
128
+ )
129
+ @service_code = service_code
130
+ @pickup_at = pickup_at
131
+ @pickup_time_window = pickup_time_window
132
+ @preview_rate = preview_rate
133
+ @time_in_transit = time_in_transit
134
+ @billing_code = BILLING_CODES.fetch(billing)
135
+ @reference_numbers = fill_codes(reference_numbers)
136
+ @pickup_instructions = pickup_instructions
137
+ @handling_instructions = handling_instructions
138
+ @delivery_instructions = delivery_instructions
139
+ @pickup_options = pickup_options
140
+ @delivery_options = delivery_options
141
+ @document_options = document_options
142
+ @commodity_information_generator = commodity_information_generator
143
+
144
+ validate_pickup_options!
145
+ validate_delivery_options!
146
+
147
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
148
+ end
149
+
150
+ private
151
+
152
+ # @param reference_numbers [Array<Hash>]
153
+ # @return [Array<Hash>]
154
+ def fill_codes(reference_numbers)
155
+ reference_numbers.each do |reference_number|
156
+ reference_number[:code] = REFERENCE_NUMBER_CODES.fetch(reference_number[:code])
157
+ end
158
+ end
159
+
160
+ # @raise [ArgumentError] invalid pickup options
161
+ # @return [nil]
162
+ def validate_pickup_options!
163
+ invalid_options = (pickup_options - PICKUP_OPTIONS.values)
164
+ return unless invalid_options.any?
165
+
166
+ raise ArgumentError, "Invalid pickup option(s): #{invalid_options.join(', ')}"
167
+ end
168
+
169
+ # @raise [ArgumentError] invalid delivery options
170
+ # @return [nil]
171
+ def validate_delivery_options!
172
+ invalid_options = (delivery_options - DELIVERY_OPTIONS.values)
173
+ return unless invalid_options.any?
174
+
175
+ raise ArgumentError, "Invalid delivery option(s): #{invalid_options.join(', ')}"
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Options for getting documents (BOLs, labels, etc.) from the API.
7
+ class DocumentOptions
8
+ # @return [Symbol] the document type (see {DOCUMENT_TYPES})
9
+ attr_reader :type
10
+
11
+ # @return [Symbol] the document format (see {DOCUMENT_FORMATS})
12
+ attr_reader :format
13
+
14
+ # @return [Boolean] whether or not the label is for thermal printers (DEPRECATED: use `label_type` instead)
15
+ attr_reader :thermal
16
+
17
+ # @return [String] the type of label
18
+ attr_reader :label_type
19
+
20
+ # @return [Integer] the start position of the sticker
21
+ attr_reader :start_position
22
+
23
+ # @return [Integer] the number of stickers
24
+ attr_reader :number_of_stickers
25
+
26
+ # Maps friendly names to document types.
27
+ DOCUMENT_TYPES = {
28
+ label: "30",
29
+ tforce_bol: "20",
30
+ vics_bol: "21"
31
+ }.freeze
32
+
33
+ # Maps friendly names to document formats.
34
+ DOCUMENT_FORMATS = {
35
+ pdf: "01"
36
+ }.freeze
37
+
38
+ # Maps booleans to thermal codes.
39
+ THERMAL_CODE = {
40
+ false => "01",
41
+ true => "02"
42
+ }.freeze
43
+
44
+ # Maps friendly names to label types.
45
+ LABEL_TYPE_CODES = {
46
+ address_labels_with_pro_nums: "01",
47
+ address_labels_without_pro_nums: "02",
48
+ pro_stickers: "03",
49
+ address_labels_1x1: "04",
50
+ address_labels_2x1: "05",
51
+ address_labels_2x2: "06",
52
+ thermal_labels_4x6: "07",
53
+ thermal_labels_4x8: "08"
54
+ }.freeze
55
+
56
+ # @param type [Symbol] the document type (see [DOCUMENT_TYPES])
57
+ # @param format [Symbol] the document format (see [DOCUMENT_FORMATS])
58
+ # @param thermal [Boolean] whether or not the label is for thermal printers (DEPRECATED: use `label_type` instead)
59
+ # @param start_position [Integer] the start position of the sticker
60
+ # @param number_of_stickers [Integer] the number of stickers
61
+ def initialize(
62
+ type: :label,
63
+ format: :pdf,
64
+ thermal: false,
65
+ label_type: :thermal_labels_4x6,
66
+ start_position: 1,
67
+ number_of_stickers: 1
68
+ )
69
+ @type = type
70
+ @format = format
71
+ @thermal = thermal
72
+ @label_type = label_type
73
+ @start_position = start_position
74
+ @number_of_stickers = number_of_stickers
75
+ end
76
+
77
+ # @return [String]
78
+ def format_code
79
+ DOCUMENT_FORMATS.fetch(format)
80
+ end
81
+
82
+ # @return [String]
83
+ def document_type_code
84
+ DOCUMENT_TYPES.fetch(type)
85
+ end
86
+
87
+ # @return [String]
88
+ # @deprecated Use `label_type` instead.
89
+ def thermal_code
90
+ THERMAL_CODE.fetch(thermal)
91
+ end
92
+
93
+ # @return [String]
94
+ def label_type_code
95
+ LABEL_TYPE_CODES.fetch(label_type)
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Generates a commodity information hash for JSON serialization.
7
+ class GenerateCommodityInformation
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment with commodities to serialize
10
+ # @param options [#options_for_structure, #options_for_package] the options to serialize
11
+ # @return [Array<Hash>] commodities hash suitable for JSON request
12
+ def call(shipment:, options:)
13
+ if shipment.structures.any?
14
+ serialize_structures(shipment.structures, options)
15
+ else
16
+ warn "[DEPRECATION] `packages` is deprecated. Please use `structures` instead."
17
+ serialize_packages(shipment.packages, options)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ # @param structures [Array<Physical::Structure>]
24
+ # @param options [#options_for_structure] the options to serialize
25
+ # @return [Array<Hash>]
26
+ def serialize_structures(structures, options)
27
+ structures.flat_map do |structure|
28
+ structure_options = options.options_for_structure(structure)
29
+ structure.packages.map do |package|
30
+ package_options = structure_options.options_for_package(package)
31
+ {
32
+ description: package.description.presence || "Commodities",
33
+ class: package_options.freight_class,
34
+ nmfc: {
35
+ prime: package_options.nmfc_primary_code,
36
+ sub: package_options.nmfc_sub_code
37
+ }.compact.presence,
38
+ pieces: 1, # We don't support grouping yet
39
+ weight: {
40
+ weight: package.weight.convert_to(:pounds).value.to_f.round(2),
41
+ weightUnit: "LBS"
42
+ },
43
+ packagingType: package_options.packaging_code,
44
+ dangerousGoods: package_options.hazardous,
45
+ dimensions: {
46
+ length: package.length.convert_to(:inches).value.to_f.round(2),
47
+ width: package.width.convert_to(:inches).value.to_f.round(2),
48
+ height: package.height.convert_to(:inches).value.to_f.round(2),
49
+ unit: "IN"
50
+ }
51
+ }.compact
52
+ end
53
+ end
54
+ end
55
+
56
+ # @param packages [Array<Physical::Package>]
57
+ # @param options [#options_for_package] the options to serialize
58
+ # @return [Array<Hash>]
59
+ def serialize_packages(packages, options)
60
+ packages.flat_map do |package|
61
+ package_options = options.options_for_package(package)
62
+ package.items.map do |item|
63
+ item_options = package_options.options_for_item(item)
64
+ {
65
+ description: item.description.presence || "Commodities",
66
+ class: item_options.freight_class,
67
+ nmfc: {
68
+ prime: item_options.nmfc_primary_code,
69
+ sub: item_options.nmfc_sub_code
70
+ }.compact.presence,
71
+ pieces: 1, # We don't support grouping yet
72
+ weight: {
73
+ weight: item.weight.convert_to(:pounds).value.to_f.round(2),
74
+ weightUnit: "LBS"
75
+ },
76
+ packagingType: item_options.packaging_code,
77
+ dangerousGoods: item_options.hazardous,
78
+ dimensions: {
79
+ length: item.length.convert_to(:inches).value.to_f.round(2),
80
+ width: item.width.convert_to(:inches).value.to_f.round(2),
81
+ height: item.height.convert_to(:inches).value.to_f.round(2),
82
+ unit: "IN"
83
+ }
84
+ }.compact
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Generates a Bill of Lading (BOL) request hash for JSON serialization.
7
+ class GenerateCreateBOLRequestHash
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment for which we want to create a BOL
10
+ # @param options [BOLOptions] options for the BOL
11
+ # @return [Hash] BOL request hash
12
+ def call(shipment:, options:)
13
+ origin = shipment.origin
14
+ destination = shipment.destination
15
+ {
16
+ requestOptions: request_options(options),
17
+ shipFrom: location(origin).merge(isResidential: origin.residential?),
18
+ shipTo: location(destination).merge(isResidential: destination.residential?),
19
+ payment: payment(origin, options),
20
+ commodities: options.commodity_information_generator.call(shipment: shipment, options: options),
21
+ instructions: instructions(options),
22
+ serviceOptions: service_options(options),
23
+ pickupRequest: pickup_request(origin, options),
24
+ documents: { image: documents(options.document_options) }
25
+ }.compact.
26
+ merge(GenerateHandlingUnitsHash.call(shipment: shipment, options: options)).
27
+ merge(GenerateReferenceHash.call(reference_numbers: options.reference_numbers))
28
+ end
29
+
30
+ private
31
+
32
+ # @param options [BOLOptions]
33
+ # @return [Hash]
34
+ def request_options(options)
35
+ {
36
+ serviceCode: options.service_code,
37
+ pickupDate: options.pickup_at&.strftime("%Y-%m-%d"),
38
+ previewRate: options.preview_rate,
39
+ timeInTransit: options.time_in_transit
40
+ }.compact
41
+ end
42
+
43
+ # @param location [Physical::Location]
44
+ # @return [Hash]
45
+ def location(location)
46
+ {
47
+ name: truncate(location.company_name.presence || location.name),
48
+ contact: truncate(location.name),
49
+ email: truncate(location.email, length: 50),
50
+ phone: {
51
+ number: truncate(location.phone, length: 15)
52
+ }.compact,
53
+ address: {
54
+ addressLine: truncate(address_line(location)),
55
+ city: location.city,
56
+ stateProvinceCode: location.region&.code,
57
+ postalCode: truncate(location.zip, length: 6),
58
+ country: location.country&.code
59
+ }.compact
60
+ }.compact_blank
61
+ end
62
+
63
+ # @param location [Physical::Location]
64
+ # @param options [BOLOptions]
65
+ # @return [Hash]
66
+ def payment(location, options)
67
+ {
68
+ payer: location(location),
69
+ billingCode: options.billing_code
70
+ }
71
+ end
72
+
73
+ # @param location [Physical::Location]
74
+ # @return [String, nil]
75
+ def address_line(location)
76
+ [
77
+ location.address1,
78
+ location.address2,
79
+ location.address3
80
+ ].compact.reject(&:blank?).join(" ").presence
81
+ end
82
+
83
+ # @param options [BOLOptions]
84
+ # @return [Hash]
85
+ def instructions(options)
86
+ {
87
+ pickup: options.pickup_instructions,
88
+ handling: options.handling_instructions,
89
+ delivery: options.delivery_instructions
90
+ }.compact
91
+ end
92
+
93
+ # @param options [BOLOptions]
94
+ # @return [Hash]
95
+ def service_options(options)
96
+ {
97
+ pickup: options.pickup_options,
98
+ delivery: options.delivery_options
99
+ }
100
+ end
101
+
102
+ # @param location [Physical::Location]
103
+ # @param options [PickupOptions]
104
+ # @return [Hash]
105
+ def pickup_request(location, options)
106
+ {
107
+ pickup: pickup(options),
108
+ requester: requester(location),
109
+ pomIndicator: false # We don't support this yet
110
+ }
111
+ end
112
+
113
+ # @param options [PickupOptions]
114
+ # @return [Hash]
115
+ def pickup(options)
116
+ {
117
+ date: options.pickup_at&.strftime("%Y-%m-%d"),
118
+ time: options.pickup_at&.strftime("%H:%M:%S"),
119
+ openTime: options.pickup_time_window&.begin&.strftime("%H:%M:%S"),
120
+ closeTime: options.pickup_time_window&.end&.strftime("%H:%M:%S")
121
+ }.compact
122
+ end
123
+
124
+ # @param location [Physical::Location]
125
+ # @return [Hash]
126
+ def requester(location)
127
+ {
128
+ companyName: truncate(location.company_name.presence || location.name),
129
+ contactName: truncate(location.name),
130
+ email: truncate(location.email, length: 50),
131
+ phone: {
132
+ number: truncate(location.phone, length: 15)
133
+ }.compact
134
+ }.compact
135
+ end
136
+
137
+ # @param document_options [Array<DocumentOptions>]
138
+ # @return [Array<Hash>]
139
+ def documents(document_options)
140
+ document_options.map { GenerateDocumentOptionsHash.call(document_options: _1) }
141
+ end
142
+
143
+ # @param value [String]
144
+ # @param length [Integer]
145
+ # @return [String]
146
+ def truncate(value, length: 35)
147
+ value && value[0..(length - 1)].strip
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Generates a document options hash for JSON serialization.
7
+ class GenerateDocumentOptionsHash
8
+ class << self
9
+ # @param document_options [DocumentOptions] the document options
10
+ # @return [Hash] document options hash suitable for JSON request
11
+ def call(document_options:)
12
+ {
13
+ type: document_options.document_type_code,
14
+ format: document_options.format_code,
15
+ label: label(document_options)
16
+ }.compact
17
+ end
18
+
19
+ private
20
+
21
+ # @param document_options [DocumentOptions] the document options
22
+ # @return [Hash, nil] label hash or nil if this is a BOL
23
+ def label(document_options)
24
+ return unless document_options.type == :label
25
+
26
+ {
27
+ type: document_options.label_type_code,
28
+ startPosition: document_options.start_position,
29
+ numberOfStickers: document_options.number_of_stickers
30
+ }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Generates a handling units hash for JSON serialization.
7
+ class GenerateHandlingUnitsHash
8
+ class << self
9
+ # @param shipment [Physical::Shipment]
10
+ # @param options [FriendlyShipping::ShipmentOptions]
11
+ # @return [Hash] handling units hash suitable for JSON request
12
+ def call(shipment:, options:)
13
+ handling_units(shipment, options).reduce(&:merge)
14
+ end
15
+
16
+ private
17
+
18
+ # @param shipment [Physical::Shipment]
19
+ # @param options [FriendlyShipping::ShipmentOptions]
20
+ # @return [Array<Hash>]
21
+ def handling_units(shipment, options)
22
+ if shipment.packages.any?
23
+ warn "[DEPRECATION] `packages` is deprecated. Please use `structures` instead."
24
+ all_package_options = shipment.packages.map { |package| options.options_for_package(package) }
25
+ all_package_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
26
+ [options_group.first, options_group.length]
27
+ end.map { |package_options, quantity| handling_unit_hash(package_options, quantity) }
28
+ else
29
+ all_structure_options = shipment.structures.map { |structure| options.options_for_structure(structure) }
30
+ all_structure_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
31
+ [options_group.first, options_group.length]
32
+ end.map { |structure_options, quantity| handling_unit_hash(structure_options, quantity) }
33
+ end
34
+ end
35
+
36
+ # @param options [PackageOptions, StructureOptions]
37
+ # @param quantity [Integer]
38
+ # @return [Hash]
39
+ def handling_unit_hash(options, quantity)
40
+ {
41
+ options.handling_unit_tag.to_sym => {
42
+ quantity: quantity,
43
+ typeCode: options.handling_unit_code
44
+ }
45
+ }
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Generates a location hash for JSON serialization.
7
+ class GenerateLocationHash
8
+ class << self
9
+ # @param location [Physical::Location] the location
10
+ # @return [Hash] location hash suitable for JSON request
11
+ def call(location:)
12
+ {
13
+ address: {
14
+ city: location.city,
15
+ stateProvinceCode: location.region&.code,
16
+ postalCode: location.zip&.strip&.[](0..4),
17
+ country: location.country&.code
18
+ }.compact
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end