friendly_shipping 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +3 -0
  4. data/.env.test.local.template +6 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop-relaxed.yml +7 -23
  7. data/.rubocop.yml +11 -2
  8. data/.rubocop_todo.yml +21 -0
  9. data/.yardopts +6 -0
  10. data/CHANGELOG.md +71 -0
  11. data/Gemfile +17 -0
  12. data/friendly_shipping.gemspec +4 -13
  13. data/lib/friendly_shipping/api_error.rb +6 -4
  14. data/lib/friendly_shipping/api_error_handler.rb +10 -6
  15. data/lib/friendly_shipping/api_failure.rb +4 -0
  16. data/lib/friendly_shipping/api_result.rb +16 -4
  17. data/lib/friendly_shipping/carrier.rb +28 -8
  18. data/lib/friendly_shipping/http_client.rb +25 -2
  19. data/lib/friendly_shipping/inflections.rb +10 -0
  20. data/lib/friendly_shipping/item_options.rb +3 -0
  21. data/lib/friendly_shipping/label.rb +41 -20
  22. data/lib/friendly_shipping/package_options.rb +21 -2
  23. data/lib/friendly_shipping/rate.rb +50 -15
  24. data/lib/friendly_shipping/request.rb +23 -7
  25. data/lib/friendly_shipping/response.rb +21 -6
  26. data/lib/friendly_shipping/services/rl/api_error.rb +33 -0
  27. data/lib/friendly_shipping/services/rl/bol_options.rb +107 -0
  28. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  29. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  30. data/lib/friendly_shipping/services/rl/item_options.rb +38 -0
  31. data/lib/friendly_shipping/services/rl/package_options.rb +40 -0
  32. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +46 -0
  33. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +50 -0
  34. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +47 -0
  35. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +66 -0
  37. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +76 -0
  38. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +86 -0
  39. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  41. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  42. data/lib/friendly_shipping/services/rl/serialize_location.rb +46 -0
  43. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  44. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  45. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  46. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  47. data/lib/friendly_shipping/services/rl/shipment_options.rb +50 -0
  48. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  49. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  50. data/lib/friendly_shipping/services/rl.rb +204 -0
  51. data/lib/friendly_shipping/services/ship_engine/api_error.rb +33 -0
  52. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  53. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  54. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -5
  55. data/lib/friendly_shipping/services/ship_engine/label_options.rb +31 -14
  56. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -11
  57. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +77 -0
  58. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -0
  59. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -0
  60. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +26 -6
  61. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +101 -0
  62. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +4 -0
  63. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -4
  64. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +28 -0
  65. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +61 -0
  66. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +20 -0
  67. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  68. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  70. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  71. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  72. data/lib/friendly_shipping/services/ship_engine.rb +94 -21
  73. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  74. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +24 -5
  75. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +39 -4
  76. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +4 -0
  77. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +10 -0
  78. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +29 -11
  79. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +7 -2
  80. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +50 -16
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  82. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  83. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl.rb +50 -30
  85. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +43 -0
  86. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +42 -0
  87. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  88. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  89. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  90. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +165 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +113 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +65 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  97. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  98. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +121 -0
  99. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +94 -0
  100. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +58 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +31 -0
  103. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  104. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +12 -0
  105. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +162 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +12 -0
  107. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  108. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +49 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  111. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  112. data/lib/friendly_shipping/services/tforce_freight.rb +202 -0
  113. data/lib/friendly_shipping/services/ups/label_options.rb +14 -2
  114. data/lib/friendly_shipping/services/ups/label_package_options.rb +1 -1
  115. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  116. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +12 -0
  117. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  118. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -1
  119. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -1
  120. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  121. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +2 -1
  122. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  123. data/lib/friendly_shipping/services/ups.rb +1 -1
  124. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -5
  125. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  126. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +3 -20
  127. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +2 -20
  128. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  129. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  130. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  131. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -2
  132. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -1
  133. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  134. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +31 -21
  135. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -8
  136. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +46 -0
  137. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  138. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +49 -0
  139. data/lib/friendly_shipping/services/ups_freight.rb +3 -1
  140. data/lib/friendly_shipping/services/ups_json/access_token.rb +29 -0
  141. data/lib/friendly_shipping/services/ups_json/api_error.rb +29 -0
  142. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  143. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  144. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +211 -0
  145. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  146. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +86 -0
  147. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  148. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  149. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  150. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +77 -0
  151. data/lib/friendly_shipping/services/ups_json/label_options.rb +177 -0
  152. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +51 -0
  153. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  154. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +44 -0
  155. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  156. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  157. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  158. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +105 -0
  159. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  160. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  161. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +22 -0
  162. data/lib/friendly_shipping/services/ups_json/rates_options.rb +113 -0
  163. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  164. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  165. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  166. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  167. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  168. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  169. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +6 -6
  170. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +6 -6
  171. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -1
  172. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -1
  173. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -2
  174. data/lib/friendly_shipping/services/usps/shipping_methods.rb +4 -3
  175. data/lib/friendly_shipping/services/usps.rb +1 -1
  176. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +3 -3
  177. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -1
  178. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +1 -1
  179. data/lib/friendly_shipping/services/usps_international.rb +1 -1
  180. data/lib/friendly_shipping/services/usps_ship/access_token.rb +37 -0
  181. data/lib/friendly_shipping/services/usps_ship/api_error.rb +29 -0
  182. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +53 -0
  183. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  184. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +80 -0
  185. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  186. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +124 -0
  187. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +55 -0
  188. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  189. data/lib/friendly_shipping/services/{ship_engine_ltl/bad_request.rb → usps_ship/timing_options.rb} +2 -2
  190. data/lib/friendly_shipping/services/usps_ship.rb +199 -0
  191. data/lib/friendly_shipping/shipment_options.rb +13 -1
  192. data/lib/friendly_shipping/shipping_method.rb +38 -11
  193. data/lib/friendly_shipping/structure_options.rb +38 -0
  194. data/lib/friendly_shipping/timing.rb +42 -7
  195. data/lib/friendly_shipping/version.rb +1 -1
  196. data/lib/friendly_shipping.rb +6 -0
  197. metadata +140 -174
  198. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  199. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
  200. data/lib/friendly_shipping/services/ship_engine_ltl/bad_request_handler.rb +0 -33
@@ -3,36 +3,52 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class ShipEngineLTL
6
+ # Serializes a shipment and options for the rate quote API request.
6
7
  class SerializeQuoteRequest
7
8
  class << self
8
- # @param [Physical::Shipment] shipment
9
- # @param [FriendlyShipping::Services::ShipEngineLTL::QuoteOptions] options
9
+ # @param shipment [Physical::Shipment] the shipment to serialize
10
+ # @param options [QuoteOptions] the options to serialize
11
+ # @return [Hash] the serialized request
10
12
  def call(shipment:, options:)
11
13
  {
12
14
  shipment: {
13
15
  service_code: options.service_code,
14
16
  pickup_date: options.pickup_date.strftime('%Y-%m-%d'),
15
- packages: options.packages_serializer_class.call(packages: shipment.packages, options: options),
17
+ packages: serialize_packages(shipment, options),
16
18
  options: serialize_options(options),
17
19
  ship_from: serialize_ship_address(shipment.origin),
18
20
  ship_to: serialize_ship_address(shipment.destination),
19
21
  bill_to: serialize_bill_address(shipment.origin),
20
22
  requested_by: serialize_requested_by(shipment.origin),
21
23
  }.compact,
22
- shipment_measurements: serialize_shipment_measurements(shipment.packages)
24
+ shipment_measurements: serialize_shipment_measurements(shipment, options)
23
25
  }
24
26
  end
25
27
 
26
28
  private
27
29
 
28
- # @param [FriendlyShipping::Services::ShipEngineLTL::QuoteOptions] options
30
+ # @param shipment [Physical::Shipment]
31
+ # @param options [QuoteOptions]
32
+ # @return [Hash]
33
+ def serialize_packages(shipment, options)
34
+ if options.packages_serializer_class
35
+ warn "[DEPRECATION] `packages_serializer_class` is deprecated. Please use `structures_serializer_class` instead."
36
+ options.packages_serializer_class.call(packages: shipment.packages, options: options)
37
+ else
38
+ options.structures_serializer_class.call(structures: shipment.structures, options: options)
39
+ end
40
+ end
41
+
42
+ # @param options [QuoteOptions]
43
+ # @return [Array<Hash>]
29
44
  def serialize_options(options)
30
45
  options.accessorial_service_codes.map do |code|
31
46
  { code: code }
32
47
  end
33
48
  end
34
49
 
35
- # @param [Physical::Location] location
50
+ # @param location [Physical::Location]
51
+ # @return [Hash]
36
52
  def serialize_ship_address(location)
37
53
  {
38
54
  account: location.properties.with_indifferent_access['account_number'],
@@ -41,7 +57,8 @@ module FriendlyShipping
41
57
  }.compact
42
58
  end
43
59
 
44
- # @param [Physical::Location] location
60
+ # @param location [Physical::Location]
61
+ # @return [Hash]
45
62
  def serialize_bill_address(location)
46
63
  {
47
64
  type: "shipper",
@@ -52,7 +69,8 @@ module FriendlyShipping
52
69
  }.compact
53
70
  end
54
71
 
55
- # @param [Physical::Location] location
72
+ # @param location [Physical::Location]
73
+ # @return [Hash]
56
74
  def serialize_address(location)
57
75
  {
58
76
  company_name: location.company_name,
@@ -64,7 +82,8 @@ module FriendlyShipping
64
82
  }.compact
65
83
  end
66
84
 
67
- # @param [Physical::Location] location
85
+ # @param location [Physical::Location]
86
+ # @return [Hash]
68
87
  def serialize_contact(location)
69
88
  {
70
89
  name: location.name,
@@ -73,7 +92,8 @@ module FriendlyShipping
73
92
  }.compact
74
93
  end
75
94
 
76
- # @param [Physical::Location] location
95
+ # @param location [Physical::Location]
96
+ # @return [Hash]
77
97
  def serialize_requested_by(location)
78
98
  {
79
99
  company_name: location.company_name,
@@ -81,23 +101,37 @@ module FriendlyShipping
81
101
  }.compact
82
102
  end
83
103
 
84
- # @param [Array<Physical::Package>] packages
85
- def serialize_shipment_measurements(packages)
104
+ # @param shipment [Physical::Shipment]
105
+ # @param options [QuoteOptions]
106
+ # @return [Hash]
107
+ def serialize_shipment_measurements(shipment, options)
108
+ if options.packages_serializer_class
109
+ warn "[DEPRECATION] `packages_serializer_class` is deprecated. Please use `structures_serializer_class` instead."
110
+ total_length = shipment.packages.sum(&:length)
111
+ max_width = shipment.packages.map(&:width).max
112
+ max_height = shipment.packages.map(&:height).max
113
+ total_weight = shipment.packages.sum(&:weight)
114
+ else
115
+ total_length = shipment.structures.sum(&:length)
116
+ max_width = shipment.structures.map(&:width).max
117
+ max_height = shipment.structures.map(&:height).max
118
+ total_weight = shipment.structures.sum(&:weight)
119
+ end
86
120
  {
87
121
  total_linear_length: {
88
- value: packages.sum(&:length).convert_to(:inches).value.ceil,
122
+ value: total_length.convert_to(:inches).value.ceil,
89
123
  unit: "inches"
90
124
  },
91
125
  total_width: {
92
- value: packages.map(&:width).max.convert_to(:inches).value.ceil,
126
+ value: max_width.convert_to(:inches).value.ceil,
93
127
  unit: "inches"
94
128
  },
95
129
  total_height: {
96
- value: packages.map(&:height).max.convert_to(:inches).value.ceil,
130
+ value: max_height.convert_to(:inches).value.ceil,
97
131
  unit: "inches"
98
132
  },
99
133
  total_weight: {
100
- value: +packages.sum(&:weight).convert_to(:pounds).value.ceil,
134
+ value: total_weight.convert_to(:pounds).value.ceil,
101
135
  unit: "pounds"
102
136
  }
103
137
  }
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngineLTL
6
+ class SerializeStructures
7
+ class << self
8
+ # @param structures [Array<Physical::Structure>]
9
+ # @param options [FriendlyShipping::Services::ShipEngineLTL::QuoteOptions]
10
+ # @return [Array<Hash>]
11
+ def call(structures:, options:)
12
+ structures.flat_map do |structure|
13
+ structure_options = options.options_for_structure(structure)
14
+ structure.packages.map do |package|
15
+ package_options = structure_options.options_for_package(package)
16
+ {
17
+ code: package_options.packaging_code,
18
+ freight_class: package_options.freight_class,
19
+ nmfc_code: package_options.nmfc_code,
20
+ description: package.description || "Commodities",
21
+ dimensions: {
22
+ width: package.width.convert_to(:inches).value.ceil,
23
+ height: package.height.convert_to(:inches).value.ceil,
24
+ length: package.length.convert_to(:inches).value.ceil,
25
+ unit: "inches"
26
+ },
27
+ weight: {
28
+ value: package.weight.convert_to(:pounds).value.ceil,
29
+ unit: "pounds"
30
+ },
31
+ quantity: 1, # we don't support this yet
32
+ stackable: package_options.stackable,
33
+ hazardous_materials: package_options.hazardous_materials
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngineLTL
6
+ class ShipmentOptions < FriendlyShipping::ShipmentOptions
7
+ # @param structure_options [Array<StructureOptions>]
8
+ # @param structure_options_class [Class]
9
+ # @param kwargs [Hash]
10
+ # @option kwargs [Array<PackageOptions>] :package_options
11
+ # @option kwargs [Class] :package_options_class
12
+ def initialize(
13
+ structure_options: Set.new,
14
+ structure_options_class: StructureOptions,
15
+ **kwargs
16
+ )
17
+ @structure_options = structure_options
18
+ @structure_options_class = structure_options_class
19
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
20
+ end
21
+
22
+ # @param [#id] structure
23
+ # @return [StructureOptions]
24
+ def options_for_structure(structure)
25
+ structure_options.detect do |structure_option|
26
+ structure_option.structure_id == structure.id
27
+ end || structure_options_class.new(structure_id: nil)
28
+ end
29
+
30
+ private
31
+
32
+ # @return [Array<StructureOptions>]
33
+ attr_reader :structure_options
34
+
35
+ # @return [Class]
36
+ attr_reader :structure_options_class
37
+
38
+ # @return [Array<PackageOptions>]
39
+ # @deprecated Use {#structure_options} instead.
40
+ def package_options
41
+ warn "[DEPRECATION] `package_options` is deprecated. Please use `structure_options` instead."
42
+ @package_options
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngineLTL
6
+ class StructureOptions < FriendlyShipping::StructureOptions
7
+ # @param kwargs [Hash]
8
+ # @option kwargs [String] :structure_id
9
+ # @option kwargs [Array<PackageOptions>] :package_options
10
+ # @option kwargs [Class] :package_options_class
11
+ def initialize(**kwargs)
12
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -2,42 +2,51 @@
2
2
 
3
3
  require 'dry/monads'
4
4
  require 'friendly_shipping/http_client'
5
- require 'friendly_shipping/services/ship_engine_ltl/bad_request_handler'
5
+ require 'friendly_shipping/services/ship_engine_ltl/api_error'
6
6
  require 'friendly_shipping/services/ship_engine_ltl/parse_carrier_response'
7
7
  require 'friendly_shipping/services/ship_engine_ltl/parse_quote_response'
8
8
  require 'friendly_shipping/services/ship_engine_ltl/serialize_packages'
9
9
  require 'friendly_shipping/services/ship_engine_ltl/serialize_quote_request'
10
+ require 'friendly_shipping/services/ship_engine_ltl/serialize_structures'
11
+ require 'friendly_shipping/services/ship_engine_ltl/shipment_options'
10
12
  require 'friendly_shipping/services/ship_engine_ltl/quote_options'
13
+ require 'friendly_shipping/services/ship_engine_ltl/structure_options'
11
14
  require 'friendly_shipping/services/ship_engine_ltl/package_options'
12
15
  require 'friendly_shipping/services/ship_engine_ltl/item_options'
13
16
 
14
17
  module FriendlyShipping
15
18
  module Services
19
+ # API service class for ShipEngine LTL, a shipping API supporting Freight carriers.
20
+ # @see https://www.shipengine.com/docs/ltl/ ShipEngine LTL API docs
16
21
  class ShipEngineLTL
17
22
  include Dry::Monads::Result::Mixin
18
23
 
24
+ # The API base URL.
19
25
  API_BASE = "https://api.shipengine.com/v-beta/ltl/"
26
+
27
+ # The API paths. Used when constructing endpoint URLs.
20
28
  API_PATHS = {
21
29
  connections: "connections",
22
30
  carriers: "carriers",
23
31
  quotes: "quotes"
24
32
  }.freeze
25
33
 
26
- def initialize(
27
- token:,
28
- test: true,
29
- client: FriendlyShipping::HttpClient.new(
30
- error_handler: FriendlyShipping::Services::ShipEngineLTL::BadRequestHandler
31
- )
32
- )
34
+ # @param token [String] the API token
35
+ # @param test [Boolean] whether to use test API endpoints
36
+ # @param client [HttpClient] optional custom HTTP client to use for requests
37
+ def initialize(token:, test: true, client: nil)
33
38
  @token = token
34
39
  @test = test
35
- @client = client
40
+
41
+ error_handler = ApiErrorHandler.new(api_error_class: ShipEngineLTL::ApiError)
42
+ @client = client || HttpClient.new(error_handler: error_handler)
36
43
  end
37
44
 
38
- # Get configured LTL carriers from ShipEngine
45
+ # Get configured LTL carriers.
39
46
  #
40
- # @return [Result<ApiResult<Array<Carrier>>>] LTL carriers configured in your account
47
+ # @param debug [Boolean] whether to attach debugging information to the response
48
+ # @return [Success<ApiResult<Array<Carrier>>>, Failure<ApiFailure<Array<String>>>] On success,
49
+ # LTL carriers configured in your account. On failure, a list of error messages.
41
50
  def carriers(debug: false)
42
51
  request = FriendlyShipping::Request.new(
43
52
  url: API_BASE + API_PATHS[:carriers],
@@ -49,12 +58,13 @@ module FriendlyShipping
49
58
  end
50
59
  end
51
60
 
52
- # Connect an LTL carrier to ShipEngine
53
- #
54
- # @param [Hash] credentials The carrier's connection information
55
- # @param [String] scac Standard Carrier Alpha Code
61
+ # Connect an LTL carrier.
56
62
  #
57
- # @return [Result<ApiResult<Hash>>] The unique carrier ID assigned by ShipEngine
63
+ # @param credentials [Hash] the carrier's connection information
64
+ # @param scac [String] Standard Carrier Alpha Code
65
+ # @see https://nmfta.org/scac/ SCAC
66
+ # @param debug [Boolean] whether to attach debugging information to the response
67
+ # @return [Success<ApiResult<Hash>>, Failure<ApiFailure<String>>] the carrier or error message
58
68
  def connect_carrier(credentials, scac, debug: false)
59
69
  request = FriendlyShipping::Request.new(
60
70
  url: API_BASE + API_PATHS[:connections] + "/#{scac}",
@@ -73,13 +83,14 @@ module FriendlyShipping
73
83
  end
74
84
  end
75
85
 
76
- # Update an existing LTL carrier in ShipEngine
86
+ # Update an existing LTL carrier.
77
87
  #
78
- # @param [Hash] credentials The carrier's connection information
79
- # @param [String] scac Standard Carrier Alpha Code
80
- # @param [String] carrier_id The carrier ID from ShipEngine that you want to update
81
- #
82
- # @return [Result<ApiResult<Hash>>] The unique carrier ID assigned by ShipEngine
88
+ # @param credentials [Hash] the carrier's connection information
89
+ # @param scac [String] Standard Carrier Alpha Code
90
+ # @see https://nmfta.org/scac/ SCAC
91
+ # @param carrier_id [String] the ID for the carrier you want to update
92
+ # @param debug [Boolean] whether to attach debugging information to the response
93
+ # @return [Success<ApiResult<Hash>>, Failure<ApiFailure<String>>] the carrier or error message
83
94
  def update_carrier(credentials, scac, carrier_id, debug: false)
84
95
  request = FriendlyShipping::Request.new(
85
96
  url: API_BASE + API_PATHS[:connections] + "/#{scac}/#{carrier_id}",
@@ -98,13 +109,13 @@ module FriendlyShipping
98
109
  end
99
110
  end
100
111
 
101
- # Request an LTL price quote from ShipEngine
102
- #
103
- # @param [String] carrier_id The carrier ID from ShipEngine that you want to quote against
104
- # @param [Physical::Shipment] shipment The shipment to quote
105
- # @param [FriendlyShipping::Services::ShipEngineLTL::QuoteOptions] options The options for the quote
112
+ # Request an LTL price quote.
106
113
  #
107
- # @return [Result<ApiResult<Hash>>] The price quote from ShipEngine
114
+ # @param carrier_id [String] the carrier ID
115
+ # @param shipment [Physical::Shipment] the shipment to quote
116
+ # @param options [QuoteOptions] the options for the quote
117
+ # @param debug [Boolean] whether to attach debugging information to the response
118
+ # @return [Success<ApiResult<Hash>>, Failure<ApiFailure<String>>] the quote or error message
108
119
  def request_quote(carrier_id, shipment, options, debug: false)
109
120
  request = FriendlyShipping::Request.new(
110
121
  url: API_BASE + API_PATHS[:quotes] + "/#{carrier_id}",
@@ -120,12 +131,21 @@ module FriendlyShipping
120
131
 
121
132
  private
122
133
 
123
- attr_reader :token, :test, :client
134
+ # @return [String] the API token
135
+ attr_reader :token
136
+
137
+ # @return [Boolean] whether to use test API endpoints
138
+ attr_reader :test
139
+
140
+ # @return [HttpClient] the HTTP client to use for requests
141
+ attr_reader :client
124
142
 
143
+ # Returns the content type and API key as a headers hash.
144
+ # @return [Hash]
125
145
  def request_headers
126
146
  {
127
147
  content_type: :json,
128
- "api-key": token
148
+ 'api-key': token
129
149
  }
130
150
  end
131
151
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jwt'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class TForceFreight
8
+ # Represents an access token returned by TForce Freight. The access token can be
9
+ # used to make API requests. Once it expires, a new token must be created.
10
+ class AccessToken
11
+ # @return [String] the token's type
12
+ attr_reader :token_type
13
+
14
+ # @return [Integer] the token's expiration
15
+ attr_reader :expires_in
16
+
17
+ # @return [Integer] the token's extended expiration
18
+ attr_reader :ext_expires_in
19
+
20
+ # @return [String] the raw JWT token
21
+ attr_reader :raw_token
22
+
23
+ # @param token_type [String] the token's type (typically "Bearer")
24
+ # @param expires_in [Integer] the token's expiration
25
+ # @param ext_expires_in [Integer] the token's extended expiration (only applicable during a service outage)
26
+ # @see https://github.com/AzureAD/azure-activedirectory-library-for-android/issues/675 Service Outage Resiliency Support
27
+ # @param raw_token [String] the raw JWT token
28
+ def initialize(token_type:, expires_in:, ext_expires_in:, raw_token:)
29
+ @token_type = token_type
30
+ @expires_in = expires_in
31
+ @ext_expires_in = ext_expires_in
32
+ @raw_token = raw_token
33
+ end
34
+
35
+ # Decodes and returns the raw JWT token.
36
+ # @return [Array<Hash>] the decoded token
37
+ def decoded_token
38
+ @_decoded_token = JWT.decode(raw_token, nil, false)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/api_error'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class TForceFreight
8
+ # Raised when an API error is returned.
9
+ class ApiError < FriendlyShipping::ApiError
10
+ # @param cause [RestClient::Exception]
11
+ def initialize(cause)
12
+ super(cause, parse_message(cause))
13
+ end
14
+
15
+ private
16
+
17
+ # @param error [RestClient::Exception]
18
+ # @return [String]
19
+ def parse_message(error)
20
+ return error.message unless error.response
21
+
22
+ parsed_json = JSON.parse(error.response.body)
23
+ if parsed_json['summary'].present?
24
+ status = parsed_json.dig("summary", "responseStatus", "code")
25
+ message = parsed_json.dig("summary", "responseStatus", "message").presence ||
26
+ parsed_json.dig("summary", "responseStatus", "description")
27
+ elsif parsed_json['responseStatus'].present?
28
+ status = parsed_json.dig("responseStatus", "code")
29
+ message = parsed_json.dig("responseStatus", "description")
30
+ else
31
+ status = parsed_json['statusCode']
32
+ message = parsed_json['message'].presence ||
33
+ parsed_json['description']
34
+ end
35
+ [status, message].compact.join(": ")
36
+ rescue JSON::ParserError, KeyError => _e
37
+ nil
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -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