friendly_shipping 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +7 -0
  4. data/.env.test +4 -0
  5. data/.env.test.local.template +6 -0
  6. data/.gitignore +1 -0
  7. data/.rubocop-relaxed.yml +7 -23
  8. data/.rubocop.yml +11 -2
  9. data/.rubocop_todo.yml +21 -0
  10. data/.yardopts +6 -0
  11. data/CHANGELOG.md +82 -0
  12. data/Gemfile +17 -0
  13. data/friendly_shipping.gemspec +4 -13
  14. data/lib/friendly_shipping/api_error.rb +6 -4
  15. data/lib/friendly_shipping/api_error_handler.rb +10 -6
  16. data/lib/friendly_shipping/api_failure.rb +4 -0
  17. data/lib/friendly_shipping/api_result.rb +16 -4
  18. data/lib/friendly_shipping/carrier.rb +28 -8
  19. data/lib/friendly_shipping/http_client.rb +25 -2
  20. data/lib/friendly_shipping/inflections.rb +10 -0
  21. data/lib/friendly_shipping/item_options.rb +3 -0
  22. data/lib/friendly_shipping/label.rb +41 -20
  23. data/lib/friendly_shipping/package_options.rb +21 -2
  24. data/lib/friendly_shipping/rate.rb +50 -15
  25. data/lib/friendly_shipping/request.rb +23 -7
  26. data/lib/friendly_shipping/response.rb +21 -6
  27. data/lib/friendly_shipping/services/rl/api_error.rb +33 -0
  28. data/lib/friendly_shipping/services/rl/bol_options.rb +107 -0
  29. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  30. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  31. data/lib/friendly_shipping/services/rl/item_options.rb +38 -0
  32. data/lib/friendly_shipping/services/rl/package_options.rb +40 -0
  33. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +46 -0
  34. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +50 -0
  35. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +47 -0
  37. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +66 -0
  38. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +76 -0
  39. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +86 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  41. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  42. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  43. data/lib/friendly_shipping/services/rl/serialize_location.rb +46 -0
  44. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  45. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  46. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  47. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  48. data/lib/friendly_shipping/services/rl/shipment_options.rb +50 -0
  49. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  50. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  51. data/lib/friendly_shipping/services/rl.rb +204 -0
  52. data/lib/friendly_shipping/services/ship_engine/api_error.rb +33 -0
  53. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  54. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  55. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -5
  56. data/lib/friendly_shipping/services/ship_engine/label_options.rb +31 -14
  57. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -11
  58. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +77 -0
  59. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -0
  60. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -0
  61. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +26 -6
  62. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +101 -0
  63. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +4 -0
  64. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -4
  65. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +28 -0
  66. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +61 -0
  67. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +20 -0
  68. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  70. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  71. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  72. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  73. data/lib/friendly_shipping/services/ship_engine.rb +94 -21
  74. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  75. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +50 -0
  76. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +50 -0
  77. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +53 -0
  78. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +82 -0
  79. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +52 -0
  80. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +46 -0
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +143 -0
  82. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  83. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  85. data/lib/friendly_shipping/services/ship_engine_ltl.rb +153 -0
  86. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +43 -0
  87. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +42 -0
  88. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  89. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  90. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +165 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +113 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +65 -0
  97. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  98. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  99. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +121 -0
  100. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +94 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +58 -0
  103. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +31 -0
  104. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  105. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +12 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +162 -0
  107. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +12 -0
  108. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +49 -0
  111. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  112. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  113. data/lib/friendly_shipping/services/tforce_freight.rb +202 -0
  114. data/lib/friendly_shipping/services/ups/label_options.rb +14 -2
  115. data/lib/friendly_shipping/services/ups/label_package_options.rb +8 -4
  116. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  117. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +12 -0
  118. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  119. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -1
  120. data/lib/friendly_shipping/services/ups/serialize_package_node.rb +11 -1
  121. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -1
  122. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  123. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +8 -4
  124. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  125. data/lib/friendly_shipping/services/ups.rb +1 -1
  126. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -5
  127. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  128. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +3 -20
  129. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +2 -20
  130. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  131. data/lib/friendly_shipping/services/ups_freight/generate_location_hash.rb +10 -6
  132. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  133. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  134. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -2
  135. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -1
  136. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  137. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +31 -21
  138. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -8
  139. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +46 -0
  140. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  141. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +49 -0
  142. data/lib/friendly_shipping/services/ups_freight.rb +3 -1
  143. data/lib/friendly_shipping/services/ups_json/access_token.rb +29 -0
  144. data/lib/friendly_shipping/services/ups_json/api_error.rb +29 -0
  145. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  146. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  147. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +211 -0
  148. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  149. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +86 -0
  150. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  151. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  152. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  153. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +77 -0
  154. data/lib/friendly_shipping/services/ups_json/label_options.rb +177 -0
  155. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +51 -0
  156. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  157. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +44 -0
  158. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  159. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  160. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  161. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +105 -0
  162. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  163. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  164. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +22 -0
  165. data/lib/friendly_shipping/services/ups_json/rates_options.rb +113 -0
  166. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  167. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  168. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  169. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  170. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  171. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  172. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +8 -7
  173. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +12 -8
  174. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -1
  175. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -1
  176. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -2
  177. data/lib/friendly_shipping/services/usps/shipping_methods.rb +8 -5
  178. data/lib/friendly_shipping/services/usps.rb +1 -1
  179. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +5 -4
  180. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -1
  181. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +3 -3
  182. data/lib/friendly_shipping/services/usps_international.rb +1 -1
  183. data/lib/friendly_shipping/services/usps_ship/access_token.rb +37 -0
  184. data/lib/friendly_shipping/services/usps_ship/api_error.rb +29 -0
  185. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +53 -0
  186. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  187. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +80 -0
  188. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  189. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +124 -0
  190. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +55 -0
  191. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  192. data/lib/friendly_shipping/services/usps_ship/timing_options.rb +9 -0
  193. data/lib/friendly_shipping/services/usps_ship.rb +199 -0
  194. data/lib/friendly_shipping/shipment_options.rb +13 -1
  195. data/lib/friendly_shipping/shipping_method.rb +38 -11
  196. data/lib/friendly_shipping/structure_options.rb +38 -0
  197. data/lib/friendly_shipping/timing.rb +42 -7
  198. data/lib/friendly_shipping/version.rb +1 -1
  199. data/lib/friendly_shipping.rb +7 -0
  200. metadata +149 -172
  201. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  202. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngineLTL
6
+ # Serializes a shipment and options for the rate quote API request.
7
+ class SerializeQuoteRequest
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment to serialize
10
+ # @param options [QuoteOptions] the options to serialize
11
+ # @return [Hash] the serialized request
12
+ def call(shipment:, options:)
13
+ {
14
+ shipment: {
15
+ service_code: options.service_code,
16
+ pickup_date: options.pickup_date.strftime('%Y-%m-%d'),
17
+ packages: serialize_packages(shipment, options),
18
+ options: serialize_options(options),
19
+ ship_from: serialize_ship_address(shipment.origin),
20
+ ship_to: serialize_ship_address(shipment.destination),
21
+ bill_to: serialize_bill_address(shipment.origin),
22
+ requested_by: serialize_requested_by(shipment.origin),
23
+ }.compact,
24
+ shipment_measurements: serialize_shipment_measurements(shipment, options)
25
+ }
26
+ end
27
+
28
+ private
29
+
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>]
44
+ def serialize_options(options)
45
+ options.accessorial_service_codes.map do |code|
46
+ { code: code }
47
+ end
48
+ end
49
+
50
+ # @param location [Physical::Location]
51
+ # @return [Hash]
52
+ def serialize_ship_address(location)
53
+ {
54
+ account: location.properties.with_indifferent_access['account_number'],
55
+ address: serialize_address(location),
56
+ contact: serialize_contact(location)
57
+ }.compact
58
+ end
59
+
60
+ # @param location [Physical::Location]
61
+ # @return [Hash]
62
+ def serialize_bill_address(location)
63
+ {
64
+ type: "shipper",
65
+ payment_terms: "prepaid",
66
+ account: location.properties.with_indifferent_access['account_number'],
67
+ address: serialize_address(location),
68
+ contact: serialize_contact(location)
69
+ }.compact
70
+ end
71
+
72
+ # @param location [Physical::Location]
73
+ # @return [Hash]
74
+ def serialize_address(location)
75
+ {
76
+ company_name: location.company_name,
77
+ address_line1: location.address1,
78
+ city_locality: location.city,
79
+ state_province: location.region.code,
80
+ postal_code: location.zip,
81
+ country_code: location.country.code
82
+ }.compact
83
+ end
84
+
85
+ # @param location [Physical::Location]
86
+ # @return [Hash]
87
+ def serialize_contact(location)
88
+ {
89
+ name: location.name,
90
+ phone_number: location.phone,
91
+ email: location.email
92
+ }.compact
93
+ end
94
+
95
+ # @param location [Physical::Location]
96
+ # @return [Hash]
97
+ def serialize_requested_by(location)
98
+ {
99
+ company_name: location.company_name,
100
+ contact: serialize_contact(location)
101
+ }.compact
102
+ end
103
+
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
120
+ {
121
+ total_linear_length: {
122
+ value: total_length.convert_to(:inches).value.ceil,
123
+ unit: "inches"
124
+ },
125
+ total_width: {
126
+ value: max_width.convert_to(:inches).value.ceil,
127
+ unit: "inches"
128
+ },
129
+ total_height: {
130
+ value: max_height.convert_to(:inches).value.ceil,
131
+ unit: "inches"
132
+ },
133
+ total_weight: {
134
+ value: total_weight.convert_to(:pounds).value.ceil,
135
+ unit: "pounds"
136
+ }
137
+ }
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -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
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads'
4
+ require 'friendly_shipping/http_client'
5
+ require 'friendly_shipping/services/ship_engine_ltl/api_error'
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/serialize_structures'
11
+ require 'friendly_shipping/services/ship_engine_ltl/shipment_options'
12
+ require 'friendly_shipping/services/ship_engine_ltl/quote_options'
13
+ require 'friendly_shipping/services/ship_engine_ltl/structure_options'
14
+ require 'friendly_shipping/services/ship_engine_ltl/package_options'
15
+ require 'friendly_shipping/services/ship_engine_ltl/item_options'
16
+
17
+ module FriendlyShipping
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
21
+ class ShipEngineLTL
22
+ include Dry::Monads::Result::Mixin
23
+
24
+ # The API base URL.
25
+ API_BASE = "https://api.shipengine.com/v-beta/ltl/"
26
+
27
+ # The API paths. Used when constructing endpoint URLs.
28
+ API_PATHS = {
29
+ connections: "connections",
30
+ carriers: "carriers",
31
+ quotes: "quotes"
32
+ }.freeze
33
+
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)
38
+ @token = token
39
+ @test = test
40
+
41
+ error_handler = ApiErrorHandler.new(api_error_class: ShipEngineLTL::ApiError)
42
+ @client = client || HttpClient.new(error_handler: error_handler)
43
+ end
44
+
45
+ # Get configured LTL carriers.
46
+ #
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.
50
+ def carriers(debug: false)
51
+ request = FriendlyShipping::Request.new(
52
+ url: API_BASE + API_PATHS[:carriers],
53
+ headers: request_headers,
54
+ debug: debug
55
+ )
56
+ client.get(request).bind do |response|
57
+ ParseCarrierResponse.call(request: request, response: response)
58
+ end
59
+ end
60
+
61
+ # Connect an LTL carrier.
62
+ #
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
68
+ def connect_carrier(credentials, scac, debug: false)
69
+ request = FriendlyShipping::Request.new(
70
+ url: API_BASE + API_PATHS[:connections] + "/#{scac}",
71
+ body: { credentials: credentials }.to_json,
72
+ headers: request_headers,
73
+ debug: debug
74
+ )
75
+ client.post(request).bind do |response|
76
+ Success(
77
+ ApiResult.new(
78
+ JSON.parse(response.body),
79
+ original_request: request,
80
+ original_response: response
81
+ )
82
+ )
83
+ end
84
+ end
85
+
86
+ # Update an existing LTL carrier.
87
+ #
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
94
+ def update_carrier(credentials, scac, carrier_id, debug: false)
95
+ request = FriendlyShipping::Request.new(
96
+ url: API_BASE + API_PATHS[:connections] + "/#{scac}/#{carrier_id}",
97
+ body: { credentials: credentials }.to_json,
98
+ headers: request_headers,
99
+ debug: debug
100
+ )
101
+ client.put(request).bind do |response|
102
+ Success(
103
+ ApiResult.new(
104
+ JSON.parse(response.body),
105
+ original_request: request,
106
+ original_response: response
107
+ )
108
+ )
109
+ end
110
+ end
111
+
112
+ # Request an LTL price quote.
113
+ #
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
119
+ def request_quote(carrier_id, shipment, options, debug: false)
120
+ request = FriendlyShipping::Request.new(
121
+ url: API_BASE + API_PATHS[:quotes] + "/#{carrier_id}",
122
+ http_method: "POST",
123
+ body: SerializeQuoteRequest.call(shipment: shipment, options: options).to_json,
124
+ headers: request_headers,
125
+ debug: debug
126
+ )
127
+ client.post(request).bind do |response|
128
+ ParseQuoteResponse.call(request: request, response: response)
129
+ end
130
+ end
131
+
132
+ private
133
+
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
142
+
143
+ # Returns the content type and API key as a headers hash.
144
+ # @return [Hash]
145
+ def request_headers
146
+ {
147
+ content_type: :json,
148
+ 'api-key': token
149
+ }
150
+ end
151
+ end
152
+ end
153
+ 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