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,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/shipment_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class RL
8
+ class ShipmentOptions < FriendlyShipping::ShipmentOptions
9
+ # @param structure_options [Array<StructureOptions>]
10
+ # @param structure_options_class [Class]
11
+ # @param kwargs [Hash]
12
+ # @option kwargs [Array<PackageOptions>] :package_options
13
+ # @option kwargs [Class] :package_options_class
14
+ def initialize(
15
+ structure_options: Set.new,
16
+ structure_options_class: StructureOptions,
17
+ **kwargs
18
+ )
19
+ @structure_options = structure_options
20
+ @structure_options_class = structure_options_class
21
+ super(**kwargs.reverse_merge(package_options_class: PackageOptions))
22
+ end
23
+
24
+ # @param [#id] structure
25
+ # @return [StructureOptions]
26
+ def options_for_structure(structure)
27
+ structure_options.detect do |structure_option|
28
+ structure_option.structure_id == structure.id
29
+ end || structure_options_class.new(structure_id: nil)
30
+ end
31
+
32
+ private
33
+
34
+ # @return [Array<StructureOptions>]
35
+ attr_reader :structure_options
36
+
37
+ # @return [Class]
38
+ attr_reader :structure_options_class
39
+
40
+ # @return [Array<PackageOptions>]
41
+ # @deprecated Use {#structures_serializer} instead.
42
+ attr_reader :package_options
43
+
44
+ # @return [Class]
45
+ # @deprecated Use {#structures_serializer_class} instead.
46
+ attr_reader :package_options_class
47
+ end
48
+ end
49
+ end
50
+ 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,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads'
4
+ require 'friendly_shipping/http_client'
5
+ require 'friendly_shipping/services/rl/api_error'
6
+ require 'friendly_shipping/services/rl/parse_create_bol_response'
7
+ require 'friendly_shipping/services/rl/parse_invoice_response'
8
+ require 'friendly_shipping/services/rl/parse_print_bol_response'
9
+ require 'friendly_shipping/services/rl/parse_print_shipping_labels_response'
10
+ require 'friendly_shipping/services/rl/parse_rate_quote_response'
11
+ require 'friendly_shipping/services/rl/parse_transit_times_response'
12
+ require 'friendly_shipping/services/rl/serialize_create_bol_request'
13
+ require 'friendly_shipping/services/rl/serialize_location'
14
+ require 'friendly_shipping/services/rl/serialize_rate_quote_request'
15
+ require 'friendly_shipping/services/rl/serialize_transit_times_request'
16
+ require 'friendly_shipping/services/rl/rate_quote_options'
17
+ require 'friendly_shipping/services/rl/bol_options'
18
+ require 'friendly_shipping/services/rl/structure_options'
19
+ require 'friendly_shipping/services/rl/package_options'
20
+ require 'friendly_shipping/services/rl/item_options'
21
+
22
+ module FriendlyShipping
23
+ module Services
24
+ # API service class for R+L Carriers, a freight/LTL carrier.
25
+ # @see https://www.rlcarriers.com/freight/shipping-software/api R+L API docs
26
+ class RL
27
+ include Dry::Monads::Result::Mixin
28
+
29
+ # @return [String] the API key for this carrier
30
+ attr_reader :api_key
31
+
32
+ # @return [Boolean] whether to use test API endpoints
33
+ attr_reader :test
34
+
35
+ # @return [HttpClient] the HTTP client to use for requests
36
+ attr_reader :client
37
+
38
+ # The API base URL for production.
39
+ LIVE_API_BASE = "https://api.rlc.com/"
40
+
41
+ # The API base URL for testing.
42
+ TEST_API_BASE = "https://apisandbox.rlc.com/"
43
+
44
+ # This carrier's API paths. Used when constructing endpoint URLs.
45
+ API_PATHS = {
46
+ bill_of_lading: "BillOfLading",
47
+ documents: "DocumentRetrieval",
48
+ print_bol: "BillOfLading/PrintBOL",
49
+ print_shipping_labels: "BillOfLading/PrintShippingLabels",
50
+ rate_quote: "RateQuote",
51
+ transit_times: "TransitTimes"
52
+ }.freeze
53
+
54
+ # @param [String] api_key the API key for this carrier
55
+ # @param [Boolean] test whether to use test API endpoints
56
+ # @param [HttpClient] client the HTTP client to use for requests
57
+ def initialize(api_key:, test: true, client: nil)
58
+ @api_key = api_key
59
+ @test = test
60
+
61
+ error_handler = ApiErrorHandler.new(api_error_class: RL::ApiError)
62
+ @client = client || HttpClient.new(error_handler: error_handler)
63
+ end
64
+
65
+ # Create an LTL Bill of Lading (BOL) and schedule a pickup with R+L Carriers.
66
+ #
67
+ # @param shipment [Physical::Shipment] the shipment for the BOL
68
+ # @param options [BOLOptions] the options for the BOL
69
+ # @return [Success<ApiResult<ShipmentInformation>>, Failure<ApiFailure>] the BOL from R+L Carriers
70
+ def create_bill_of_lading(shipment, options:, debug: false)
71
+ request = FriendlyShipping::Request.new(
72
+ url: api_base + API_PATHS[:bill_of_lading],
73
+ http_method: "POST",
74
+ body: SerializeCreateBOLRequest.call(shipment: shipment, options: options).to_json,
75
+ headers: request_headers,
76
+ debug: debug
77
+ )
78
+ client.post(request).bind do |response|
79
+ ParseCreateBOLResponse.call(request: request, response: response)
80
+ end
81
+ end
82
+
83
+ # Retrieve an existing binary LTL Bill of Lading (BOL) document for printing. The BOL is appended
84
+ # to the {ShipmentInformation} object's documents array.
85
+ #
86
+ # @param shipment_info [ShipmentInformation] the shipment for the BOL
87
+ # @return [Success<ApiResult<ShipmentDocument>>, Failure<ApiFailure>] the binary BOL document from R+L Carriers
88
+ def print_bill_of_lading(shipment_info, debug: false)
89
+ request = FriendlyShipping::Request.new(
90
+ url: api_base + API_PATHS[:print_bol] + "?ProNumber=#{shipment_info.pro_number}",
91
+ http_method: "GET",
92
+ headers: request_headers,
93
+ debug: debug
94
+ )
95
+ client.get(request).bind do |response|
96
+ ParsePrintBOLResponse.call(request: request, response: response).bind do |api_result|
97
+ shipment_info.documents << api_result.data
98
+ Success(api_result)
99
+ end
100
+ end
101
+ end
102
+
103
+ # Retrieve binary LTL shipping label documents for printing. The label documents are appended
104
+ # to the {ShipmentInformation} object's documents array.
105
+ #
106
+ # @param shipment_info [ShipmentInformation] the shipment for the labels
107
+ # @param style [Integer] the R+L Carriers shipping label style (between 1 and 13)
108
+ # @see https://rl-cdn.com/docs/rlc/shipping-forms/shipping-label-select.pdf Shipping label styles
109
+ # @param start_position [Integer] the R+L Carriers start position for the first label (between 1 and 10)
110
+ # @param num_labels [Integer] number of labels to print (between 1 and 100)
111
+ # @return [Success<ApiResult<ShipmentDocument>>, Failure<ApiFailure>] the binary shipping labels from R+L Carriers
112
+ def print_shipping_labels(shipment_info, style: 1, start_position: 1, num_labels: 4, debug: false)
113
+ request = FriendlyShipping::Request.new(
114
+ url: api_base + API_PATHS[:print_shipping_labels] +
115
+ "?ProNumber=#{shipment_info.pro_number}&" \
116
+ "Style=#{style}&" \
117
+ "StartPosition=#{start_position}&" \
118
+ "NumberOfLabels=#{num_labels}",
119
+ http_method: "GET",
120
+ headers: request_headers,
121
+ debug: debug
122
+ )
123
+ client.get(request).bind do |response|
124
+ ParsePrintShippingLabelsResponse.call(request: request, response: response).bind do |api_result|
125
+ shipment_info.documents << api_result.data
126
+ Success(api_result)
127
+ end
128
+ end
129
+ end
130
+
131
+ # Request an LTL shipping rate quote from R+L Carriers.
132
+ #
133
+ # @param shipment [Physical::Shipment] the shipment to quote
134
+ # @param options [RateQuoteOptions] the options for the quote
135
+ #
136
+ # @return [Result<ApiResult<Array<Rate>>>] the rate quote from R+L Carriers
137
+ def rate_quote(shipment, options:, debug: false)
138
+ request = FriendlyShipping::Request.new(
139
+ url: LIVE_API_BASE + API_PATHS[:rate_quote],
140
+ http_method: "POST",
141
+ body: SerializeRateQuoteRequest.call(shipment: shipment, options: options).to_json,
142
+ headers: request_headers,
143
+ debug: debug
144
+ )
145
+ client.post(request).bind do |response|
146
+ ParseRateQuoteResponse.call(request: request, response: response)
147
+ end
148
+ end
149
+
150
+ # Request an LTL shipment transit timing from R+L Carriers.
151
+ #
152
+ # @param shipment [Physical::Shipment] the shipment we're timing
153
+ # @param options [RateQuoteOptions] the options for the timing
154
+ #
155
+ # @return [Result<ApiResult<Array<Timing>>>] the transit timing from R+L Carriers
156
+ def transit_times(shipment, options:, debug: false)
157
+ request = FriendlyShipping::Request.new(
158
+ url: LIVE_API_BASE + API_PATHS[:transit_times],
159
+ http_method: "POST",
160
+ body: SerializeTransitTimesRequest.call(shipment: shipment, options: options).to_json,
161
+ headers: request_headers,
162
+ debug: debug
163
+ )
164
+ client.post(request).bind do |response|
165
+ ParseTransitTimesResponse.call(request: request, response: response)
166
+ end
167
+ end
168
+
169
+ # Retrieve an existing binary Invoice
170
+ #
171
+ # @param [String] pro_number The PRO number for the Invoice
172
+ #
173
+ # @return [Result<ApiResult<ShipmentDocument>>] The binary Invoice document from R&L
174
+ def get_invoice(pro_number, debug: false)
175
+ request = FriendlyShipping::Request.new(
176
+ url: api_base + API_PATHS[:documents] + "?ProNumber=#{pro_number}&DocumentTypes=Invoice&MediaType=PDF",
177
+ http_method: "GET",
178
+ headers: request_headers,
179
+ debug: debug
180
+ )
181
+ client.get(request).bind do |response|
182
+ ParseInvoiceResponse.call(request: request, response: response)
183
+ end
184
+ end
185
+
186
+ private
187
+
188
+ # Returns the content type and API key as a headers hash.
189
+ # @return [Hash]
190
+ def request_headers
191
+ {
192
+ content_type: :json,
193
+ apiKey: api_key
194
+ }
195
+ end
196
+
197
+ # Returns the API base URL based on the {test} attribute's value.
198
+ # @return [String]
199
+ def api_base
200
+ test ? TEST_API_BASE : LIVE_API_BASE
201
+ end
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/api_error'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class ShipEngine
8
+ # Raised when an API error is returned.
9
+ class ApiError < FriendlyShipping::ApiError
10
+ # @param cause [RestClient::Exception] the cause of the error
11
+ def initialize(cause)
12
+ super(cause, parse_message(cause))
13
+ end
14
+
15
+ private
16
+
17
+ # Parses the message from the error response.
18
+ #
19
+ # @param error [RestClient::Exception] the cause of the error
20
+ # @return [String, nil] the message or nil if it couldn't be parsed
21
+ def parse_message(error)
22
+ return error.message unless error.response && error.http_code == 400
23
+
24
+ parsed_body = JSON.parse(error.response.body)
25
+ messages = parsed_body.fetch('errors')&.map { |e| e.fetch('message') }
26
+ messages&.join(', ')
27
+ rescue JSON::ParserError, KeyError => _e
28
+ nil
29
+ end
30
+ end
31
+ end
32
+ end
33
+ 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"
@@ -5,13 +5,18 @@ require 'friendly_shipping/item_options'
5
5
  module FriendlyShipping
6
6
  module Services
7
7
  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.
8
+ # Item options for generating shipping labels.
11
9
  class LabelItemOptions < FriendlyShipping::ItemOptions
12
- attr_reader :commodity_code,
13
- :country_of_origin
10
+ # @return [String] the HS or NMFC code for international shipments
11
+ attr_reader :commodity_code
14
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
15
20
  def initialize(
16
21
  commodity_code: nil,
17
22
  country_of_origin: nil,
@@ -2,31 +2,47 @@
2
2
 
3
3
  require 'friendly_shipping/shipment_options'
4
4
  require 'friendly_shipping/services/ship_engine/label_customs_options'
5
+ require 'friendly_shipping/services/ship_engine/customs_items_serializer'
5
6
 
6
7
  module FriendlyShipping
7
8
  module Services
8
9
  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
- #
10
+ # Options for generating shipping labels.
17
11
  class LabelOptions < FriendlyShipping::ShipmentOptions
18
- attr_reader :shipping_method,
19
- :label_download_type,
20
- :label_format,
21
- :label_image_id,
22
- :customs_options
12
+ # @return [ShippingMethod] the label's shipping method
13
+ attr_reader :shipping_method
23
14
 
15
+ # @return [Symbol] whether to download directly or obtain a URL to the label
16
+ attr_reader :label_download_type
17
+
18
+ # @return [Symbol] the format for the label
19
+ attr_reader :label_format
20
+
21
+ # @return [String] identifier for image uploaded to ShipEngine
22
+ attr_reader :label_image_id
23
+
24
+ # @return [LabelCustomsOptions] customs options for international shipment labels
25
+ attr_reader :customs_options
26
+
27
+ # @return [Proc, #call] a callable that takes packages and an options object to create the customs items array for the shipment
28
+ attr_reader :customs_items_serializer
29
+
30
+ # @param shipping_method [ShippingMethod] the label's shipping method
31
+ # @param label_download_type [Symbol] whether to download directly (`:inline`) or obtain a URL to the label (`:url`). Default :url
32
+ # @param label_format [Symbol] the format for the label. Possible Values: :png, :zpl and :pdf. Default :pdf
33
+ # @param label_image_id [String] identifier for image uploaded to ShipEngine. Default: nil
34
+ # @param customs_options [LabelCustomsOptions] customs options for obtaining international shipment labels
35
+ # @param customs_items_serializer [Proc, #call] a callable that takes packages and an options object to create the customs items array for the shipment
36
+ # @param kwargs [Hash]
37
+ # @option kwargs [Array<LabelPackageOptions>] :package_options package options for the packages in the shipment
38
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
24
39
  def initialize(
25
40
  shipping_method:,
26
41
  label_download_type: :url,
27
42
  label_format: :pdf,
28
43
  label_image_id: nil,
29
44
  customs_options: LabelCustomsOptions.new,
45
+ customs_items_serializer: CustomsItemsSerializer,
30
46
  **kwargs
31
47
  )
32
48
  @shipping_method = shipping_method
@@ -34,7 +50,8 @@ module FriendlyShipping
34
50
  @label_format = label_format
35
51
  @label_image_id = label_image_id
36
52
  @customs_options = customs_options
37
- super(**kwargs.merge(package_options_class: LabelPackageOptions))
53
+ @customs_items_serializer = customs_items_serializer
54
+ super(**kwargs.reverse_merge(package_options_class: LabelPackageOptions))
38
55
  end
39
56
  end
40
57
  end
@@ -6,22 +6,29 @@ require 'friendly_shipping/services/ship_engine/label_item_options'
6
6
  module FriendlyShipping
7
7
  module Services
8
8
  class ShipEngine
9
- # Options relating to packages in the ShipEngine labels call
10
- #
11
- # @attribute :package_code [Symbol] The type of package. Possible types can be gotten
12
- # via the ShipEngine API: https://www.shipengine.com/docs/reference/list-carrier-packages/
13
- # If a package type is given, no dimensions will be added to the call (as we can assume the
14
- # carrier knows the dimensions of their packaging types).
15
- # @attribute messages [Array<String>] A list of messages to add to the label. No carrier accepts
16
- # more than three messages, and some have restrictions on how many characters are possible.
17
- # We're not validating here though.
9
+ # Package options for generating shipping labels.
18
10
  class LabelPackageOptions < FriendlyShipping::PackageOptions
19
- attr_reader :package_code, :messages
11
+ # @return [Symbol] the type of package
12
+ attr_reader :package_code
20
13
 
14
+ # @return [Array<String>] a list of messages to add to the label
15
+ attr_reader :messages
16
+
17
+ # @param package_code [Symbol] The type of package. Possible types can be gotten
18
+ # via the ShipEngine API: https://www.shipengine.com/docs/reference/list-carrier-packages/
19
+ # If a package type is given, no dimensions will be added to the call (as we can assume the
20
+ # carrier knows the dimensions of their packaging types).
21
+ # @param messages [Array<String>] A list of messages to add to the label. No carrier accepts
22
+ # more than three messages, and some have restrictions on how many characters are possible.
23
+ # We're not validating here though.
24
+ # @param kwargs [Hash]
25
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
26
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
27
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
21
28
  def initialize(package_code: nil, messages: [], **kwargs)
22
29
  @package_code = package_code
23
30
  @messages = messages
24
- super(**kwargs.merge(item_options_class: LabelItemOptions))
31
+ super(**kwargs.reverse_merge(item_options_class: LabelItemOptions))
25
32
  end
26
33
  end
27
34
  end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class ShipEngine
8
+ class ParseAddressValidationResponse
9
+ extend Dry::Monads::Result::Mixin
10
+
11
+ class << self
12
+ # @param [FriendlyShipping::Request] request
13
+ # @param [FriendlyShipping::Response] response
14
+ # @return [Success<ApiResult<Array<Physical::Location>>>, Failure<ApiFailure>]
15
+ def call(request:, response:)
16
+ parsed_json = JSON.parse(response.body)
17
+ if parsed_json.first['status'] == "error"
18
+ errors = parsed_json.first['messages'].map { |message| message['message'] }.join(", ")
19
+ Failure(
20
+ ApiFailure.new(
21
+ errors,
22
+ original_request: request,
23
+ original_response: response
24
+ )
25
+ )
26
+ else
27
+ locations = parsed_json.map do |result|
28
+ address = result['matched_address']
29
+ Physical::Location.new(
30
+ name: address['name'],
31
+ email: address['email'],
32
+ phone: address['phone'],
33
+ company_name: address['company_name'],
34
+ address1: address['address_line1'],
35
+ address2: address['address_line2'],
36
+ address3: address['address_line3'],
37
+ city: address['city_locality'],
38
+ zip: address['postal_code'],
39
+ region: address['state_province'],
40
+ country: address['country_code'],
41
+ address_type: address_type(address)
42
+ )
43
+ end
44
+ Success(
45
+ ApiResult.new(
46
+ locations,
47
+ original_request: request,
48
+ original_response: response
49
+ )
50
+ )
51
+ end
52
+ rescue JSON::ParserError => e
53
+ Failure(
54
+ FriendlyShipping::ApiFailure.new(
55
+ e,
56
+ original_request: request,
57
+ original_response: response
58
+ )
59
+ )
60
+ end
61
+
62
+ private
63
+
64
+ # @param address [Hash]
65
+ # @return [String]
66
+ def address_type(address)
67
+ case address['address_residential_indicator']
68
+ when "yes" then "residential"
69
+ when "no" then "commercial"
70
+ else "unknown"
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -5,8 +5,12 @@ require 'json'
5
5
  module FriendlyShipping
6
6
  module Services
7
7
  class ShipEngine
8
+ # Parses the carriers API response.
8
9
  class ParseCarrierResponse
9
10
  class << self
11
+ # @param request [Request] the request to attach to the API result
12
+ # @param response [Response] the response to parse
13
+ # @return [ApiResult<Array<Carrier>] the parsed carriers
10
14
  def call(request:, response:)
11
15
  parsed_json = JSON.parse(response.body)
12
16
  carriers = parsed_json['carriers'].map do |carrier_data|
@@ -35,6 +39,11 @@ module FriendlyShipping
35
39
 
36
40
  private
37
41
 
42
+ # Parses and returns a ShipEngine shipping method from the carriers API response.
43
+ #
44
+ # @param carrier [Carrier] the carrier for the shipping method
45
+ # @param shipping_method_data [Hash] the data for the shipping method
46
+ # @return [ShippingMethod] the parsed shipping method
38
47
  def parse_shipping_method(carrier, shipping_method_data)
39
48
  FriendlyShipping::ShippingMethod.new(
40
49
  carrier: carrier,
@@ -5,7 +5,11 @@ require 'json'
5
5
  module FriendlyShipping
6
6
  module Services
7
7
  class ShipEngine
8
+ # Parses the labels API response.
8
9
  class ParseLabelResponse
10
+ # @param request [Request] the request to attach to the API result
11
+ # @param response [Response] the response to parse
12
+ # @return [ApiResult<Array<Label>] the parsed labels (ShipEngine only returns one label at a time)
9
13
  def self.call(request:, response:)
10
14
  parsed_json = JSON.parse(response.body)
11
15