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
@@ -5,10 +5,15 @@ require 'json'
5
5
  module FriendlyShipping
6
6
  module Services
7
7
  class ShipEngine
8
- class ParseRateEstimateResponse
8
+ # Parses the rate estimates API response.
9
+ class ParseRateEstimatesResponse
9
10
  extend Dry::Monads::Result::Mixin
10
11
 
11
12
  class << self
13
+ # @param request [Request] the request to attach to the API result
14
+ # @param response [Response] the response to parse
15
+ # @param options [RateEstimatesOptions] the options to use when parsing
16
+ # @return [Success<ApiResult<Array<Rate>>>, Failure<ApiFailure<Array<String>>>] the parsed rate estimates or errors
12
17
  def call(response:, request:, options:)
13
18
  error_messages = []
14
19
  parsed_json = JSON.parse(response.body)
@@ -23,6 +28,7 @@ module FriendlyShipping
23
28
 
24
29
  shipping_method = FriendlyShipping::ShippingMethod.new(
25
30
  carrier: carrier,
31
+ name: rate['service_type'],
26
32
  service_code: rate['service_code']
27
33
  )
28
34
 
@@ -31,9 +37,19 @@ module FriendlyShipping
31
37
  shipping_method: shipping_method,
32
38
  amounts: amounts,
33
39
  remote_service_id: rate['rate_id'],
34
- delivery_date: Time.parse(rate['estimated_delivery_date']),
40
+ pickup_date: rate['ship_date'] && Time.parse(rate['ship_date']),
41
+ delivery_date: rate['estimated_delivery_date'] && Time.parse(rate['estimated_delivery_date']),
42
+ guaranteed: rate['guaranteed_service'],
35
43
  warnings: rate['warning_messages'],
36
- errors: rate['error_messages']
44
+ errors: rate['error_messages'],
45
+ data: {
46
+ package_type: rate['package_type'],
47
+ delivery_days: rate['delivery_days'],
48
+ carrier_delivery_days: rate['carrier_delivery_days'],
49
+ negotiated_rate: rate['negotiated_rate'],
50
+ trackable: rate['trackable'],
51
+ validation_status: rate['validation_status']
52
+ }
37
53
  )
38
54
  end.compact
39
55
 
@@ -58,21 +74,25 @@ module FriendlyShipping
58
74
 
59
75
  private
60
76
 
77
+ # @param parsed_json [Hash] the parsed JSON
78
+ # @return [Boolean] whether the rate estimates are valid
61
79
  def valid_rates(parsed_json)
62
80
  parsed_json.map do |rate|
63
- ["valid", "has_warnings", "unknown"].include? rate['validation_status']
81
+ %w[valid has_warnings unknown].include? rate['validation_status']
64
82
  end.any?
65
83
  end
66
84
 
85
+ # @param rate_hash [Hash] the rate hash
86
+ # @return [Hash<Symbol,Money>] the amounts in the hash
67
87
  def get_amounts(rate_hash)
68
- [:shipping, :other, :insurance, :confirmation].map do |name|
88
+ [:shipping, :other, :insurance, :confirmation].to_h do |name|
69
89
  currency = Money::Currency.new(rate_hash["#{name}_amount"]["currency"])
70
90
  amount = rate_hash["#{name}_amount"]["amount"] * currency.subunit_to_unit
71
91
  [
72
92
  name,
73
93
  Money.new(amount, currency)
74
94
  ]
75
- end.to_h
95
+ end
76
96
  end
77
97
  end
78
98
  end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class ShipEngine
8
+ # Parses the rates API response.
9
+ class ParseRatesResponse
10
+ extend Dry::Monads::Result::Mixin
11
+
12
+ class << self
13
+ # @param request [Request] the request to attach to the API result
14
+ # @param response [Response] the response to parse
15
+ # @param options [RatesOptions] the options to use when parsing
16
+ # @return [Success<ApiResult<Array<Rate>>>, Failure<ApiFailure<String>>] the parsed rates or error
17
+ def call(request:, response:, options:)
18
+ parsed_json = JSON.parse(response.body)
19
+ rates = build_rates(parsed_json, options)
20
+ if rates.any?
21
+ Success(
22
+ ApiResult.new(
23
+ rates,
24
+ original_request: request,
25
+ original_response: response
26
+ )
27
+ )
28
+ else
29
+ error = parsed_json.dig("rate_response", "errors", 0, "message") || "Unknown error"
30
+ Failure(
31
+ ApiFailure.new(
32
+ error,
33
+ original_request: request,
34
+ original_response: response
35
+ )
36
+ )
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # @param parsed_json [String] the parsed JSON
43
+ # @param options [RateOptions] the options to use when parsing
44
+ # @return [Array<Rate>] the parsed rates
45
+ def build_rates(parsed_json, options)
46
+ returned_rates = parsed_json.dig('rate_response', 'rates')
47
+ return [] unless returned_rates
48
+
49
+ returned_rates.map do |rate|
50
+ if rate['validation_status'] == 'invalid'
51
+ error_messages.concat rate['error_messages']
52
+ next
53
+ end
54
+
55
+ carrier = options.carriers.detect { |c| c.id == rate['carrier_id'] }
56
+ next unless carrier
57
+
58
+ shipping_method = FriendlyShipping::ShippingMethod.new(
59
+ carrier: carrier,
60
+ service_code: rate['service_code']
61
+ )
62
+
63
+ # validate service code is available/matches shipping method?
64
+
65
+ amounts = get_amounts(rate)
66
+ FriendlyShipping::Rate.new(
67
+ shipping_method: shipping_method,
68
+ amounts: amounts,
69
+ remote_service_id: rate['rate_id'],
70
+ delivery_date: rate['estimated_delivery_date'] && Time.parse(rate['estimated_delivery_date']),
71
+ warnings: rate['warning_messages'],
72
+ errors: rate['error_messages'],
73
+ data: {
74
+ package_type: rate['package_type'],
75
+ delivery_days: rate['delivery_days'],
76
+ carrier_delivery_days: rate['carrier_delivery_days'],
77
+ negotiated_rate: rate['negotiated_rate'],
78
+ trackable: rate['trackable'],
79
+ validation_status: rate['validation_status']
80
+ }
81
+ )
82
+ end.compact
83
+ end
84
+
85
+ # @param rate_hash [Hash] the rate hash
86
+ # @return [Hash<Symbol,Money>] the amounts in the hash
87
+ def get_amounts(rate_hash)
88
+ [:shipping, :other, :insurance, :confirmation].to_h do |name|
89
+ currency = Money::Currency.new(rate_hash["#{name}_amount"]["currency"])
90
+ amount = rate_hash["#{name}_amount"]["amount"] * currency.subunit_to_unit
91
+ [
92
+ name,
93
+ Money.new(amount, currency)
94
+ ]
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -3,9 +3,13 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class ShipEngine
6
+ # Parses the API response for voiding a label.
6
7
  class ParseVoidResponse
7
8
  extend Dry::Monads::Result::Mixin
8
9
 
10
+ # @param request [Request] the request to attach to the API result
11
+ # @param response [Response] the response to parse
12
+ # @return [Success<ApiResult<String>>, Failure<ApiFailure<String>>] the success or failure message
9
13
  def self.call(request:, response:)
10
14
  parsed_json = JSON.parse(response.body)
11
15
  approved, message = parsed_json["approved"], parsed_json["message"]
@@ -5,17 +5,30 @@ require 'friendly_shipping/shipment_options'
5
5
  module FriendlyShipping
6
6
  module Services
7
7
  class ShipEngine
8
- # options for the rate estimates call
9
- #
10
- # @attribute carriers [Array<FriendlyShipping::Carrier] a list of the carriers we want to get IDs from.
8
+ # Options for the rate estimates API call.
11
9
  class RateEstimatesOptions < ShipmentOptions
10
+ # @return [Array<Carrier>] the carriers for these rate estimates
12
11
  attr_reader :carriers
13
12
 
14
- def initialize(carriers:, **kwargs)
13
+ # @return [#strftime]
14
+ attr_reader :ship_date
15
+
16
+ # @param carriers [Array<Carrier] the carriers for these rate estimates
17
+ # @param ship_date [#strftime] the date we want to ship on
18
+ # @param kwargs [Hash]
19
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
20
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
21
+ def initialize(
22
+ carriers:,
23
+ ship_date: Date.today,
24
+ **kwargs
25
+ )
15
26
  @carriers = carriers
27
+ @ship_date = ship_date
16
28
  super(**kwargs)
17
29
  end
18
30
 
31
+ # @return [Array<String>] the carrier IDs for these rate estimates
19
32
  def carrier_ids
20
33
  carriers.map(&:id)
21
34
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/item_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class ShipEngine
8
+ # Options for each item passed in the rates API call.
9
+ class RatesItemOptions < FriendlyShipping::ItemOptions
10
+ # @return [String] the HS or NMFC code for international shipments
11
+ attr_reader :commodity_code
12
+
13
+ # @return [String] the country of origin for international shipments
14
+ attr_reader :country_of_origin
15
+
16
+ # @param commodity_code [String] the HS or NMFC code for international shipments
17
+ # @param country_of_origin [String] the country of origin for international shipments
18
+ # @param kwargs [Hash]
19
+ # @option kwargs [String] :item_id the ID for the item that belongs to these options
20
+ def initialize(commodity_code: nil, country_of_origin: nil, **kwargs)
21
+ @commodity_code = commodity_code
22
+ @country_of_origin = country_of_origin
23
+ super(**kwargs)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/shipment_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class ShipEngine
8
+ # Options for the rates API call.
9
+ class RatesOptions < FriendlyShipping::ShipmentOptions
10
+ # @return [Array<Carrier>] the carriers from which to get rates
11
+ attr_reader :carriers
12
+
13
+ # @return [String] the service code for which to get rates
14
+ attr_reader :service_code
15
+
16
+ # @return [#strftime]
17
+ attr_reader :ship_date
18
+
19
+ # @return [String]
20
+ attr_reader :comparison_rate_type
21
+
22
+ # @param carriers [Array<Carrier>] the carriers for these rates
23
+ # @param service_code [String] the service code to use when getting rates
24
+ # @param ship_date [#strftime] the date we want to ship on
25
+ # @param comparison_rate_type [String] set to "retail" for retail rates (UPS/USPS only)
26
+ # @param kwargs [Hash]
27
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
28
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
29
+ def initialize(
30
+ carriers:,
31
+ service_code:,
32
+ ship_date: Date.today,
33
+ comparison_rate_type: nil,
34
+ **kwargs
35
+ )
36
+ @carriers = carriers
37
+ @service_code = service_code
38
+ @ship_date = ship_date
39
+ @comparison_rate_type = comparison_rate_type
40
+ validate_comparison_rate_type!
41
+ super(**kwargs.reverse_merge(package_options_class: RatesPackageOptions))
42
+ end
43
+
44
+ # @return [Array<String>] the carrier IDs for these rates
45
+ def carrier_ids
46
+ carriers.map(&:id)
47
+ end
48
+
49
+ private
50
+
51
+ # @raise [ArgumentError] invalid comparison rate type
52
+ # @return [nil]
53
+ def validate_comparison_rate_type!
54
+ return if comparison_rate_type.nil? || comparison_rate_type == "retail"
55
+
56
+ raise ArgumentError, "Invalid comparison rate type: #{comparison_rate_type}"
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/ups_freight/rates_item_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class ShipEngine
8
+ # Options for each package passed in the rates API call.
9
+ class RatesPackageOptions < FriendlyShipping::PackageOptions
10
+ # @param kwargs [Hash]
11
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
12
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
13
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
14
+ def initialize(**kwargs)
15
+ super(**kwargs.reverse_merge(item_options_class: RatesItemOptions))
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ class SerializeAddressResidentialIndicator
7
+ class << self
8
+ # @param location [Physical::Location]
9
+ # @return [Hash]
10
+ def call(location)
11
+ { address_residential_indicator: residential_indicator(location) }
12
+ end
13
+
14
+ private
15
+
16
+ # @param location [Physical::Location]
17
+ # @return [String]
18
+ def residential_indicator(location)
19
+ return "unknown" if location&.address_type.nil?
20
+
21
+ location.address_type == "residential" ? "yes" : "no"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ class SerializeAddressValidationRequest
7
+ class << self
8
+ # @param location [Physical::Location]
9
+ # @return [Array<Hash>]
10
+ def call(location:)
11
+ [
12
+ {
13
+ name: location.name,
14
+ phone: location.phone,
15
+ email: location.email,
16
+ company_name: location.company_name,
17
+ address_line1: location.address1,
18
+ address_line2: location.address2,
19
+ address_line3: location.address3,
20
+ city_locality: location.city,
21
+ state_province: location.region&.code,
22
+ postal_code: location.zip,
23
+ country_code: location.country&.code
24
+ }.merge(SerializeAddressResidentialIndicator.call(location))
25
+ ]
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -3,8 +3,13 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class ShipEngine
6
+ # Serializes a shipment and options for the label API request.
6
7
  class SerializeLabelShipment
7
8
  class << self
9
+ # @param shipment [Physical::Shipment] the shipment to serialize
10
+ # @param options [LabelOptions] the options to serialize
11
+ # @param test [Boolean] whether to use the test API
12
+ # @return [Hash] the serialized request
8
13
  def call(shipment:, options:, test:)
9
14
  shipment_hash = {
10
15
  label_format: options.label_format,
@@ -25,6 +30,7 @@ module FriendlyShipping
25
30
  shipment_hash[:shipment][:customs] = serialize_customs(shipment.packages, options)
26
31
  end
27
32
 
33
+ # Not all carriers support test labels
28
34
  if test
29
35
  shipment_hash[:test_label] = true
30
36
  end
@@ -38,6 +44,8 @@ module FriendlyShipping
38
44
 
39
45
  private
40
46
 
47
+ # @param address [Physical::Location]
48
+ # @return [Hash]
41
49
  def serialize_address(address)
42
50
  {
43
51
  name: address.name,
@@ -48,17 +56,19 @@ module FriendlyShipping
48
56
  city_locality: address.city,
49
57
  state_province: address.region.code,
50
58
  postal_code: address.zip,
51
- country_code: address.country.code,
52
- address_residential_indicator: "No"
53
- }
59
+ country_code: address.country.code
60
+ }.merge(SerializeAddressResidentialIndicator.call(address))
54
61
  end
55
62
 
63
+ # @param packages [Array<Physical::Package>]
64
+ # @param options [LabelOptions]
65
+ # @return [Array<Hash>]
56
66
  def serialize_packages(packages, options)
57
67
  packages.map do |package|
58
68
  package_options = options.options_for_package(package)
59
69
  package_hash = serialize_weight(package.weight)
60
70
  package_hash[:label_messages] = package_options.messages.map.with_index do |message, index|
61
- ["reference#{index + 1}".to_sym, message]
71
+ [:"reference#{index + 1}", message]
62
72
  end.to_h
63
73
 
64
74
  package_hash[:package_code] = package_options.package_code
@@ -72,37 +82,20 @@ module FriendlyShipping
72
82
  end
73
83
  end
74
84
 
85
+ # @param packages [Array<Physical::Package>]
86
+ # @param options [LabelOptions]
75
87
  def serialize_customs(packages, options)
76
88
  {
77
89
  contents: options.customs_options.contents,
78
90
  non_delivery: options.customs_options.non_delivery,
79
- customs_items: serialize_customs_items(packages, options)
91
+ customs_items: options.customs_items_serializer.call(packages, options)
80
92
  }
81
93
  end
82
94
 
83
- def serialize_customs_items(packages, options)
84
- packages.map do |package|
85
- package.items.group_by(&:sku).map do |sku, items|
86
- reference_item = items.first
87
- package_options = options.options_for_package(package)
88
- item_options = package_options.options_for_item(reference_item)
89
- {
90
- sku: sku,
91
- description: reference_item.description,
92
- quantity: items.count,
93
- value: {
94
- amount: reference_item.cost.to_d,
95
- currency: reference_item.cost.currency.to_s
96
- },
97
- harmonized_tariff_code: item_options.commodity_code,
98
- country_of_origin: item_options.country_of_origin
99
- }
100
- end
101
- end.flatten
102
- end
103
-
95
+ # @param weight [Measured::Weight]
96
+ # @return [Hash]
104
97
  def serialize_weight(weight)
105
- ounces = weight.convert_to(:ounce).value.to_f
98
+ ounces = weight.convert_to(:ounce).value.to_f.round(2)
106
99
  {
107
100
  weight: {
108
101
  # Max weight for USPS First Class is 15.9 oz, not 16 oz
@@ -112,6 +105,8 @@ module FriendlyShipping
112
105
  }
113
106
  end
114
107
 
108
+ # @param shipment [Physical::Shipment]
109
+ # @return [Boolean]
115
110
  def international?(shipment)
116
111
  shipment.origin.country != shipment.destination.country
117
112
  end
@@ -3,23 +3,48 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class ShipEngine
6
+ # Serializes a shipment and options for the rate estimates API request.
6
7
  class SerializeRateEstimateRequest
7
- def self.call(shipment:, options:)
8
- {
9
- carrier_ids: options.carrier_ids,
10
- from_country_code: shipment.origin.country.alpha_2_code,
11
- from_postal_code: shipment.origin.zip,
12
- to_country_code: shipment.destination.country.alpha_2_code,
13
- to_postal_code: shipment.destination.zip,
14
- to_city_locality: shipment.destination.city,
15
- to_state_province: shipment.destination.region.code,
16
- weight: {
17
- value: shipment.packages.map { |p| p.weight.convert_to(:pound).value.to_f }.sum,
18
- unit: 'pound'
19
- },
20
- confirmation: 'none',
21
- address_residential_indicator: shipment.destination.residential? ? "yes" : "no"
22
- }
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment to serialize
10
+ # @param options [LabelOptions] the options to serialize
11
+ # @return [Hash] the serialized request
12
+ def call(shipment:, options:)
13
+ {
14
+ carrier_ids: options.carrier_ids,
15
+ from_country_code: shipment.origin.country.alpha_2_code,
16
+ from_postal_code: shipment.origin.zip,
17
+ to_country_code: shipment.destination.country.alpha_2_code,
18
+ to_postal_code: shipment.destination.zip,
19
+ to_city_locality: shipment.destination.city,
20
+ to_state_province: shipment.destination.region.code,
21
+ weight: {
22
+ value: shipment.packages.map { |p| p.weight.convert_to(:pound).value.to_f }.sum.round(2),
23
+ unit: 'pound'
24
+ },
25
+ dimensions: dimensions(shipment.packages),
26
+ confirmation: 'none',
27
+ ship_date: options.ship_date.strftime('%Y-%m-%d'),
28
+ }.merge(SerializeAddressResidentialIndicator.call(shipment.destination)).compact_blank
29
+ end
30
+
31
+ private
32
+
33
+ # @param packages [Array<Physical::Package>]
34
+ # @return [Hash]
35
+ def dimensions(packages)
36
+ length = packages.map { |p| p.length.convert_to(:inch).value.to_f }.sum.round(2)
37
+ width = packages.map { |p| p.width.convert_to(:inch).value.to_f }.sum.round(2)
38
+ height = packages.map { |p| p.height.convert_to(:inch).value.to_f }.sum.round(2)
39
+ return {} if length == 0 && width == 0 && height == 0
40
+
41
+ {
42
+ unit: 'inch',
43
+ length: length,
44
+ width: width,
45
+ height: height
46
+ }
47
+ end
23
48
  end
24
49
  end
25
50
  end