friendly_shipping 0.8.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +3 -0
  4. data/.env.test.local.template +6 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop-relaxed.yml +7 -23
  7. data/.rubocop.yml +17 -2
  8. data/.rubocop_todo.yml +21 -0
  9. data/.yardopts +6 -0
  10. data/CHANGELOG.md +71 -0
  11. data/Gemfile +17 -0
  12. data/README.md +1 -1
  13. data/friendly_shipping.gemspec +8 -17
  14. data/lib/friendly_shipping/access_token.rb +24 -0
  15. data/lib/friendly_shipping/api_error.rb +8 -4
  16. data/lib/friendly_shipping/api_error_handler.rb +11 -9
  17. data/lib/friendly_shipping/api_failure.rb +2 -7
  18. data/lib/friendly_shipping/api_result.rb +26 -4
  19. data/lib/friendly_shipping/carrier.rb +28 -8
  20. data/lib/friendly_shipping/http_client.rb +25 -8
  21. data/lib/friendly_shipping/inflections.rb +10 -0
  22. data/lib/friendly_shipping/item_options.rb +3 -0
  23. data/lib/friendly_shipping/label.rb +41 -22
  24. data/lib/friendly_shipping/package_options.rb +21 -2
  25. data/lib/friendly_shipping/rate.rb +50 -15
  26. data/lib/friendly_shipping/request.rb +23 -7
  27. data/lib/friendly_shipping/response.rb +21 -6
  28. data/lib/friendly_shipping/services/rl/api_error.rb +31 -0
  29. data/lib/friendly_shipping/services/rl/bol_options.rb +103 -0
  30. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  31. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  32. data/lib/friendly_shipping/services/rl/item_options.rb +36 -0
  33. data/lib/friendly_shipping/services/rl/package_options.rb +38 -0
  34. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +43 -0
  35. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +44 -0
  37. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +44 -0
  38. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +93 -0
  39. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +73 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +82 -0
  41. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  42. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  43. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  44. data/lib/friendly_shipping/services/rl/serialize_location.rb +53 -0
  45. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  46. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  47. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  48. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  49. data/lib/friendly_shipping/services/rl/shipment_options.rb +48 -0
  50. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  51. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  52. data/lib/friendly_shipping/services/rl.rb +187 -0
  53. data/lib/friendly_shipping/services/ship_engine/api_error.rb +31 -0
  54. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  55. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  56. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -7
  57. data/lib/friendly_shipping/services/ship_engine/label_options.rb +30 -17
  58. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -14
  59. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +75 -0
  60. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -2
  61. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -2
  62. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +27 -9
  63. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +99 -0
  64. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +5 -1
  65. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -6
  66. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +26 -0
  67. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +59 -0
  68. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +18 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  70. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  71. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  72. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  73. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  74. data/lib/friendly_shipping/services/ship_engine.rb +89 -33
  75. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  76. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +24 -5
  77. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +39 -4
  78. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +4 -2
  79. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +10 -2
  80. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +29 -11
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +7 -2
  82. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +50 -16
  83. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  85. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  86. data/lib/friendly_shipping/services/ship_engine_ltl.rb +47 -39
  87. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +28 -0
  88. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +40 -0
  89. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  90. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +153 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +111 -0
  97. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +63 -0
  98. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  99. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  100. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +119 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +90 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  103. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +50 -0
  104. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +29 -0
  105. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +10 -0
  107. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +159 -0
  108. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +10 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  111. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +47 -0
  112. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  113. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  114. data/lib/friendly_shipping/services/tforce_freight.rb +176 -0
  115. data/lib/friendly_shipping/services/ups/label_item_options.rb +0 -2
  116. data/lib/friendly_shipping/services/ups/label_options.rb +14 -5
  117. data/lib/friendly_shipping/services/ups/label_package_options.rb +1 -3
  118. data/lib/friendly_shipping/services/ups/parse_address_validation_response.rb +1 -1
  119. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  120. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +11 -3
  121. data/lib/friendly_shipping/services/ups/parse_shipment_accept_response.rb +0 -3
  122. data/lib/friendly_shipping/services/ups/parse_shipment_confirm_response.rb +0 -2
  123. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  124. data/lib/friendly_shipping/services/ups/parse_void_shipment_response.rb +0 -2
  125. data/lib/friendly_shipping/services/ups/parse_xml_response.rb +1 -1
  126. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -3
  127. data/lib/friendly_shipping/services/ups/rate_estimate_package_options.rb +0 -2
  128. data/lib/friendly_shipping/services/ups/serialize_access_request.rb +0 -2
  129. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -5
  130. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  131. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +2 -4
  132. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  133. data/lib/friendly_shipping/services/ups.rb +2 -24
  134. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -7
  135. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  136. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +2 -23
  137. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +1 -27
  138. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  139. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  140. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  141. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -5
  142. data/lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb +0 -4
  143. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -3
  144. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  145. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +30 -24
  146. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -10
  147. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +44 -0
  148. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  149. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +47 -0
  150. data/lib/friendly_shipping/services/ups_freight.rb +2 -20
  151. data/lib/friendly_shipping/services/ups_json/access_token.rb +20 -0
  152. data/lib/friendly_shipping/services/ups_json/api_error.rb +27 -0
  153. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  154. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  155. data/lib/friendly_shipping/services/ups_json/generate_city_state_lookup_payload.rb +23 -0
  156. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +208 -0
  157. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  158. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +84 -0
  159. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  160. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  161. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  162. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +75 -0
  163. data/lib/friendly_shipping/services/ups_json/label_options.rb +174 -0
  164. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +49 -0
  165. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  166. data/lib/friendly_shipping/services/ups_json/parse_city_state_lookup_response.rb +44 -0
  167. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +51 -0
  168. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  169. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  170. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  171. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +107 -0
  172. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  173. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  174. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +20 -0
  175. data/lib/friendly_shipping/services/ups_json/rates_options.rb +111 -0
  176. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  177. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  178. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  179. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  180. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  181. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  182. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +6 -6
  183. data/lib/friendly_shipping/services/usps/parse_rate_response.rb +0 -4
  184. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +6 -9
  185. data/lib/friendly_shipping/services/usps/parse_xml_response.rb +1 -1
  186. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -3
  187. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -3
  188. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -4
  189. data/lib/friendly_shipping/services/usps/shipping_methods.rb +4 -3
  190. data/lib/friendly_shipping/services/usps.rb +2 -13
  191. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +3 -3
  192. data/lib/friendly_shipping/services/usps_international/parse_rate_response.rb +0 -3
  193. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -3
  194. data/lib/friendly_shipping/services/usps_international/rate_estimate_package_options.rb +0 -2
  195. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +1 -3
  196. data/lib/friendly_shipping/services/usps_international.rb +2 -6
  197. data/lib/friendly_shipping/services/usps_ship/access_token.rb +22 -0
  198. data/lib/friendly_shipping/services/usps_ship/api_error.rb +27 -0
  199. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +55 -0
  200. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  201. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +82 -0
  202. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  203. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +122 -0
  204. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +53 -0
  205. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  206. data/lib/friendly_shipping/services/{ship_engine_ltl/bad_request.rb → usps_ship/timing_options.rb} +2 -2
  207. data/lib/friendly_shipping/services/usps_ship.rb +185 -0
  208. data/lib/friendly_shipping/shipment_options.rb +13 -1
  209. data/lib/friendly_shipping/shipping_method.rb +38 -11
  210. data/lib/friendly_shipping/structure_options.rb +38 -0
  211. data/lib/friendly_shipping/timing.rb +42 -7
  212. data/lib/friendly_shipping/version.rb +1 -1
  213. data/lib/friendly_shipping.rb +20 -15
  214. metadata +143 -174
  215. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  216. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
  217. data/lib/friendly_shipping/services/ship_engine_ltl/bad_request_handler.rb +0 -33
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ class ParseInvoiceResponse
7
+ extend Dry::Monads::Result::Mixin
8
+
9
+ class << self
10
+ # @param [FriendlyShipping::Request] request
11
+ # @param [FriendlyShipping::Response] response
12
+ # @return [Result<ApiResult<ShipmentDocument>>]
13
+ def call(request:, response:)
14
+ parsed_json = JSON.parse(response.body)
15
+ data = (parsed_json['Documents'].find { _1["Type"] == "Invoice" } || {})["Data"]
16
+
17
+ document = ShipmentDocument.new(binary: data, format: :pdf, document_type: :invoice)
18
+ if document.valid?
19
+ Success(
20
+ ApiResult.new(
21
+ document,
22
+ original_request: request,
23
+ original_response: response
24
+ )
25
+ )
26
+ else
27
+ errors = if parsed_json["Messages"].present?
28
+ parsed_json["Messages"]
29
+ else
30
+ parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }]).
31
+ map { _1['ErrorMessage'] }
32
+ end
33
+
34
+ Failure(
35
+ ApiResult.new(
36
+ errors,
37
+ original_request: request,
38
+ original_response: response
39
+ )
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Parses the response from the R+L API when printing a Bill of Lading (BOL).
7
+ class ParsePrintBOLResponse
8
+ extend Dry::Monads::Result::Mixin
9
+
10
+ class << self
11
+ # @param request [Request] the request to attach to the API result
12
+ # @param response [Response] the response to parse
13
+ # @return [Result<ApiResult<ShipmentDocument>>] shipment document containing the BOL
14
+ def call(request:, response:)
15
+ parsed_json = JSON.parse(response.body)
16
+ bol_document = ShipmentDocument.new(
17
+ format: :pdf,
18
+ document_type: :rl_bol,
19
+ binary: Base64.decode64(parsed_json['BolDocument'])
20
+ )
21
+ if bol_document.valid?
22
+ Success(
23
+ ApiResult.new(
24
+ bol_document,
25
+ original_request: request,
26
+ original_response: response
27
+ )
28
+ )
29
+ else
30
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
31
+ Failure(
32
+ ApiResult.new(
33
+ errors.map { |e| e['ErrorMessage'] },
34
+ original_request: request,
35
+ original_response: response
36
+ )
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Parses the response from the R+L API when printing shipping labels.
7
+ class ParsePrintShippingLabelsResponse
8
+ extend Dry::Monads::Result::Mixin
9
+
10
+ class << self
11
+ # @param request [Request] the request to attach to the API result
12
+ # @param response [Response] the response to parse
13
+ # @return [Result<ApiResult<ShipmentDocument>>] shipping document containing the labels
14
+ def call(request:, response:)
15
+ parsed_json = JSON.parse(response.body)
16
+ label_doc = ShipmentDocument.new(
17
+ format: :pdf,
18
+ document_type: :label,
19
+ binary: Base64.decode64(parsed_json['ShippingLabelsFile'])
20
+ )
21
+ if label_doc.valid?
22
+ Success(
23
+ ApiResult.new(
24
+ label_doc,
25
+ original_request: request,
26
+ original_response: response
27
+ )
28
+ )
29
+ else
30
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
31
+ Failure(
32
+ ApiResult.new(
33
+ errors.map { |e| e['ErrorMessage'] },
34
+ original_request: request,
35
+ original_response: response
36
+ )
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Parses the response from the R+L API when getting rate quotes.
7
+ class ParseRateQuoteResponse
8
+ extend Dry::Monads::Result::Mixin
9
+
10
+ class << self
11
+ # @param request [Request] the request to attach to the API result
12
+ # @param response [Response] the response to parse
13
+ # @return [Success<ApiResult>, Failure<ApiResult>] the parsed rates
14
+ def call(request:, response:)
15
+ parsed_json = JSON.parse(response.body)
16
+ rates = build_rates(parsed_json)
17
+ if rates.any?
18
+ Success(
19
+ ApiResult.new(
20
+ rates,
21
+ original_request: request,
22
+ original_response: response
23
+ )
24
+ )
25
+ else
26
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
27
+ Failure(
28
+ ApiResult.new(
29
+ errors.map { |e| e['ErrorMessage'] },
30
+ original_request: request,
31
+ original_response: response
32
+ )
33
+ )
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ # The currency to use when parsing the rate quotes.
40
+ CURRENCY = Money::Currency.new('USD').freeze
41
+
42
+ # Builds {Rate} instances from the parsed JSON.
43
+ #
44
+ # @param parsed_json [String] the parsed JSON
45
+ # @return [Array<Rate>] the parsed rates
46
+ def build_rates(parsed_json)
47
+ service_levels = parsed_json.dig('RateQuote', 'ServiceLevels')
48
+ return [] unless service_levels
49
+
50
+ service_levels.map do |service_level|
51
+ shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_level['Code'] }
52
+ total = Money.new(service_level['NetCharge'].delete('$,.'), CURRENCY)
53
+
54
+ charges = parsed_json.dig('RateQuote', 'Charges')
55
+ cost_breakdown = build_cost_breakdown(charges, service_level)
56
+
57
+ FriendlyShipping::Rate.new(
58
+ shipping_method: shipping_method,
59
+ amounts: { total: total },
60
+ data: {
61
+ cost_breakdown: cost_breakdown
62
+ }
63
+ )
64
+ end
65
+ end
66
+
67
+ # @param [Array<Hash>] charges
68
+ # @param [Hash] service_level
69
+ # @return [Array<Hash>]
70
+ def build_cost_breakdown(charges, service_level)
71
+ result = charges.map do |charge|
72
+ {
73
+ "Type" => charge['Type'].presence,
74
+ "Description" => charge['Title'],
75
+ "Weight" => charge['Weight'].presence,
76
+ "Rate" => charge['Rate'].presence,
77
+ "Amount" => charge['Amount'].presence
78
+ }.compact
79
+ end
80
+ if service_level['Code'] != 'STD'
81
+ result << {
82
+ "Type" => service_level['Code'],
83
+ "Description" => service_level['Name'],
84
+ "Amount" => service_level['Charge']
85
+ }
86
+ end
87
+ result
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Parses the response from the R+L API when getting transit times.
7
+ class ParseTransitTimesResponse
8
+ extend Dry::Monads::Result::Mixin
9
+
10
+ class << self
11
+ # @param request [Request] the request to attach to the API result
12
+ # @param response [Response] the response to parse
13
+ # @return [Success<ApiResult>, Failure<ApiResult>] the parsed timings
14
+ def call(request:, response:)
15
+ parsed_json = JSON.parse(response.body)
16
+ timings = build_timings(parsed_json)
17
+ if timings.any?
18
+ Success(
19
+ ApiResult.new(
20
+ timings,
21
+ original_request: request,
22
+ original_response: response
23
+ )
24
+ )
25
+ else
26
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
27
+ Failure(
28
+ ApiResult.new(
29
+ errors.map { |e| e['ErrorMessage'] },
30
+ original_request: request,
31
+ original_response: response
32
+ )
33
+ )
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ # Builds {Timing} instances from the parsed JSON.
40
+ #
41
+ # @param parsed_json [String] the parsed JSON
42
+ # @return [Array<Timing>] the parsed timings
43
+ def build_timings(parsed_json)
44
+ destinations = parsed_json['Destinations']
45
+ return [] unless destinations
46
+
47
+ pickup_date = parsed_json['PickupDate']
48
+ pickup = Time.strptime(pickup_date, '%m/%d/%Y')
49
+
50
+ destination = destinations.first
51
+
52
+ delivery_date = destination['DeliveryDate']
53
+ delivery = Time.strptime(delivery_date, '%m/%d/%Y')
54
+
55
+ days_in_transit = destination['ServiceDays']
56
+
57
+ [
58
+ FriendlyShipping::Timing.new(
59
+ shipping_method: SHIPPING_METHODS.first,
60
+ pickup: pickup,
61
+ delivery: delivery,
62
+ guaranteed: false,
63
+ data: {
64
+ days_in_transit: days_in_transit
65
+ }
66
+ )
67
+ ]
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Rate quote options for serializing R+L API requests.
7
+ class RateQuoteOptions < ShipmentOptions
8
+ # @return [Time] the pickup date
9
+ attr_reader :pickup_date
10
+
11
+ # @return [Numeric] the declared value of this shipment
12
+ attr_reader :declared_value
13
+
14
+ # @return [Array<String>] additional service codes
15
+ attr_reader :additional_service_codes
16
+
17
+ # @return [Callable] the serializer for this shipment's structures
18
+ attr_reader :structures_serializer
19
+
20
+ # @return [Callable] the serializer for this shipment's packages
21
+ # @deprecated Use {#structures_serializer} instead.
22
+ attr_reader :packages_serializer
23
+
24
+ # @param pickup_date [Time] the pickup date
25
+ # @param declared_value [Numeric] the declared value of this shipment
26
+ # @param additional_service_codes [Array<String>] additional service codes
27
+ # @param structures_serializer [Callable] a callable that takes structures
28
+ # and an options object to create an Array of item hashes per the R+L Carriers docs
29
+ # @param packages_serializer [Callable] a callable that takes packages
30
+ # and an options object to create an Array of item hashes per the R+L Carriers docs (DEPRECATED: use ``structures_serializer`` instead)
31
+ # @param kwargs [Hash]
32
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
33
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
34
+ def initialize(
35
+ pickup_date:,
36
+ declared_value: nil,
37
+ additional_service_codes: [],
38
+ structures_serializer: RateQuoteStructuresSerializer,
39
+ packages_serializer: RateQuotePackagesSerializer,
40
+ **kwargs
41
+ )
42
+ @pickup_date = pickup_date
43
+ @declared_value = declared_value
44
+ @additional_service_codes = additional_service_codes
45
+ @structures_serializer = structures_serializer
46
+ @packages_serializer = packages_serializer
47
+ validate_additional_service_codes!
48
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
49
+ end
50
+
51
+ # Optional service codes that can be used for R+L shipments.
52
+ ADDITIONAL_SERVICE_CODES = %w[
53
+ InsideDelivery
54
+ LimitedAccessPickup
55
+ LimitedAccessDelivery
56
+ OriginLiftgate
57
+ DestinationLiftgate
58
+ DeliveryAppointment
59
+ Hazmat
60
+ InsidePickup
61
+ Freezable
62
+ SortAndSegregate
63
+ OverDimension
64
+ ].freeze
65
+
66
+ private
67
+
68
+ # Raises an exception if the additional service codes passed into the initializer
69
+ # don't correspond to valid codes from {ADDITIONAL_SERVICE_CODES}.
70
+ #
71
+ # @raise [ArgumentError] invalid additional service codes
72
+ # @return [nil]
73
+ def validate_additional_service_codes!
74
+ invalid_codes = (additional_service_codes - ADDITIONAL_SERVICE_CODES)
75
+ return unless invalid_codes.any?
76
+
77
+ raise ArgumentError, "Invalid additional service code(s): #{invalid_codes.join(',')}"
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Serializes packages for R+L rate quote API requests.
7
+ class RateQuotePackagesSerializer
8
+ class << self
9
+ # @param packages [Array<Physical::Package>] packages to serialize
10
+ # @param options [RateQuoteOptions] options for this request
11
+ # @return [Array<Hash>] serialized packages
12
+ def call(packages:, options:)
13
+ item_hashes = packages.flat_map do |package|
14
+ package_options = options.options_for_package(package)
15
+ package.items.map do |item|
16
+ item_options = package_options.options_for_item(item)
17
+ {
18
+ Class: item_options.freight_class,
19
+ Height: item.height.convert_to(:inches),
20
+ Length: item.length.convert_to(:inches),
21
+ Weight: item.weight.convert_to(:pounds),
22
+ Width: item.width.convert_to(:inches)
23
+ }
24
+ end
25
+ end
26
+ group_items(item_hashes)
27
+ end
28
+
29
+ private
30
+
31
+ # Group items by freight class. The R+L Carriers API has a limit on the number of items
32
+ # we can submit to the API, so this helps reduce the number of items.
33
+ #
34
+ # @param item_hashes [Array<Hash>] the item hashes to group
35
+ # @return [Array<Hash>] item hashes grouped by freight class
36
+ def group_items(item_hashes)
37
+ item_hashes.group_by do |item_hash|
38
+ item_hash[:Class]
39
+ end.map do |freight_class, grouped_item_hashes|
40
+ {
41
+ Class: freight_class,
42
+ Height: grouped_item_hashes.map { _1[:Height].value.ceil }.max,
43
+ Length: grouped_item_hashes.map { _1[:Length].value.ceil }.max,
44
+ Weight: grouped_item_hashes.sum { _1[:Weight] }.value.ceil,
45
+ Width: grouped_item_hashes.map { _1[:Width].value.ceil }.max,
46
+ Quantity: grouped_item_hashes.size
47
+ }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ class RateQuoteStructuresSerializer
7
+ class << self
8
+ # @param structures [Array<Physical::Structure>]
9
+ # @param options [RateQuoteOptions]
10
+ # @return [Array<Hash>]
11
+ def call(structures:, options:)
12
+ item_hashes = 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
+ Class: package_options.freight_class,
18
+ Height: package.height.convert_to(:inches),
19
+ Length: package.length.convert_to(:inches),
20
+ Weight: package.weight.convert_to(:pounds),
21
+ Width: package.width.convert_to(:inches)
22
+ }
23
+ end
24
+ end
25
+ group_items(item_hashes)
26
+ end
27
+
28
+ private
29
+
30
+ # Group items by freight class. The R+L Carriers API has a limit on the number of items
31
+ # we can submit to the API, so this helps reduce the number of items.
32
+ #
33
+ # @param item_hashes [Array<Hash>]
34
+ # @return [Array<Hash>]
35
+ def group_items(item_hashes)
36
+ item_hashes.group_by do |item_hash|
37
+ item_hash[:Class]
38
+ end.map do |freight_class, grouped_item_hashes|
39
+ {
40
+ Class: freight_class,
41
+ Height: grouped_item_hashes.map { _1[:Height].value.ceil }.max,
42
+ Length: grouped_item_hashes.map { _1[:Length].value.ceil }.max,
43
+ Weight: grouped_item_hashes.sum { _1[:Weight] }.value.ceil,
44
+ Width: grouped_item_hashes.map { _1[:Width].value.ceil }.max,
45
+ Quantity: grouped_item_hashes.size
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Serializes an R+L API request to create a Bill of Lading (BOL).
7
+ class SerializeCreateBOLRequest
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment for the request
10
+ # @param options [BOLOptions] options for the request
11
+ # @return [Hash] the serialized request
12
+ def call(shipment:, options:)
13
+ {
14
+ BillOfLading: {
15
+ BOLDate: Time.now.strftime('%m/%d/%Y'),
16
+ Shipper: SerializeLocation.call(shipment.origin),
17
+ Consignee: SerializeLocation.call(shipment.destination),
18
+ BillTo: SerializeLocation.call(shipment.origin),
19
+ Items: serialize_items(shipment, options),
20
+ DeclaredValue: serialize_declared_value(options.declared_value),
21
+ SpecialInstructions: options.special_instructions,
22
+ ReferenceNumbers: serialize_reference_numbers(options.reference_numbers),
23
+ AdditionalServices: options.additional_service_codes
24
+ }.compact,
25
+ PickupRequest: serialize_pickup_request(options),
26
+ GenerateUniversalPro: !!options.generate_universal_pro
27
+ }.compact
28
+ end
29
+
30
+ private
31
+
32
+ # @param shipment [Physical::Shipment] the shipment with items to serialize
33
+ # @param options [BOLOptions] options for the items to be serialized
34
+ # @return [Hash]
35
+ def serialize_items(shipment, options)
36
+ if options.packages_serializer
37
+ warn "[DEPRECATION] `packages_serializer` is deprecated. Please use `structures_serializer` instead."
38
+ options.packages_serializer.call(packages: shipment.packages, options: options)
39
+ else
40
+ options.structures_serializer.call(structures: shipment.structures, options: options)
41
+ end
42
+ end
43
+
44
+ # @param declared_value [Numeric] the declared value to serialize
45
+ # @return [Hash, nil] the serialized declared value or nil if the value is blank
46
+ def serialize_declared_value(declared_value)
47
+ return if declared_value.blank?
48
+
49
+ {
50
+ Amount: declared_value,
51
+ Per: "1"
52
+ }
53
+ end
54
+
55
+ # @param reference_numbers [Hash] the reference numbers to serialize
56
+ # @return [Hash, nil] the serialized reference numbers or nil if the reference numbers are blank
57
+ def serialize_reference_numbers(reference_numbers)
58
+ return if reference_numbers.blank?
59
+
60
+ {
61
+ ShipperNumber: reference_numbers[:shipper_number],
62
+ RateQuoteNumber: reference_numbers[:rate_quote_number],
63
+ PONumber: reference_numbers[:po_number]
64
+ }.compact
65
+ end
66
+
67
+ # @param options [BOLOptions] the options containing pickup information
68
+ # @return [Hash, nil] the serialized pickup request
69
+ def serialize_pickup_request(options)
70
+ pickup_time_window = options.pickup_time_window
71
+ return if pickup_time_window.nil?
72
+
73
+ {
74
+ PickupInformation: {
75
+ PickupDate: pickup_time_window.begin.strftime('%m/%d/%Y'),
76
+ ReadyTime: pickup_time_window.begin.strftime('%I:%M %p'),
77
+ CloseTime: pickup_time_window.end.strftime('%I:%M %p'),
78
+ AdditionalInstructions: options.pickup_instructions
79
+ }.compact
80
+ }
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Serialize a physical location for use in an R+L API call.
7
+ class SerializeLocation
8
+ class << self
9
+ # @param [Physical::Location] location
10
+ # @return [Hash]
11
+ def call(location)
12
+ {
13
+ CompanyName: location.company_name.presence || location.name,
14
+ AddressLine1: truncate(location.address1),
15
+ AddressLine2: truncate(location.address2),
16
+ City: clean_city(location.city),
17
+ StateOrProvince: location.region.code,
18
+ ZipOrPostalCode: location.zip,
19
+ CountryCode: location.country.alpha_3_code,
20
+ PhoneNumber: clean_phone(location.phone),
21
+ EmailAddress: location.email
22
+ }.compact
23
+ end
24
+
25
+ private
26
+
27
+ # R+L does not support periods in city names.
28
+ #
29
+ # @param city [String]
30
+ # @return [String]
31
+ def clean_city(city)
32
+ city.delete(".").strip
33
+ end
34
+
35
+ # R+L does not support leading country codes in phone numbers.
36
+ #
37
+ # @param [String] phone
38
+ # @return [String]
39
+ def clean_phone(phone)
40
+ phone.gsub(/^1-/, "").strip
41
+ end
42
+
43
+ # @param value [String]
44
+ # @param length [Integer]
45
+ # @return [String]
46
+ def truncate(value, length: 30)
47
+ value && value[0..(length - 1)].strip
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end