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
@@ -1,32 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/shipment_options'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class ShipEngineLTL
6
+ # Options for rate quote requests.
8
7
  class QuoteOptions < ShipmentOptions
9
- attr_reader :service_code,
10
- :pickup_date,
11
- :accessorial_service_codes,
12
- :packages_serializer_class
8
+ # @return [String] the service code to use when getting rates
9
+ attr_reader :service_code
10
+
11
+ # @return [Time] the pickup date
12
+ attr_reader :pickup_date
13
+
14
+ # @return [Array<String>] the accessorial service codes
15
+ attr_reader :accessorial_service_codes
16
+
17
+ # @return [Class] the class to use for serializing structures
18
+ attr_reader :structures_serializer_class
19
+
20
+ # @return [Class] the class to use for serializing packages
21
+ # @deprecated Use {#structures_serializer_class} instead.
22
+ attr_reader :packages_serializer_class
13
23
 
14
- # @param [String] service_code
15
- # @param [Time] pickup_date
16
- # @param [Array<String>] accessorial_service_codes
17
- # @param [Class] packages_serializer_class
24
+ # @param service_code [String] the service code to use when getting rates
25
+ # @param pickup_date [Time] the pickup date
26
+ # @param accessorial_service_codes [Array<String>] the accessorial service codes (if any)
27
+ # @param packages_serializer_class [Class] the class to use for serializing packages (DEPRECATED: use `structures_serializer_class` instead)
28
+ # @param structures_serializer_class [Class] the class to use for serializing structures
29
+ # @param kwargs [Hash]
30
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment (DEPRECATED: use `structure_options` instead)
31
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided (DEPRECATED: use `structure_options_class` instead)
32
+ # @option kwargs [Array<StructureOptions>] :structure_options the options for structures in this shipment
33
+ # @option kwargs [Class] :structure_options_class the class to use for structure options when none are provided
18
34
  def initialize(
19
35
  service_code: nil,
20
36
  pickup_date: nil,
21
37
  accessorial_service_codes: [],
38
+ structures_serializer_class: SerializeStructures,
22
39
  packages_serializer_class: SerializePackages,
23
40
  **kwargs
24
41
  )
25
42
  @service_code = service_code
26
43
  @pickup_date = pickup_date
27
44
  @accessorial_service_codes = accessorial_service_codes
45
+ @structures_serializer_class = structures_serializer_class
28
46
  @packages_serializer_class = packages_serializer_class
29
- super(**kwargs.merge(package_options_class: PackageOptions))
47
+ super(**kwargs)
30
48
  end
31
49
  end
32
50
  end
@@ -3,11 +3,16 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class ShipEngineLTL
6
+ # Serializes packages for the rate quote API request.
7
+ # @deprecated Use {SerializeStructures} instead.
6
8
  class SerializePackages
7
9
  class << self
8
- # @param [Array<Physical::Package>] packages
9
- # @param [FriendlyShipping::Services::ShipEngineLTL::QuoteOptions] options
10
+ # @param packages [Array<Physical::Package>]
11
+ # @param options [QuoteOptions]
12
+ # @return [Array<Hash>]
10
13
  def call(packages:, options:)
14
+ warn "[DEPRECATION] `SerializePackages` is deprecated. Please use `SerializeStructures` instead."
15
+
11
16
  packages.flat_map do |package|
12
17
  package_options = options.options_for_package(package)
13
18
  package.items.map do |item|
@@ -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
@@ -1,43 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/monads'
4
- require 'friendly_shipping/http_client'
5
- require 'friendly_shipping/services/ship_engine_ltl/bad_request_handler'
6
- require 'friendly_shipping/services/ship_engine_ltl/parse_carrier_response'
7
- require 'friendly_shipping/services/ship_engine_ltl/parse_quote_response'
8
- require 'friendly_shipping/services/ship_engine_ltl/serialize_packages'
9
- require 'friendly_shipping/services/ship_engine_ltl/serialize_quote_request'
10
- require 'friendly_shipping/services/ship_engine_ltl/quote_options'
11
- require 'friendly_shipping/services/ship_engine_ltl/package_options'
12
- require 'friendly_shipping/services/ship_engine_ltl/item_options'
3
+ require "json"
13
4
 
14
5
  module FriendlyShipping
15
6
  module Services
7
+ # API service class for ShipEngine LTL, a shipping API supporting Freight carriers.
8
+ # @see https://www.shipengine.com/docs/ltl/ ShipEngine LTL API docs
16
9
  class ShipEngineLTL
17
10
  include Dry::Monads::Result::Mixin
18
11
 
12
+ # The API base URL.
19
13
  API_BASE = "https://api.shipengine.com/v-beta/ltl/"
14
+
15
+ # The API paths. Used when constructing endpoint URLs.
20
16
  API_PATHS = {
21
17
  connections: "connections",
22
18
  carriers: "carriers",
23
19
  quotes: "quotes"
24
20
  }.freeze
25
21
 
26
- def initialize(
27
- token:,
28
- test: true,
29
- client: FriendlyShipping::HttpClient.new(
30
- error_handler: FriendlyShipping::Services::ShipEngineLTL::BadRequestHandler
31
- )
32
- )
22
+ # @param token [String] the API token
23
+ # @param test [Boolean] whether to use test API endpoints
24
+ # @param client [HttpClient] optional custom HTTP client to use for requests
25
+ def initialize(token:, test: true, client: nil)
33
26
  @token = token
34
27
  @test = test
35
- @client = client
28
+
29
+ error_handler = ApiErrorHandler.new(api_error_class: ShipEngineLTL::ApiError)
30
+ @client = client || HttpClient.new(error_handler: error_handler)
36
31
  end
37
32
 
38
- # Get configured LTL carriers from ShipEngine
33
+ # Get configured LTL carriers.
39
34
  #
40
- # @return [Result<ApiResult<Array<Carrier>>>] LTL carriers configured in your account
35
+ # @param debug [Boolean] whether to attach debugging information to the response
36
+ # @return [Success<ApiResult<Array<Carrier>>>, Failure<ApiResult<Array<String>>>] On success,
37
+ # LTL carriers configured in your account. On failure, a list of error messages.
41
38
  def carriers(debug: false)
42
39
  request = FriendlyShipping::Request.new(
43
40
  url: API_BASE + API_PATHS[:carriers],
@@ -49,12 +46,13 @@ module FriendlyShipping
49
46
  end
50
47
  end
51
48
 
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
49
+ # Connect an LTL carrier.
56
50
  #
57
- # @return [Result<ApiResult<Hash>>] The unique carrier ID assigned by ShipEngine
51
+ # @param credentials [Hash] the carrier's connection information
52
+ # @param scac [String] Standard Carrier Alpha Code
53
+ # @see https://nmfta.org/scac/ SCAC
54
+ # @param debug [Boolean] whether to attach debugging information to the response
55
+ # @return [Success<ApiResult<Hash>>, Failure<ApiResult<String>>] the carrier or error message
58
56
  def connect_carrier(credentials, scac, debug: false)
59
57
  request = FriendlyShipping::Request.new(
60
58
  url: API_BASE + API_PATHS[:connections] + "/#{scac}",
@@ -73,13 +71,14 @@ module FriendlyShipping
73
71
  end
74
72
  end
75
73
 
76
- # Update an existing LTL carrier in ShipEngine
74
+ # Update an existing LTL carrier.
77
75
  #
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
76
+ # @param credentials [Hash] the carrier's connection information
77
+ # @param scac [String] Standard Carrier Alpha Code
78
+ # @see https://nmfta.org/scac/ SCAC
79
+ # @param carrier_id [String] the ID for the carrier you want to update
80
+ # @param debug [Boolean] whether to attach debugging information to the response
81
+ # @return [Success<ApiResult<Hash>>, Failure<ApiResult<String>>] the carrier or error message
83
82
  def update_carrier(credentials, scac, carrier_id, debug: false)
84
83
  request = FriendlyShipping::Request.new(
85
84
  url: API_BASE + API_PATHS[:connections] + "/#{scac}/#{carrier_id}",
@@ -98,13 +97,13 @@ module FriendlyShipping
98
97
  end
99
98
  end
100
99
 
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
100
+ # Request an LTL price quote.
106
101
  #
107
- # @return [Result<ApiResult<Hash>>] The price quote from ShipEngine
102
+ # @param carrier_id [String] the carrier ID
103
+ # @param shipment [Physical::Shipment] the shipment to quote
104
+ # @param options [QuoteOptions] the options for the quote
105
+ # @param debug [Boolean] whether to attach debugging information to the response
106
+ # @return [Success<ApiResult<Hash>>, Failure<ApiResult<String>>] the quote or error message
108
107
  def request_quote(carrier_id, shipment, options, debug: false)
109
108
  request = FriendlyShipping::Request.new(
110
109
  url: API_BASE + API_PATHS[:quotes] + "/#{carrier_id}",
@@ -120,12 +119,21 @@ module FriendlyShipping
120
119
 
121
120
  private
122
121
 
123
- attr_reader :token, :test, :client
122
+ # @return [String] the API token
123
+ attr_reader :token
124
+
125
+ # @return [Boolean] whether to use test API endpoints
126
+ attr_reader :test
127
+
128
+ # @return [HttpClient] the HTTP client to use for requests
129
+ attr_reader :client
124
130
 
131
+ # Returns the content type and API key as a headers hash.
132
+ # @return [Hash]
125
133
  def request_headers
126
134
  {
127
135
  content_type: :json,
128
- "api-key": token
136
+ 'api-key': token
129
137
  }
130
138
  end
131
139
  end
@@ -0,0 +1,28 @@
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 < FriendlyShipping::AccessToken
11
+ # @return [String] the token's type
12
+ attr_reader :token_type
13
+
14
+ # @return [Integer] the token's extended expiration
15
+ attr_reader :ext_expires_in
16
+
17
+ # @param token_type [String] the token's type (typically "Bearer")
18
+ # @param ext_expires_in [Integer] the token's extended expiration (only applicable during a service outage)
19
+ # @see https://github.com/AzureAD/azure-activedirectory-library-for-android/issues/675 Service Outage Resiliency Support
20
+ def initialize(token_type:, ext_expires_in:, **other_kwargs)
21
+ @token_type = token_type
22
+ @ext_expires_in = ext_expires_in
23
+ super(**other_kwargs)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class TForceFreight
6
+ # Raised when an API error is returned.
7
+ class ApiError < FriendlyShipping::ApiError
8
+ # @param cause [RestClient::Exception]
9
+ def initialize(cause)
10
+ super(cause, parse_message(cause))
11
+ end
12
+
13
+ private
14
+
15
+ # @param error [RestClient::Exception]
16
+ # @return [String]
17
+ def parse_message(error)
18
+ return error.message unless error.response
19
+
20
+ parsed_json = JSON.parse(error.response.body)
21
+ if parsed_json['summary'].present?
22
+ status = parsed_json.dig("summary", "responseStatus", "code")
23
+ message = parsed_json.dig("summary", "responseStatus", "message").presence ||
24
+ parsed_json.dig("summary", "responseStatus", "description")
25
+ elsif parsed_json['responseStatus'].present?
26
+ status = parsed_json.dig("responseStatus", "code")
27
+ message = parsed_json.dig("responseStatus", "description")
28
+ else
29
+ status = parsed_json['statusCode']
30
+ message = parsed_json['message'].presence ||
31
+ parsed_json['description']
32
+ end
33
+ [status, message].compact.join(": ")
34
+ rescue JSON::ParserError, KeyError => _e
35
+ nil
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end