friendly_shipping 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) 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 +11 -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/friendly_shipping.gemspec +4 -13
  13. data/lib/friendly_shipping/api_error.rb +6 -4
  14. data/lib/friendly_shipping/api_error_handler.rb +10 -6
  15. data/lib/friendly_shipping/api_failure.rb +4 -0
  16. data/lib/friendly_shipping/api_result.rb +16 -4
  17. data/lib/friendly_shipping/carrier.rb +28 -8
  18. data/lib/friendly_shipping/http_client.rb +25 -2
  19. data/lib/friendly_shipping/inflections.rb +10 -0
  20. data/lib/friendly_shipping/item_options.rb +3 -0
  21. data/lib/friendly_shipping/label.rb +41 -20
  22. data/lib/friendly_shipping/package_options.rb +21 -2
  23. data/lib/friendly_shipping/rate.rb +50 -15
  24. data/lib/friendly_shipping/request.rb +23 -7
  25. data/lib/friendly_shipping/response.rb +21 -6
  26. data/lib/friendly_shipping/services/rl/api_error.rb +33 -0
  27. data/lib/friendly_shipping/services/rl/bol_options.rb +107 -0
  28. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  29. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  30. data/lib/friendly_shipping/services/rl/item_options.rb +38 -0
  31. data/lib/friendly_shipping/services/rl/package_options.rb +40 -0
  32. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +46 -0
  33. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +50 -0
  34. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +47 -0
  35. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +66 -0
  37. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +76 -0
  38. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +86 -0
  39. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  41. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  42. data/lib/friendly_shipping/services/rl/serialize_location.rb +46 -0
  43. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  44. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  45. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  46. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  47. data/lib/friendly_shipping/services/rl/shipment_options.rb +50 -0
  48. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  49. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  50. data/lib/friendly_shipping/services/rl.rb +204 -0
  51. data/lib/friendly_shipping/services/ship_engine/api_error.rb +33 -0
  52. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  53. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  54. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -5
  55. data/lib/friendly_shipping/services/ship_engine/label_options.rb +31 -14
  56. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -11
  57. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +77 -0
  58. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -0
  59. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -0
  60. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +26 -6
  61. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +101 -0
  62. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +4 -0
  63. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -4
  64. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +28 -0
  65. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +61 -0
  66. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +20 -0
  67. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  68. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  70. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  71. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  72. data/lib/friendly_shipping/services/ship_engine.rb +94 -21
  73. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  74. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +24 -5
  75. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +39 -4
  76. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +4 -0
  77. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +10 -0
  78. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +29 -11
  79. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +7 -2
  80. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +50 -16
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  82. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  83. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl.rb +50 -30
  85. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +43 -0
  86. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +42 -0
  87. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  88. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  89. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  90. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +165 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +113 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +65 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  97. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  98. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +121 -0
  99. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +94 -0
  100. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +58 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +31 -0
  103. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  104. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +12 -0
  105. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +162 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +12 -0
  107. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  108. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +49 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  111. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  112. data/lib/friendly_shipping/services/tforce_freight.rb +202 -0
  113. data/lib/friendly_shipping/services/ups/label_options.rb +14 -2
  114. data/lib/friendly_shipping/services/ups/label_package_options.rb +1 -1
  115. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  116. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +12 -0
  117. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  118. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -1
  119. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -1
  120. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  121. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +2 -1
  122. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  123. data/lib/friendly_shipping/services/ups.rb +1 -1
  124. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -5
  125. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  126. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +3 -20
  127. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +2 -20
  128. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  129. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  130. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  131. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -2
  132. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -1
  133. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  134. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +31 -21
  135. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -8
  136. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +46 -0
  137. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  138. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +49 -0
  139. data/lib/friendly_shipping/services/ups_freight.rb +3 -1
  140. data/lib/friendly_shipping/services/ups_json/access_token.rb +29 -0
  141. data/lib/friendly_shipping/services/ups_json/api_error.rb +29 -0
  142. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  143. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  144. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +211 -0
  145. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  146. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +86 -0
  147. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  148. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  149. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  150. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +77 -0
  151. data/lib/friendly_shipping/services/ups_json/label_options.rb +177 -0
  152. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +51 -0
  153. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  154. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +44 -0
  155. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  156. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  157. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  158. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +105 -0
  159. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  160. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  161. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +22 -0
  162. data/lib/friendly_shipping/services/ups_json/rates_options.rb +113 -0
  163. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  164. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  165. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  166. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  167. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  168. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  169. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +6 -6
  170. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +6 -6
  171. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -1
  172. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -1
  173. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -2
  174. data/lib/friendly_shipping/services/usps/shipping_methods.rb +4 -3
  175. data/lib/friendly_shipping/services/usps.rb +1 -1
  176. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +3 -3
  177. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -1
  178. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +1 -1
  179. data/lib/friendly_shipping/services/usps_international.rb +1 -1
  180. data/lib/friendly_shipping/services/usps_ship/access_token.rb +37 -0
  181. data/lib/friendly_shipping/services/usps_ship/api_error.rb +29 -0
  182. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +53 -0
  183. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  184. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +80 -0
  185. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  186. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +124 -0
  187. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +55 -0
  188. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  189. data/lib/friendly_shipping/services/{ship_engine_ltl/bad_request.rb → usps_ship/timing_options.rb} +2 -2
  190. data/lib/friendly_shipping/services/usps_ship.rb +199 -0
  191. data/lib/friendly_shipping/shipment_options.rb +13 -1
  192. data/lib/friendly_shipping/shipping_method.rb +38 -11
  193. data/lib/friendly_shipping/structure_options.rb +38 -0
  194. data/lib/friendly_shipping/timing.rb +42 -7
  195. data/lib/friendly_shipping/version.rb +1 -1
  196. data/lib/friendly_shipping.rb +6 -0
  197. metadata +140 -174
  198. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  199. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
  200. data/lib/friendly_shipping/services/ship_engine_ltl/bad_request_handler.rb +0 -33
@@ -1,28 +1,54 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyShipping
4
+ # Base class for a shipping rate returned by a carrier API.
4
5
  class Rate
6
+ # Indicates a shipping rate has no amounts.
5
7
  class NoAmountsGiven < StandardError; end
6
- attr_reader :shipping_method,
7
- :amounts,
8
- :remote_service_id,
9
- :delivery_date,
10
- :warnings,
11
- :errors,
12
- :data
13
-
14
- # @param [FriendlyShipping::ShippingMethod] shipping_method The rate's shipping method
15
- # @param [Hash] amounts The amounts (as Money objects) that make up the rate
16
- # @param [Integer] remote_service_id The remote service ID for the rate
17
- # @param [Time] delivery_date The delivery date for the rate
18
- # @param [Array] warnings Any warnings that were generated
19
- # @param [Array] errors Any errors that were generated
20
- # @param [Hash] data Additional data related to the rate
8
+
9
+ # @return [ShippingMethod] the rate's shipping method
10
+ attr_reader :shipping_method
11
+
12
+ # @return [Hash{String,Symbol=>Money}] the amounts that make up the rate
13
+ attr_reader :amounts
14
+
15
+ # @return [Integer] the remote service ID for the rate
16
+ attr_reader :remote_service_id
17
+
18
+ # @return [Time] the pickup date for the rate
19
+ attr_reader :pickup_date
20
+
21
+ # @return [Time] the delivery date for the rate
22
+ attr_reader :delivery_date
23
+
24
+ # @return [Boolean] whether the delivery date is guaranteed
25
+ attr_reader :guaranteed
26
+
27
+ # @return [Array] any warnings that were generated while getting this rate
28
+ attr_reader :warnings
29
+
30
+ # @return [Array] any errors that were generated while getting this rate
31
+ attr_reader :errors
32
+
33
+ # @return [Hash] additional data related to the rate
34
+ attr_reader :data
35
+
36
+ # @param shipping_method [ShippingMethod] the rate's shipping method
37
+ # @param amounts [Hash{String,Symbol=>Money}] the amounts that make up the rate
38
+ # @param remote_service_id [Integer] the remote service ID for the rate
39
+ # @param pickup_date [Time] the pickup date for the rate
40
+ # @param delivery_date [Time] the delivery date for the rate
41
+ # @param guaranteed [Boolean] whether the delivery date is guaranteed
42
+ # @param warnings [Array] any warnings that were generated while getting this rate
43
+ # @param errors [Array] any errors that were generated while getting this rate
44
+ # @param data [Hash] additional data related to the rate
21
45
  def initialize(
22
46
  shipping_method:,
23
47
  amounts:,
24
48
  remote_service_id: nil,
49
+ pickup_date: nil,
25
50
  delivery_date: nil,
51
+ guaranteed: false,
26
52
  warnings: [],
27
53
  errors: [],
28
54
  data: {}
@@ -30,12 +56,21 @@ module FriendlyShipping
30
56
  @remote_service_id = remote_service_id
31
57
  @shipping_method = shipping_method
32
58
  @amounts = amounts
59
+ @pickup_date = pickup_date
33
60
  @delivery_date = delivery_date
61
+ @guaranteed = guaranteed
34
62
  @warnings = warnings
35
63
  @errors = errors
36
64
  @data = data
37
65
  end
38
66
 
67
+ # Make this class interchangeable with FriendlyShipping::Timing
68
+ alias_method :pickup, :pickup_date
69
+ alias_method :delivery, :delivery_date
70
+
71
+ # Sums all the amounts in the rate, returning the total amount.
72
+ # @return [Money]
73
+ # @raise [NoAmountsGiven] if the rate has no amounts
39
74
  def total_amount
40
75
  raise NoAmountsGiven if amounts.empty?
41
76
 
@@ -1,15 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyShipping
4
+ # Represents an HTTP request sent to a carrier API.
4
5
  class Request
5
- attr_reader :url, :http_method, :body, :headers, :debug
6
+ # @return [String] the HTTP request URL
7
+ attr_reader :url
6
8
 
7
- # @param [String] url The HTTP request URL
8
- # @param [String] http_method The HTTP request method
9
- # @param [String] body The HTTP request body
10
- # @param [String] readable_body Human-readable HTTP request body
11
- # @param [Hash] headers The HTTP request headers
12
- # @param [Boolean] debug Whether to debug the request
9
+ # @return [String] the HTTP request method
10
+ attr_reader :http_method
11
+
12
+ # @return [String] the HTTP request body
13
+ attr_reader :body
14
+
15
+ # @return [String] human-readable HTTP request body
16
+ attr_reader :headers
17
+
18
+ # @return [Boolean] whether to attach debugging information to the response
19
+ attr_reader :debug
20
+
21
+ # @param url [String] the HTTP request URL
22
+ # @param http_method [String] the HTTP request method
23
+ # @param body [String] the HTTP request body
24
+ # @param readable_body [String] human-readable HTTP request body
25
+ # @param headers [Hash] the HTTP request headers
26
+ # @param debug [Boolean] whether to attach debugging information to the response
13
27
  def initialize(url:, http_method: nil, body: nil, readable_body: nil, headers: {}, debug: false)
14
28
  @url = url
15
29
  @http_method = http_method
@@ -19,6 +33,8 @@ module FriendlyShipping
19
33
  @debug = debug
20
34
  end
21
35
 
36
+ # Returns the HTTP request body.
37
+ # @return [String] human-readable HTTP request body
22
38
  def readable_body
23
39
  @readable_body.presence || @body
24
40
  end
@@ -1,12 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyShipping
4
+ # Represents an HTTP response received from a carrier API.
4
5
  class Response
5
- attr_reader :status, :body, :headers
6
+ # @return [Integer] the HTTP response status code
7
+ attr_reader :status
6
8
 
7
- # @param [Integer] status The HTTP response status code
8
- # @param [String] body The HTTP response body
9
- # @param [Hash] headers The HTTP response headers
9
+ # @return [String] the HTTP response body
10
+ attr_reader :body
11
+
12
+ # @return [Hash] the HTTP response headers
13
+ attr_reader :headers
14
+
15
+ # @param status [Integer] the HTTP response status code
16
+ # @param body [String] the HTTP response body
17
+ # @param headers [Hash] the HTTP response headers
10
18
  def initialize(status:, body:, headers:)
11
19
  @status = status
12
20
  @body = body
@@ -15,13 +23,16 @@ module FriendlyShipping
15
23
 
16
24
  alias_method :code, :status
17
25
 
18
- # @param [RestClient::Response] response
19
- # @return [FriendlyShipping::Response]
26
+ # Constructs a new {Response} from a `RestClient::Response` object.
27
+ # @param response [RestClient::Response] the response to use
28
+ # @return [Response]
20
29
  def self.new_from_rest_client_response(response)
21
30
  new(status: response&.code, body: response&.body, headers: response&.headers)
22
31
  end
23
32
 
33
+ # Returns true if the given object shares the same class and attributes with this response.
24
34
  # @param [Object] other
35
+ # @return [Boolean]
25
36
  def ==(other)
26
37
  other.class == self.class &&
27
38
  other.attributes == attributes
@@ -29,12 +40,16 @@ module FriendlyShipping
29
40
 
30
41
  alias_method :eql?, :==
31
42
 
43
+ # Returns this response's attributes as a hash.
44
+ # @return [Hash]
32
45
  def hash
33
46
  attributes.hash
34
47
  end
35
48
 
36
49
  protected
37
50
 
51
+ # Returns the status, body, and headers from this response.
52
+ # @return [Array]
38
53
  def attributes
39
54
  [status, body, headers]
40
55
  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 RL
8
+ # Raised when an R+L 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('ErrorMessage') }
26
+ messages&.join(', ')
27
+ rescue JSON::ParserError, KeyError => _e
28
+ nil
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/rl/shipment_options'
4
+ require 'friendly_shipping/services/rl/bol_structures_serializer'
5
+ require 'friendly_shipping/services/rl/bol_packages_serializer'
6
+
7
+ module FriendlyShipping
8
+ module Services
9
+ class RL
10
+ # Bill of Lading (BOL) options class. Used when serializing R+L API requests.
11
+ class BOLOptions < ShipmentOptions
12
+ # @return [Range] the pickup time window
13
+ attr_reader :pickup_time_window
14
+
15
+ # @return [String] the pickup instructions
16
+ attr_reader :pickup_instructions
17
+
18
+ # @return [Numeric] the declared value of this shipment
19
+ attr_reader :declared_value
20
+
21
+ # @return [String] any special instructions
22
+ attr_reader :special_instructions
23
+
24
+ # @return [Hash] reference numbers for the shipment
25
+ attr_reader :reference_numbers
26
+
27
+ # @return [Array<String>] additional service codes
28
+ attr_reader :additional_service_codes
29
+
30
+ # @return [Boolean] whether to generate universal PRO number
31
+ attr_reader :generate_universal_pro
32
+
33
+ # @return [Callable] the structures serializer
34
+ attr_reader :structures_serializer
35
+
36
+ # @return [Callable]
37
+ # @deprecated Use {#structures_serializer} instead.
38
+ attr_reader :packages_serializer
39
+
40
+ # @param pickup_time_window [Range]
41
+ # @param pickup_instructions [String]
42
+ # @param declared_value [Numeric]
43
+ # @param special_instructions [String]
44
+ # @param reference_numbers [Hash]
45
+ # @param additional_service_codes [Array<String>]
46
+ # @param generate_universal_pro [Boolean]
47
+ # @param structures_serializer [Callable] a callable that takes structures
48
+ # and an options object to create an Array of item hashes per the R+L Carriers docs
49
+ # @param packages_serializer [Callable] a callable that takes packages
50
+ # and an options object to create an Array of item hashes per the R+L Carriers docs (DEPRECATED: use `structures_serializer` instead)
51
+ # @param kwargs [Hash]
52
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
53
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
54
+ def initialize(
55
+ pickup_time_window:,
56
+ pickup_instructions: nil,
57
+ declared_value: nil,
58
+ special_instructions: nil,
59
+ reference_numbers: {},
60
+ additional_service_codes: [],
61
+ structures_serializer: BOLStructuresSerializer,
62
+ packages_serializer: BOLPackagesSerializer,
63
+ generate_universal_pro: false,
64
+ **kwargs
65
+ )
66
+ @pickup_time_window = pickup_time_window
67
+ @pickup_instructions = pickup_instructions
68
+ @declared_value = declared_value
69
+ @special_instructions = special_instructions
70
+ @reference_numbers = reference_numbers
71
+ @additional_service_codes = additional_service_codes
72
+ @structures_serializer = structures_serializer
73
+ @packages_serializer = packages_serializer
74
+ @generate_universal_pro = generate_universal_pro
75
+ validate_additional_service_codes!
76
+ super(**kwargs)
77
+ end
78
+
79
+ # Optional service codes that can be used for R+L shipments.
80
+ ADDITIONAL_SERVICE_CODES = %w[
81
+ OriginLiftgate
82
+ DestinationLiftgate
83
+ InsidePickup
84
+ InsideDelivery
85
+ LimitedAccessPickup
86
+ LimitedAccessDelivery
87
+ Freezable
88
+ DeliveryAppointment
89
+ ].freeze
90
+
91
+ private
92
+
93
+ # Raises an exception if the additional service codes passed into the initializer
94
+ # don't correspond to valid codes from {ADDITIONAL_SERVICE_CODES}.
95
+ #
96
+ # @raise [ArgumentError] invalid additional service codes
97
+ # @return [nil]
98
+ def validate_additional_service_codes!
99
+ invalid_codes = (additional_service_codes - ADDITIONAL_SERVICE_CODES)
100
+ return unless invalid_codes.any?
101
+
102
+ raise ArgumentError, "Invalid additional service code(s): #{invalid_codes.join(',')}"
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Serializes packages for R+L BOL API requests.
7
+ class BOLPackagesSerializer
8
+ # @param packages [Array<Physical::Package>] packages to serialize
9
+ # @param options [BOLOptions] options for these packages
10
+ # @return [Array<Hash>] serialized packages
11
+ def self.call(packages:, options:)
12
+ packages.flat_map do |package|
13
+ package_options = options.options_for_package(package)
14
+ package.items.map do |item|
15
+ item_options = package_options.options_for_item(item)
16
+ {
17
+ IsHazmat: false,
18
+ Pieces: 1,
19
+ PackageType: "BOX",
20
+ NMFCItemNumber: item_options.nmfc_primary_code,
21
+ NMFCSubNumber: item_options.nmfc_sub_code,
22
+ Class: item_options.freight_class,
23
+ Weight: item.weight.convert_to(:pounds).value.ceil,
24
+ Description: item.description.presence || "Commodities"
25
+ }.compact
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ class BOLStructuresSerializer
7
+ # @param [Array<Physical::Structure>] structures
8
+ # @param [FriendlyShipping::Services::RL::BOLOptions] options
9
+ # @return [Array<Hash>]
10
+ def self.call(structures:, options:)
11
+ structures.flat_map do |structure|
12
+ structure_options = options.options_for_structure(structure)
13
+ structure.packages.map do |package|
14
+ package_options = structure_options.options_for_package(package)
15
+ {
16
+ IsHazmat: false,
17
+ Pieces: 1,
18
+ PackageType: "BOX",
19
+ NMFCItemNumber: package_options.nmfc_primary_code,
20
+ NMFCSubNumber: package_options.nmfc_sub_code,
21
+ Class: package_options.freight_class,
22
+ Weight: package.weight.convert_to(:pounds).value.ceil,
23
+ Description: package.description.presence || "Commodities"
24
+ }.compact
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/item_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class RL
8
+ # Serializes items for R+L API requests.
9
+ class ItemOptions < FriendlyShipping::ItemOptions
10
+ # @return [String] the freight class
11
+ attr_reader :freight_class
12
+
13
+ # @return [String] the NMFC primary code
14
+ attr_reader :nmfc_primary_code
15
+
16
+ # @return [String] the NMFC sub code
17
+ attr_reader :nmfc_sub_code
18
+
19
+ # @param freight_class [String] the freight class
20
+ # @param nmfc_primary_code [String] the NMFC primary code
21
+ # @param nmfc_sub_code [String] the NMFC sub code
22
+ # @param kwargs [Hash]
23
+ # @option kwargs [String] :item_id the ID for the item that belongs to these options
24
+ def initialize(
25
+ freight_class: nil,
26
+ nmfc_primary_code: nil,
27
+ nmfc_sub_code: nil,
28
+ **kwargs
29
+ )
30
+ @freight_class = freight_class
31
+ @nmfc_primary_code = nmfc_primary_code
32
+ @nmfc_sub_code = nmfc_sub_code
33
+ super(**kwargs)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/package_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class RL
8
+ # Serializes packages for R+L API requests.
9
+ class PackageOptions < FriendlyShipping::PackageOptions
10
+ # @return [String]
11
+ attr_reader :freight_class
12
+
13
+ # @return [String]
14
+ attr_reader :nmfc_primary_code
15
+
16
+ # @return [String]
17
+ attr_reader :nmfc_sub_code
18
+
19
+ # @param freight_class [String] the freight class
20
+ # @param nmfc_primary_code [String] the NMFC primary code
21
+ # @param nmfc_sub_code [String] the NMFC sub code
22
+ # @param kwargs [Hash]
23
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
24
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
25
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
26
+ def initialize(
27
+ freight_class: nil,
28
+ nmfc_primary_code: nil,
29
+ nmfc_sub_code: nil,
30
+ **kwargs
31
+ )
32
+ @freight_class = freight_class
33
+ @nmfc_primary_code = nmfc_primary_code
34
+ @nmfc_sub_code = nmfc_sub_code
35
+ super(**kwargs.reverse_merge(item_options_class: ItemOptions))
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'friendly_shipping/services/rl/shipment_information'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class RL
9
+ # Parses the response from the R+L API when creating a Bill of Lading (BOL).
10
+ class ParseCreateBOLResponse
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 [Result<ApiResult<ShipmentInformation>>] shipment info with the BOL document attached
17
+ def call(request:, response:)
18
+ parsed_json = JSON.parse(response.body)
19
+ shipment_info = ShipmentInformation.new(
20
+ pro_number: parsed_json['ProNumber'],
21
+ pickup_request_number: parsed_json['PickupRequestNumber']
22
+ )
23
+ if shipment_info.valid?
24
+ Success(
25
+ ApiResult.new(
26
+ shipment_info,
27
+ original_request: request,
28
+ original_response: response
29
+ )
30
+ )
31
+ else
32
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
33
+ Failure(
34
+ ApiResult.new(
35
+ errors.map { |e| e['ErrorMessage'] },
36
+ original_request: request,
37
+ original_response: response
38
+ )
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'friendly_shipping/services/rl/shipment_document'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class RL
9
+ class ParseInvoiceResponse
10
+ extend Dry::Monads::Result::Mixin
11
+
12
+ class << self
13
+ # @param [FriendlyShipping::Request] request
14
+ # @param [FriendlyShipping::Response] response
15
+ # @return [Result<ApiResult<ShipmentDocument>>]
16
+ def call(request:, response:)
17
+ parsed_json = JSON.parse(response.body)
18
+ data = (parsed_json['Documents'].find { _1["Type"] == "Invoice" } || {})["Data"]
19
+
20
+ document = ShipmentDocument.new(binary: data, format: :pdf, document_type: :invoice)
21
+ if document.valid?
22
+ Success(
23
+ ApiResult.new(
24
+ document,
25
+ original_request: request,
26
+ original_response: response
27
+ )
28
+ )
29
+ else
30
+ errors = if parsed_json["Messages"].present?
31
+ parsed_json["Messages"]
32
+ else
33
+ parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }]).
34
+ map { _1['ErrorMessage'] }
35
+ end
36
+
37
+ Failure(
38
+ ApiResult.new(
39
+ errors,
40
+ original_request: request,
41
+ original_response: response
42
+ )
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'friendly_shipping/services/rl/shipment_document'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class RL
9
+ # Parses the response from the R+L API when printing a Bill of Lading (BOL).
10
+ class ParsePrintBOLResponse
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 [Result<ApiResult<ShipmentDocument>>] shipment document containing the BOL
17
+ def call(request:, response:)
18
+ parsed_json = JSON.parse(response.body)
19
+ bol_document = ShipmentDocument.new(
20
+ format: :pdf,
21
+ document_type: :rl_bol,
22
+ binary: Base64.decode64(parsed_json['BolDocument'])
23
+ )
24
+ if bol_document.valid?
25
+ Success(
26
+ ApiResult.new(
27
+ bol_document,
28
+ original_request: request,
29
+ original_response: response
30
+ )
31
+ )
32
+ else
33
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
34
+ Failure(
35
+ ApiResult.new(
36
+ errors.map { |e| e['ErrorMessage'] },
37
+ original_request: request,
38
+ original_response: response
39
+ )
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'friendly_shipping/services/rl/shipment_document'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class RL
9
+ # Parses the response from the R+L API when printing shipping labels.
10
+ class ParsePrintShippingLabelsResponse
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 [Result<ApiResult<ShipmentDocument>>] shipping document containing the labels
17
+ def call(request:, response:)
18
+ parsed_json = JSON.parse(response.body)
19
+ label_doc = ShipmentDocument.new(
20
+ format: :pdf,
21
+ document_type: :label,
22
+ binary: Base64.decode64(parsed_json['ShippingLabelsFile'])
23
+ )
24
+ if label_doc.valid?
25
+ Success(
26
+ ApiResult.new(
27
+ label_doc,
28
+ original_request: request,
29
+ original_response: response
30
+ )
31
+ )
32
+ else
33
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
34
+ Failure(
35
+ ApiResult.new(
36
+ errors.map { |e| e['ErrorMessage'] },
37
+ original_request: request,
38
+ original_response: response
39
+ )
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end