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
@@ -1,29 +1,48 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/types'
4
-
5
3
  module FriendlyShipping
4
+ # Base class for a shipping label returned by a carrier API.
6
5
  class Label
7
- attr_reader :id,
8
- :shipment_id,
9
- :tracking_number,
10
- :service_code,
11
- :label_href,
12
- :data,
13
- :label_format,
14
- :cost,
15
- :shipment_cost,
16
- :label_data
17
-
18
- # @param [Integer] id The label's ID
19
- # @param [Integer] shipment_id The label's shipment ID
20
- # @param [String] tracking_number The label's tracking number
21
- # @param [String] service_code The label's service code
22
- # @param [String] label_href The URL for the label
23
- # @param [String] label_format The label's format
24
- # @param [String] label_data The raw label data
25
- # @param [Float] shipment_cost The cost of the shipment
26
- # @param [Hash] data Additional data related to the label
6
+ # @return [String] the label's unique ID
7
+ attr_reader :id
8
+
9
+ # @return [String] the label's shipment ID
10
+ attr_reader :shipment_id
11
+
12
+ # @return [String] the label's tracking number
13
+ attr_reader :tracking_number
14
+
15
+ # @return [String] the label's service code
16
+ attr_reader :service_code
17
+
18
+ # @return [String] the URL for the label
19
+ attr_reader :label_href
20
+
21
+ # @return [String] the label's format
22
+ attr_reader :label_format
23
+
24
+ # @return [String] the raw label data
25
+ attr_reader :label_data
26
+
27
+ # @return [Money] the label's cost
28
+ attr_reader :cost
29
+
30
+ # @return [Money] the overall cost of the shipment
31
+ attr_reader :shipment_cost
32
+
33
+ # @return [Hash] additional data related to the label
34
+ attr_reader :data
35
+
36
+ # @param id [String] the label's unique ID
37
+ # @param shipment_id [String] the label's shipment ID
38
+ # @param tracking_number [String] the label's tracking number
39
+ # @param service_code [String] the label's service code
40
+ # @param label_href [String] the URL for the label
41
+ # @param label_format [String] the label's format
42
+ # @param label_data [String] the raw label data
43
+ # @param cost [Money] the label's cost
44
+ # @param shipment_cost [Money] the overall cost of the shipment
45
+ # @param data [Hash] additional data related to the label
27
46
  def initialize(
28
47
  id: nil,
29
48
  shipment_id: nil,
@@ -1,9 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyShipping
4
+ # Base class for package options. Used when serializing API requests.
4
5
  class PackageOptions
6
+ # @return [String] the ID for the package that belongs to these options
5
7
  attr_reader :package_id
6
8
 
9
+ # @param package_id [String] the ID for the package that belongs to these options
10
+ # @param item_options [Array<ItemOptions>] the options for items in this package
11
+ # @param item_options_class [Class] the class to use for item options when none are provided
7
12
  def initialize(
8
13
  package_id:,
9
14
  item_options: Set.new,
@@ -14,6 +19,11 @@ module FriendlyShipping
14
19
  @item_options_class = item_options_class
15
20
  end
16
21
 
22
+ # Finds and returns item options for the given item. If options cannot be
23
+ # found, the `item_options_class` is used to construct new options.
24
+ #
25
+ # @param item [#id] the item for which to get options
26
+ # @return [ItemOptions]
17
27
  def options_for_item(item)
18
28
  item_options.detect do |item_option|
19
29
  item_option.item_id == item.id
@@ -22,9 +32,18 @@ module FriendlyShipping
22
32
 
23
33
  private
24
34
 
25
- attr_reader :item_options,
26
- :item_options_class
35
+ # @return [Array<ItemOptions>]
36
+ attr_reader :item_options
27
37
 
38
+ # @return [Class]
39
+ attr_reader :item_options_class
40
+
41
+ # Attempts to find a hash value for the given key. If no value is found, the
42
+ # default value is returned.
43
+ #
44
+ # @param key [Symbol] the key to use
45
+ # @param default [Object] the default value
46
+ # @param kwargs [Hash] the hash to search
28
47
  def value_or_default(key, default, kwargs)
29
48
  kwargs.key?(key) ? kwargs.delete(key) : default
30
49
  end
@@ -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,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Raised when an R+L 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('ErrorMessage') }
24
+ messages&.join(', ')
25
+ rescue JSON::ParserError, KeyError => _e
26
+ nil
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Bill of Lading (BOL) options class. Used when serializing R+L API requests.
7
+ class BOLOptions < ShipmentOptions
8
+ # @return [Range] the pickup time window
9
+ attr_reader :pickup_time_window
10
+
11
+ # @return [String] the pickup instructions
12
+ attr_reader :pickup_instructions
13
+
14
+ # @return [Numeric] the declared value of this shipment
15
+ attr_reader :declared_value
16
+
17
+ # @return [String] any special instructions
18
+ attr_reader :special_instructions
19
+
20
+ # @return [Hash] reference numbers for the shipment
21
+ attr_reader :reference_numbers
22
+
23
+ # @return [Array<String>] additional service codes
24
+ attr_reader :additional_service_codes
25
+
26
+ # @return [Boolean] whether to generate universal PRO number
27
+ attr_reader :generate_universal_pro
28
+
29
+ # @return [Callable] the structures serializer
30
+ attr_reader :structures_serializer
31
+
32
+ # @return [Callable]
33
+ # @deprecated Use {#structures_serializer} instead.
34
+ attr_reader :packages_serializer
35
+
36
+ # @param pickup_time_window [Range]
37
+ # @param pickup_instructions [String]
38
+ # @param declared_value [Numeric]
39
+ # @param special_instructions [String]
40
+ # @param reference_numbers [Hash]
41
+ # @param additional_service_codes [Array<String>]
42
+ # @param generate_universal_pro [Boolean]
43
+ # @param structures_serializer [Callable] a callable that takes structures
44
+ # and an options object to create an Array of item hashes per the R+L Carriers docs
45
+ # @param packages_serializer [Callable] a callable that takes packages
46
+ # and an options object to create an Array of item hashes per the R+L Carriers docs (DEPRECATED: use `structures_serializer` instead)
47
+ # @param kwargs [Hash]
48
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
49
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
50
+ def initialize(
51
+ pickup_time_window:,
52
+ pickup_instructions: nil,
53
+ declared_value: nil,
54
+ special_instructions: nil,
55
+ reference_numbers: {},
56
+ additional_service_codes: [],
57
+ structures_serializer: BOLStructuresSerializer,
58
+ packages_serializer: BOLPackagesSerializer,
59
+ generate_universal_pro: false,
60
+ **kwargs
61
+ )
62
+ @pickup_time_window = pickup_time_window
63
+ @pickup_instructions = pickup_instructions
64
+ @declared_value = declared_value
65
+ @special_instructions = special_instructions
66
+ @reference_numbers = reference_numbers
67
+ @additional_service_codes = additional_service_codes
68
+ @structures_serializer = structures_serializer
69
+ @packages_serializer = packages_serializer
70
+ @generate_universal_pro = generate_universal_pro
71
+ validate_additional_service_codes!
72
+ super(**kwargs)
73
+ end
74
+
75
+ # Optional service codes that can be used for R+L shipments.
76
+ ADDITIONAL_SERVICE_CODES = %w[
77
+ OriginLiftgate
78
+ DestinationLiftgate
79
+ InsidePickup
80
+ InsideDelivery
81
+ LimitedAccessPickup
82
+ LimitedAccessDelivery
83
+ Freezable
84
+ DeliveryAppointment
85
+ ].freeze
86
+
87
+ private
88
+
89
+ # Raises an exception if the additional service codes passed into the initializer
90
+ # don't correspond to valid codes from {ADDITIONAL_SERVICE_CODES}.
91
+ #
92
+ # @raise [ArgumentError] invalid additional service codes
93
+ # @return [nil]
94
+ def validate_additional_service_codes!
95
+ invalid_codes = (additional_service_codes - ADDITIONAL_SERVICE_CODES)
96
+ return unless invalid_codes.any?
97
+
98
+ raise ArgumentError, "Invalid additional service code(s): #{invalid_codes.join(',')}"
99
+ end
100
+ end
101
+ end
102
+ end
103
+ 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,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Serializes items for R+L API requests.
7
+ class ItemOptions < FriendlyShipping::ItemOptions
8
+ # @return [String] the freight class
9
+ attr_reader :freight_class
10
+
11
+ # @return [String] the NMFC primary code
12
+ attr_reader :nmfc_primary_code
13
+
14
+ # @return [String] the NMFC sub code
15
+ attr_reader :nmfc_sub_code
16
+
17
+ # @param freight_class [String] the freight class
18
+ # @param nmfc_primary_code [String] the NMFC primary code
19
+ # @param nmfc_sub_code [String] the NMFC sub code
20
+ # @param kwargs [Hash]
21
+ # @option kwargs [String] :item_id the ID for the item that belongs to these options
22
+ def initialize(
23
+ freight_class: nil,
24
+ nmfc_primary_code: nil,
25
+ nmfc_sub_code: nil,
26
+ **kwargs
27
+ )
28
+ @freight_class = freight_class
29
+ @nmfc_primary_code = nmfc_primary_code
30
+ @nmfc_sub_code = nmfc_sub_code
31
+ super(**kwargs)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Serializes packages for R+L API requests.
7
+ class PackageOptions < FriendlyShipping::PackageOptions
8
+ # @return [String]
9
+ attr_reader :freight_class
10
+
11
+ # @return [String]
12
+ attr_reader :nmfc_primary_code
13
+
14
+ # @return [String]
15
+ attr_reader :nmfc_sub_code
16
+
17
+ # @param freight_class [String] the freight class
18
+ # @param nmfc_primary_code [String] the NMFC primary code
19
+ # @param nmfc_sub_code [String] the NMFC sub code
20
+ # @param kwargs [Hash]
21
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
22
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
23
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
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.reverse_merge(item_options_class: ItemOptions))
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class RL
6
+ # Parses the response from the R+L API when creating a Bill of Lading (BOL).
7
+ class ParseCreateBOLResponse
8
+ extend Dry::Monads::Result::Mixin
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 [Result<ApiResult<ShipmentInformation>>] shipment info with the BOL document attached
14
+ def call(request:, response:)
15
+ parsed_json = JSON.parse(response.body)
16
+ shipment_info = ShipmentInformation.new(
17
+ pro_number: parsed_json['ProNumber'],
18
+ pickup_request_number: parsed_json['PickupRequestNumber']
19
+ )
20
+ if shipment_info.valid?
21
+ Success(
22
+ ApiResult.new(
23
+ shipment_info,
24
+ original_request: request,
25
+ original_response: response
26
+ )
27
+ )
28
+ else
29
+ errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
30
+ Failure(
31
+ ApiResult.new(
32
+ errors.map { |e| e['ErrorMessage'] },
33
+ original_request: request,
34
+ original_response: response
35
+ )
36
+ )
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end