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,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'friendly_shipping/services/rl/shipping_methods'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class RL
9
+ # Parses the response from the R+L API when getting rate quotes.
10
+ class ParseRateQuoteResponse
11
+ extend Dry::Monads::Result::Mixin
12
+
13
+ class << self
14
+ # @param request [Request] the request to attach to the API result
15
+ # @param response [Response] the response to parse
16
+ # @return [Success<ApiResult>, Failure<ApiResult>] the parsed rates
17
+ def call(request:, response:)
18
+ parsed_json = JSON.parse(response.body)
19
+ rates = build_rates(parsed_json)
20
+ if rates.any?
21
+ Success(
22
+ ApiResult.new(
23
+ rates,
24
+ original_request: request,
25
+ original_response: response
26
+ )
27
+ )
28
+ else
29
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
30
+ Failure(
31
+ ApiResult.new(
32
+ errors.map { |e| e['ErrorMessage'] },
33
+ original_request: request,
34
+ original_response: response
35
+ )
36
+ )
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # The currency to use when parsing the rate quotes.
43
+ CURRENCY = Money::Currency.new('USD').freeze
44
+
45
+ # Builds {Rate} instances from the parsed JSON.
46
+ #
47
+ # @param parsed_json [String] the parsed JSON
48
+ # @return [Array<Rate>] the parsed rates
49
+ def build_rates(parsed_json)
50
+ service_levels = parsed_json.dig('RateQuote', 'ServiceLevels')
51
+ return [] unless service_levels
52
+
53
+ service_levels.map do |service_level|
54
+ shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_level['Code'] }
55
+ total = Money.new(service_level['NetCharge'].delete('$,.'), CURRENCY)
56
+ FriendlyShipping::Rate.new(
57
+ shipping_method: shipping_method,
58
+ amounts: { total: total }
59
+ )
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'friendly_shipping/services/rl/shipping_methods'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class RL
9
+ # Parses the response from the R+L API when getting transit times.
10
+ class ParseTransitTimesResponse
11
+ extend Dry::Monads::Result::Mixin
12
+
13
+ class << self
14
+ # @param request [Request] the request to attach to the API result
15
+ # @param response [Response] the response to parse
16
+ # @return [Success<ApiResult>, Failure<ApiResult>] the parsed timings
17
+ def call(request:, response:)
18
+ parsed_json = JSON.parse(response.body)
19
+ timings = build_timings(parsed_json)
20
+ if timings.any?
21
+ Success(
22
+ ApiResult.new(
23
+ timings,
24
+ original_request: request,
25
+ original_response: response
26
+ )
27
+ )
28
+ else
29
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
30
+ Failure(
31
+ ApiResult.new(
32
+ errors.map { |e| e['ErrorMessage'] },
33
+ original_request: request,
34
+ original_response: response
35
+ )
36
+ )
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # Builds {Timing} instances from the parsed JSON.
43
+ #
44
+ # @param parsed_json [String] the parsed JSON
45
+ # @return [Array<Timing>] the parsed timings
46
+ def build_timings(parsed_json)
47
+ destinations = parsed_json['Destinations']
48
+ return [] unless destinations
49
+
50
+ pickup_date = parsed_json['PickupDate']
51
+ pickup = Time.strptime(pickup_date, '%m/%d/%Y')
52
+
53
+ destination = destinations.first
54
+
55
+ delivery_date = destination['DeliveryDate']
56
+ delivery = Time.strptime(delivery_date, '%m/%d/%Y')
57
+
58
+ days_in_transit = destination['ServiceDays']
59
+
60
+ [
61
+ FriendlyShipping::Timing.new(
62
+ shipping_method: SHIPPING_METHODS.first,
63
+ pickup: pickup,
64
+ delivery: delivery,
65
+ guaranteed: false,
66
+ data: {
67
+ days_in_transit: days_in_transit
68
+ }
69
+ )
70
+ ]
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/rl/shipment_options'
4
+ require 'friendly_shipping/services/rl/rate_quote_structures_serializer'
5
+ require 'friendly_shipping/services/rl/rate_quote_packages_serializer'
6
+
7
+ module FriendlyShipping
8
+ module Services
9
+ class RL
10
+ # Rate quote options for serializing R+L API requests.
11
+ class RateQuoteOptions < ShipmentOptions
12
+ # @return [Time] the pickup date
13
+ attr_reader :pickup_date
14
+
15
+ # @return [Numeric] the declared value of this shipment
16
+ attr_reader :declared_value
17
+
18
+ # @return [Array<String>] additional service codes
19
+ attr_reader :additional_service_codes
20
+
21
+ # @return [Callable] the serializer for this shipment's structures
22
+ attr_reader :structures_serializer
23
+
24
+ # @return [Callable] the serializer for this shipment's packages
25
+ # @deprecated Use {#structures_serializer} instead.
26
+ attr_reader :packages_serializer
27
+
28
+ # @param pickup_date [Time] the pickup date
29
+ # @param declared_value [Numeric] the declared value of this shipment
30
+ # @param additional_service_codes [Array<String>] additional service codes
31
+ # @param structures_serializer [Callable] a callable that takes structures
32
+ # and an options object to create an Array of item hashes per the R+L Carriers docs
33
+ # @param packages_serializer [Callable] a callable that takes packages
34
+ # and an options object to create an Array of item hashes per the R+L Carriers docs (DEPRECATED: use ``structures_serializer`` instead)
35
+ # @param kwargs [Hash]
36
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
37
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
38
+ def initialize(
39
+ pickup_date:,
40
+ declared_value: nil,
41
+ additional_service_codes: [],
42
+ structures_serializer: RateQuoteStructuresSerializer,
43
+ packages_serializer: RateQuotePackagesSerializer,
44
+ **kwargs
45
+ )
46
+ @pickup_date = pickup_date
47
+ @declared_value = declared_value
48
+ @additional_service_codes = additional_service_codes
49
+ @structures_serializer = structures_serializer
50
+ @packages_serializer = packages_serializer
51
+ validate_additional_service_codes!
52
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
53
+ end
54
+
55
+ # Optional service codes that can be used for R+L shipments.
56
+ ADDITIONAL_SERVICE_CODES = %w[
57
+ InsideDelivery
58
+ LimitedAccessPickup
59
+ LimitedAccessDelivery
60
+ OriginLiftgate
61
+ DestinationLiftgate
62
+ DeliveryAppointment
63
+ Hazmat
64
+ InsidePickup
65
+ Freezable
66
+ SortAndSegregate
67
+ OverDimension
68
+ ].freeze
69
+
70
+ private
71
+
72
+ # Raises an exception if the additional service codes passed into the initializer
73
+ # don't correspond to valid codes from {ADDITIONAL_SERVICE_CODES}.
74
+ #
75
+ # @raise [ArgumentError] invalid additional service codes
76
+ # @return [nil]
77
+ def validate_additional_service_codes!
78
+ invalid_codes = (additional_service_codes - ADDITIONAL_SERVICE_CODES)
79
+ return unless invalid_codes.any?
80
+
81
+ raise ArgumentError, "Invalid additional service code(s): #{invalid_codes.join(',')}"
82
+ end
83
+ end
84
+ end
85
+ end
86
+ 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,46 @@
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: location.address1,
15
+ AddressLine2: 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
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Serializes an R+L API request to get a shipping rate quote.
7
+ class SerializeRateQuoteRequest
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment for the request
10
+ # @param options [RateQuoteOptions] options for the request
11
+ # @return [Hash] the serialized request
12
+ def call(shipment:, options:)
13
+ {
14
+ RateQuote: {
15
+ PickupDate: options.pickup_date.strftime('%m/%d/%Y'),
16
+ Origin: serialize_location(shipment.origin),
17
+ Destination: serialize_location(shipment.destination),
18
+ Items: serialize_items(shipment, options),
19
+ DeclaredValue: options.declared_value,
20
+ AdditionalServices: options.additional_service_codes,
21
+ Pallets: serialize_pallets(shipment)
22
+ }.compact
23
+ }.compact
24
+ end
25
+
26
+ private
27
+
28
+ # @param location [Physical::Location] the location to serialize
29
+ # @return [Hash] the serialized location
30
+ def serialize_location(location)
31
+ {
32
+ City: location.city,
33
+ StateOrProvince: location.region.code,
34
+ ZipOrPostalCode: location.zip,
35
+ CountryCode: location.country.alpha_3_code
36
+ }.compact
37
+ end
38
+
39
+ # @param shipment [Physical::Shipment]
40
+ # @param options [RateQuoteOptions] options for the items
41
+ # @return [Array<Hash>] the serialized items
42
+ def serialize_items(shipment, options)
43
+ if options.packages_serializer
44
+ warn "[DEPRECATION] `packages_serializer` is deprecated. Please use `structures_serializer` instead."
45
+ options.packages_serializer.call(packages: shipment.packages, options: options)
46
+ else
47
+ options.structures_serializer.call(structures: shipment.structures, options: options)
48
+ end
49
+ end
50
+
51
+ # @param shipment [Physical::Shipment]
52
+ # @return [Array<Hash>] the serialized pallets
53
+ def serialize_pallets(shipment)
54
+ pallets = shipment.pallets.any? ? shipment.pallets : shipment.structures
55
+ pallets.group_by do |pallet|
56
+ pallet.weight.convert_to(:pounds).value.ceil
57
+ end.map do |weight, grouped_pallets|
58
+ {
59
+ Code: "0001",
60
+ Weight: weight,
61
+ Quantity: grouped_pallets.size
62
+ }
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Serializes an R+L API request to get a shipment transit timing estimate.
7
+ class SerializeTransitTimesRequest
8
+ class << self
9
+ # @param shipment [Physical::Shipment] the shipment for the request
10
+ # @param options [RateQuoteOptions] options for the request
11
+ # @return [Hash] the serialized request
12
+ def call(shipment:, options:)
13
+ {
14
+ PickupDate: options.pickup_date.strftime('%m/%d/%Y'),
15
+ Origin: serialize_location(shipment.origin),
16
+ Destinations: [
17
+ serialize_location(shipment.destination)
18
+ ]
19
+ }.compact
20
+ end
21
+
22
+ private
23
+
24
+ # @param location [Physical::Location] the location to serialize
25
+ # @return [Hash] the serialized location
26
+ def serialize_location(location)
27
+ {
28
+ City: location.city,
29
+ StateOrProvince: location.region.code,
30
+ ZipOrPostalCode: location.zip,
31
+ CountryCode: location.country.alpha_3_code
32
+ }.compact
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Represents an R+L shipment document such as a BOL or shipping label.
7
+ class ShipmentDocument
8
+ # @return [String] the format of the document's binary data
9
+ attr_reader :format
10
+
11
+ # @return [String] the type of document
12
+ attr_reader :document_type
13
+
14
+ # @return [String] the document's binary data
15
+ attr_reader :binary
16
+
17
+ # @param format [String] the format of the document's binary data
18
+ # @param document_type [String] the type of document
19
+ # @param binary [String] the document's binary data
20
+ def initialize(
21
+ format:,
22
+ document_type:,
23
+ binary:
24
+ )
25
+ @format = format
26
+ @document_type = document_type
27
+ @binary = binary
28
+ end
29
+
30
+ # Returns true if format, document type, and binary data are all present.
31
+ # Returns false if any of these values are missing.
32
+ #
33
+ # @return [Boolean]
34
+ def valid?
35
+ format.present? && document_type.present? && binary.present?
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Represents information for a specific shipment. This includes one or more
7
+ # documents, a PRO number, and a pickup request number.
8
+ class ShipmentInformation
9
+ # @return [String] the shipment's PRO number
10
+ attr_reader :pro_number
11
+
12
+ # @return [String] the shipment's pickup request number
13
+ attr_reader :pickup_request_number
14
+
15
+ # @return [Array<ShipmentDocument>] the shipment's documents
16
+ attr_reader :documents
17
+
18
+ # @param pro_number [String] the shipment's PRO number
19
+ # @param pickup_request_number [String] the shipment's pickup request number
20
+ # @param documents [Array<ShipmentDocument>] the shipment's documents (BOL, labels, etc)
21
+ def initialize(
22
+ pro_number:,
23
+ pickup_request_number: nil,
24
+ documents: []
25
+ )
26
+ @pro_number = pro_number
27
+ @pickup_request_number = pickup_request_number
28
+ @documents = documents
29
+ end
30
+
31
+ # Returns true if PRO number and pickup request number are present.
32
+ # Returns false if either of these values are missing.
33
+ #
34
+ # @return [Boolean]
35
+ def valid?
36
+ pro_number.present? && pickup_request_number.present?
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end