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,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
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ class ShipmentOptions < FriendlyShipping::ShipmentOptions
7
+ # @param structure_options [Array<StructureOptions>]
8
+ # @param structure_options_class [Class]
9
+ # @param kwargs [Hash]
10
+ # @option kwargs [Array<PackageOptions>] :package_options
11
+ # @option kwargs [Class] :package_options_class
12
+ def initialize(
13
+ structure_options: Set.new,
14
+ structure_options_class: StructureOptions,
15
+ **kwargs
16
+ )
17
+ @structure_options = structure_options
18
+ @structure_options_class = structure_options_class
19
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
20
+ end
21
+
22
+ # @param [#id] structure
23
+ # @return [StructureOptions]
24
+ def options_for_structure(structure)
25
+ structure_options.detect do |structure_option|
26
+ structure_option.structure_id == structure.id
27
+ end || structure_options_class.new(structure_id: nil)
28
+ end
29
+
30
+ private
31
+
32
+ # @return [Array<StructureOptions>]
33
+ attr_reader :structure_options
34
+
35
+ # @return [Class]
36
+ attr_reader :structure_options_class
37
+
38
+ # @return [Array<PackageOptions>]
39
+ # @deprecated Use {#structures_serializer} instead.
40
+ attr_reader :package_options
41
+
42
+ # @return [Class]
43
+ # @deprecated Use {#structures_serializer_class} instead.
44
+ attr_reader :package_options_class
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Available origin countries for R+L shipments.
7
+ ORIGIN_COUNTRIES = %w[
8
+ AG BB CA DO GU GY JM PR VC TT US VG VI
9
+ ].map { |country_code| Carmen::Country.coded(country_code) }.freeze
10
+
11
+ # Available R+L shipping methods.
12
+ SHIPPING_METHODS = [
13
+ ["STD", "Standard Service"],
14
+ ["GSDS", "Guaranteed Service"],
15
+ ["GSAM", "Guaranteed AM Service"],
16
+ ["GSHW", "Guaranteed Hourly Window Service"],
17
+ ["EXPD", "Expedited Service"]
18
+ ].freeze.map do |code, name|
19
+ FriendlyShipping::ShippingMethod.new(
20
+ name: name,
21
+ service_code: code,
22
+ origin_countries: ORIGIN_COUNTRIES,
23
+ multi_package: true
24
+ ).freeze
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ class StructureOptions < FriendlyShipping::StructureOptions
7
+ def initialize(**kwargs)
8
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ # API service class for R+L Carriers, a freight/LTL carrier.
8
+ # @see https://www.rlcarriers.com/freight/shipping-software/api R+L API docs
9
+ class RL
10
+ include Dry::Monads::Result::Mixin
11
+
12
+ # @return [String] the API key for this carrier
13
+ attr_reader :api_key
14
+
15
+ # @return [Boolean] whether to use test API endpoints
16
+ attr_reader :test
17
+
18
+ # @return [HttpClient] the HTTP client to use for requests
19
+ attr_reader :client
20
+
21
+ # The API base URL for production.
22
+ LIVE_API_BASE = "https://api.rlc.com/"
23
+
24
+ # The API base URL for testing.
25
+ TEST_API_BASE = "https://apisandbox.rlc.com/"
26
+
27
+ # This carrier's API paths. Used when constructing endpoint URLs.
28
+ API_PATHS = {
29
+ bill_of_lading: "BillOfLading",
30
+ documents: "DocumentRetrieval",
31
+ print_bol: "BillOfLading/PrintBOL",
32
+ print_shipping_labels: "BillOfLading/PrintShippingLabels",
33
+ rate_quote: "RateQuote",
34
+ transit_times: "TransitTimes"
35
+ }.freeze
36
+
37
+ # @param [String] api_key the API key for this carrier
38
+ # @param [Boolean] test whether to use test API endpoints
39
+ # @param [HttpClient] client the HTTP client to use for requests
40
+ def initialize(api_key:, test: true, client: nil)
41
+ @api_key = api_key
42
+ @test = test
43
+
44
+ error_handler = ApiErrorHandler.new(api_error_class: RL::ApiError)
45
+ @client = client || HttpClient.new(error_handler: error_handler)
46
+ end
47
+
48
+ # Create an LTL Bill of Lading (BOL) and schedule a pickup with R+L Carriers.
49
+ #
50
+ # @param shipment [Physical::Shipment] the shipment for the BOL
51
+ # @param options [BOLOptions] the options for the BOL
52
+ # @return [Success<ApiResult<ShipmentInformation>>, Failure<ApiResult>] the BOL from R+L Carriers
53
+ def create_bill_of_lading(shipment, options:, debug: false)
54
+ request = FriendlyShipping::Request.new(
55
+ url: api_base + API_PATHS[:bill_of_lading],
56
+ http_method: "POST",
57
+ body: SerializeCreateBOLRequest.call(shipment: shipment, options: options).to_json,
58
+ headers: request_headers,
59
+ debug: debug
60
+ )
61
+ client.post(request).bind do |response|
62
+ ParseCreateBOLResponse.call(request: request, response: response)
63
+ end
64
+ end
65
+
66
+ # Retrieve an existing binary LTL Bill of Lading (BOL) document for printing. The BOL is appended
67
+ # to the {ShipmentInformation} object's documents array.
68
+ #
69
+ # @param shipment_info [ShipmentInformation] the shipment for the BOL
70
+ # @return [Success<ApiResult<ShipmentDocument>>, Failure<ApiResult>] the binary BOL document from R+L Carriers
71
+ def print_bill_of_lading(shipment_info, debug: false)
72
+ request = FriendlyShipping::Request.new(
73
+ url: api_base + API_PATHS[:print_bol] + "?ProNumber=#{shipment_info.pro_number}",
74
+ http_method: "GET",
75
+ headers: request_headers,
76
+ debug: debug
77
+ )
78
+ client.get(request).bind do |response|
79
+ ParsePrintBOLResponse.call(request: request, response: response).bind do |api_result|
80
+ shipment_info.documents << api_result.data
81
+ Success(api_result)
82
+ end
83
+ end
84
+ end
85
+
86
+ # Retrieve binary LTL shipping label documents for printing. The label documents are appended
87
+ # to the {ShipmentInformation} object's documents array.
88
+ #
89
+ # @param shipment_info [ShipmentInformation] the shipment for the labels
90
+ # @param style [Integer] the R+L Carriers shipping label style (between 1 and 13)
91
+ # @see https://rl-cdn.com/docs/rlc/shipping-forms/shipping-label-select.pdf Shipping label styles
92
+ # @param start_position [Integer] the R+L Carriers start position for the first label (between 1 and 10)
93
+ # @param num_labels [Integer] number of labels to print (between 1 and 100)
94
+ # @return [Success<ApiResult<ShipmentDocument>>, Failure<ApiResult>] the binary shipping labels from R+L Carriers
95
+ def print_shipping_labels(shipment_info, style: 1, start_position: 1, num_labels: 4, debug: false)
96
+ request = FriendlyShipping::Request.new(
97
+ url: api_base + API_PATHS[:print_shipping_labels] +
98
+ "?ProNumber=#{shipment_info.pro_number}&" \
99
+ "Style=#{style}&" \
100
+ "StartPosition=#{start_position}&" \
101
+ "NumberOfLabels=#{num_labels}",
102
+ http_method: "GET",
103
+ headers: request_headers,
104
+ debug: debug
105
+ )
106
+ client.get(request).bind do |response|
107
+ ParsePrintShippingLabelsResponse.call(request: request, response: response).bind do |api_result|
108
+ shipment_info.documents << api_result.data
109
+ Success(api_result)
110
+ end
111
+ end
112
+ end
113
+
114
+ # Request an LTL shipping rate quote from R+L Carriers.
115
+ #
116
+ # @param shipment [Physical::Shipment] the shipment to quote
117
+ # @param options [RateQuoteOptions] the options for the quote
118
+ #
119
+ # @return [Result<ApiResult<Array<Rate>>>] the rate quote from R+L Carriers
120
+ def rate_quote(shipment, options:, debug: false)
121
+ request = FriendlyShipping::Request.new(
122
+ url: LIVE_API_BASE + API_PATHS[:rate_quote],
123
+ http_method: "POST",
124
+ body: SerializeRateQuoteRequest.call(shipment: shipment, options: options).to_json,
125
+ headers: request_headers,
126
+ debug: debug
127
+ )
128
+ client.post(request).bind do |response|
129
+ ParseRateQuoteResponse.call(request: request, response: response)
130
+ end
131
+ end
132
+
133
+ # Request an LTL shipment transit timing from R+L Carriers.
134
+ #
135
+ # @param shipment [Physical::Shipment] the shipment we're timing
136
+ # @param options [RateQuoteOptions] the options for the timing
137
+ #
138
+ # @return [Result<ApiResult<Array<Timing>>>] the transit timing from R+L Carriers
139
+ def transit_times(shipment, options:, debug: false)
140
+ request = FriendlyShipping::Request.new(
141
+ url: LIVE_API_BASE + API_PATHS[:transit_times],
142
+ http_method: "POST",
143
+ body: SerializeTransitTimesRequest.call(shipment: shipment, options: options).to_json,
144
+ headers: request_headers,
145
+ debug: debug
146
+ )
147
+ client.post(request).bind do |response|
148
+ ParseTransitTimesResponse.call(request: request, response: response)
149
+ end
150
+ end
151
+
152
+ # Retrieve an existing binary Invoice
153
+ #
154
+ # @param [String] pro_number The PRO number for the Invoice
155
+ #
156
+ # @return [Result<ApiResult<ShipmentDocument>>] The binary Invoice document from R&L
157
+ def get_invoice(pro_number, debug: false)
158
+ request = FriendlyShipping::Request.new(
159
+ url: api_base + API_PATHS[:documents] + "?ProNumber=#{pro_number}&DocumentTypes=Invoice&MediaType=PDF",
160
+ http_method: "GET",
161
+ headers: request_headers,
162
+ debug: debug
163
+ )
164
+ client.get(request).bind do |response|
165
+ ParseInvoiceResponse.call(request: request, response: response)
166
+ end
167
+ end
168
+
169
+ private
170
+
171
+ # Returns the content type and API key as a headers hash.
172
+ # @return [Hash]
173
+ def request_headers
174
+ {
175
+ content_type: :json,
176
+ apiKey: api_key
177
+ }
178
+ end
179
+
180
+ # Returns the API base URL based on the {test} attribute's value.
181
+ # @return [String]
182
+ def api_base
183
+ test ? TEST_API_BASE : LIVE_API_BASE
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ # Raised when an API error is returned.
7
+ class ApiError < FriendlyShipping::ApiError
8
+ # @param cause [RestClient::Exception] the cause of the error
9
+ def initialize(cause)
10
+ super(cause, parse_message(cause))
11
+ end
12
+
13
+ private
14
+
15
+ # Parses the message from the error response.
16
+ #
17
+ # @param error [RestClient::Exception] the cause of the error
18
+ # @return [String, nil] the message or nil if it couldn't be parsed
19
+ def parse_message(error)
20
+ return error.message unless error.response && error.http_code == 400
21
+
22
+ parsed_body = JSON.parse(error.response.body)
23
+ messages = parsed_body.fetch('errors')&.map { |e| e.fetch('message') }
24
+ messages&.join(', ')
25
+ rescue JSON::ParserError, KeyError => _e
26
+ nil
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ # Serializes customs items for international labels.
7
+ class CustomsItemsSerializer
8
+ class << self
9
+ # @param packages [Array<Physical::Package>] the packages to serialize
10
+ # @param options [LabelOptions] options for the packages
11
+ # @return [Array<Hash>] the serialized customs items
12
+ def call(packages, options)
13
+ packages.map do |package|
14
+ package.items.group_by(&:sku).map do |sku, items|
15
+ reference_item = items.first
16
+ package_options = options.options_for_package(package)
17
+ item_options = package_options.options_for_item(reference_item)
18
+ {
19
+ sku: sku,
20
+ description: reference_item.description,
21
+ quantity: items.count,
22
+ value: {
23
+ amount: reference_item.cost.to_d,
24
+ currency: reference_item.cost.currency.to_s
25
+ },
26
+ harmonized_tariff_code: item_options.commodity_code,
27
+ country_of_origin: item_options.country_of_origin
28
+ }
29
+ end
30
+ end.flatten
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -3,15 +3,18 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class ShipEngine
6
- # Represents customs options for obtaining international shipment labels.
7
- # @option contents [String] The contents of the shipment.
8
- # Valid values are: gift, merchandise, returned_goods, documents, sample
9
- # @option non_delivery [String] Indicates what should be done if the shipment cannot be delivered.
10
- # Valid values are: treat_as_abandoned, return_to_sender
6
+ # Options for obtaining international shipment labels with customs.
11
7
  class LabelCustomsOptions
12
- attr_reader :contents,
13
- :non_delivery
8
+ # @return [String] the contents of the shipment
9
+ attr_reader :contents
14
10
 
11
+ # @return [String] indicates what should be done if the shipment cannot be delivered
12
+ attr_reader :non_delivery
13
+
14
+ # @param contents [String] the contents of the shipment. Valid values are: gift, merchandise,
15
+ # returned_goods, documents, sample
16
+ # @param non_delivery [String] indicates what should be done if the shipment cannot be delivered.
17
+ # Valid values are: treat_as_abandoned, return_to_sender
15
18
  def initialize(
16
19
  contents: "merchandise",
17
20
  non_delivery: "return_to_sender"
@@ -1,17 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/item_options'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class ShipEngine
8
- # Represents item options for obtaining shipment labels.
9
- # @option commodity_code [String] This item's HS or NMFC code for international shipments.
10
- # @option country_of_origin [String] This item's country of origin for international shipments.
6
+ # Item options for generating shipping labels.
11
7
  class LabelItemOptions < FriendlyShipping::ItemOptions
12
- attr_reader :commodity_code,
13
- :country_of_origin
8
+ # @return [String] the HS or NMFC code for international shipments
9
+ attr_reader :commodity_code
10
+
11
+ # @return [String] the country of origin for international shipments
12
+ attr_reader :country_of_origin
14
13
 
14
+ # @param commodity_code [String] the HS or NMFC code for international shipments
15
+ # @param country_of_origin [String] the country of origin for international shipments
16
+ # @param kwargs [Hash]
17
+ # @option kwargs [String] :item_id the ID for the item that belongs to these options
15
18
  def initialize(
16
19
  commodity_code: nil,
17
20
  country_of_origin: nil,
@@ -1,32 +1,44 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/shipment_options'
4
- require 'friendly_shipping/services/ship_engine/label_customs_options'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  class ShipEngine
9
- # Options for generating ShipEngine labels
10
- #
11
- # @attribute label_format [Symbol] The format for the label. Possible Values: :png, :zpl and :pdf. Default :pdf
12
- # @attribute label_download_type [Symbol] Whether to download directly (`:inline`) or
13
- # obtain a URL to the label (`:url`). Default :url
14
- # @attribute label_image_id [String] Identifier for image uploaded to ShipEngine. Default: nil
15
- # @attribute package_options [Enumberable<LabelPackageOptions>] Package options for the packages in the shipment
16
- #
6
+ # Options for generating shipping labels.
17
7
  class LabelOptions < FriendlyShipping::ShipmentOptions
18
- attr_reader :shipping_method,
19
- :label_download_type,
20
- :label_format,
21
- :label_image_id,
22
- :customs_options
8
+ # @return [ShippingMethod] the label's shipping method
9
+ attr_reader :shipping_method
10
+
11
+ # @return [Symbol] whether to download directly or obtain a URL to the label
12
+ attr_reader :label_download_type
13
+
14
+ # @return [Symbol] the format for the label
15
+ attr_reader :label_format
16
+
17
+ # @return [String] identifier for image uploaded to ShipEngine
18
+ attr_reader :label_image_id
19
+
20
+ # @return [LabelCustomsOptions] customs options for international shipment labels
21
+ attr_reader :customs_options
22
+
23
+ # @return [Proc, #call] a callable that takes packages and an options object to create the customs items array for the shipment
24
+ attr_reader :customs_items_serializer
23
25
 
26
+ # @param shipping_method [ShippingMethod] the label's shipping method
27
+ # @param label_download_type [Symbol] whether to download directly (`:inline`) or obtain a URL to the label (`:url`). Default :url
28
+ # @param label_format [Symbol] the format for the label. Possible Values: :png, :zpl and :pdf. Default :pdf
29
+ # @param label_image_id [String] identifier for image uploaded to ShipEngine. Default: nil
30
+ # @param customs_options [LabelCustomsOptions] customs options for obtaining international shipment labels
31
+ # @param customs_items_serializer [Proc, #call] a callable that takes packages and an options object to create the customs items array for the shipment
32
+ # @param kwargs [Hash]
33
+ # @option kwargs [Array<LabelPackageOptions>] :package_options package options for the packages in the shipment
34
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
24
35
  def initialize(
25
36
  shipping_method:,
26
37
  label_download_type: :url,
27
38
  label_format: :pdf,
28
39
  label_image_id: nil,
29
40
  customs_options: LabelCustomsOptions.new,
41
+ customs_items_serializer: CustomsItemsSerializer,
30
42
  **kwargs
31
43
  )
32
44
  @shipping_method = shipping_method
@@ -34,7 +46,8 @@ module FriendlyShipping
34
46
  @label_format = label_format
35
47
  @label_image_id = label_image_id
36
48
  @customs_options = customs_options
37
- super(**kwargs.merge(package_options_class: LabelPackageOptions))
49
+ @customs_items_serializer = customs_items_serializer
50
+ super(**kwargs.reverse_merge(package_options_class: LabelPackageOptions))
38
51
  end
39
52
  end
40
53
  end