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,27 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/package_options'
4
- require 'friendly_shipping/services/ship_engine/label_item_options'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  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.
6
+ # Package options for generating shipping labels.
18
7
  class LabelPackageOptions < FriendlyShipping::PackageOptions
19
- attr_reader :package_code, :messages
8
+ # @return [Symbol] the type of package
9
+ attr_reader :package_code
10
+
11
+ # @return [Array<String>] a list of messages to add to the label
12
+ attr_reader :messages
20
13
 
14
+ # @param package_code [Symbol] The type of package. Possible types can be gotten
15
+ # via the ShipEngine API: https://www.shipengine.com/docs/reference/list-carrier-packages/
16
+ # If a package type is given, no dimensions will be added to the call (as we can assume the
17
+ # carrier knows the dimensions of their packaging types).
18
+ # @param messages [Array<String>] A list of messages to add to the label. No carrier accepts
19
+ # more than three messages, and some have restrictions on how many characters are possible.
20
+ # We're not validating here though.
21
+ # @param kwargs [Hash]
22
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
23
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
24
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
21
25
  def initialize(package_code: nil, messages: [], **kwargs)
22
26
  @package_code = package_code
23
27
  @messages = messages
24
- super(**kwargs.merge(item_options_class: LabelItemOptions))
28
+ super(**kwargs.reverse_merge(item_options_class: LabelItemOptions))
25
29
  end
26
30
  end
27
31
  end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ class ParseAddressValidationResponse
7
+ extend Dry::Monads::Result::Mixin
8
+
9
+ class << self
10
+ # @param [FriendlyShipping::Request] request
11
+ # @param [FriendlyShipping::Response] response
12
+ # @return [Success<ApiResult<Array<Physical::Location>>>, Failure<ApiResult>]
13
+ def call(request:, response:)
14
+ parsed_json = JSON.parse(response.body)
15
+ if parsed_json.first['status'] == "error"
16
+ errors = parsed_json.first['messages'].map { |message| message['message'] }.join(", ")
17
+ Failure(
18
+ ApiResult.new(
19
+ errors,
20
+ original_request: request,
21
+ original_response: response
22
+ )
23
+ )
24
+ else
25
+ locations = parsed_json.map do |result|
26
+ address = result['matched_address']
27
+ Physical::Location.new(
28
+ name: address['name'],
29
+ email: address['email'],
30
+ phone: address['phone'],
31
+ company_name: address['company_name'],
32
+ address1: address['address_line1'],
33
+ address2: address['address_line2'],
34
+ address3: address['address_line3'],
35
+ city: address['city_locality'],
36
+ zip: address['postal_code'],
37
+ region: address['state_province'],
38
+ country: address['country_code'],
39
+ address_type: address_type(address)
40
+ )
41
+ end
42
+ Success(
43
+ ApiResult.new(
44
+ locations,
45
+ original_request: request,
46
+ original_response: response
47
+ )
48
+ )
49
+ end
50
+ rescue JSON::ParserError => e
51
+ Failure(
52
+ FriendlyShipping::ApiResult.new(
53
+ e,
54
+ original_request: request,
55
+ original_response: response
56
+ )
57
+ )
58
+ end
59
+
60
+ private
61
+
62
+ # @param address [Hash]
63
+ # @return [String]
64
+ def address_type(address)
65
+ case address['address_residential_indicator']
66
+ when "yes" then "residential"
67
+ when "no" then "commercial"
68
+ else "unknown"
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class ShipEngine
6
+ # Parses the carriers API response.
8
7
  class ParseCarrierResponse
9
8
  class << self
9
+ # @param request [Request] the request to attach to the API result
10
+ # @param response [Response] the response to parse
11
+ # @return [ApiResult<Array<Carrier>] the parsed carriers
10
12
  def call(request:, response:)
11
13
  parsed_json = JSON.parse(response.body)
12
14
  carriers = parsed_json['carriers'].map do |carrier_data|
@@ -35,6 +37,11 @@ module FriendlyShipping
35
37
 
36
38
  private
37
39
 
40
+ # Parses and returns a ShipEngine shipping method from the carriers API response.
41
+ #
42
+ # @param carrier [Carrier] the carrier for the shipping method
43
+ # @param shipping_method_data [Hash] the data for the shipping method
44
+ # @return [ShippingMethod] the parsed shipping method
38
45
  def parse_shipping_method(carrier, shipping_method_data)
39
46
  FriendlyShipping::ShippingMethod.new(
40
47
  carrier: carrier,
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class ShipEngine
6
+ # Parses the labels API response.
8
7
  class ParseLabelResponse
8
+ # @param request [Request] the request to attach to the API result
9
+ # @param response [Response] the response to parse
10
+ # @return [ApiResult<Array<Label>] the parsed labels (ShipEngine only returns one label at a time)
9
11
  def self.call(request:, response:)
10
12
  parsed_json = JSON.parse(response.body)
11
13
 
@@ -1,14 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class ShipEngine
8
- class ParseRateEstimateResponse
6
+ # Parses the rate estimates API response.
7
+ class ParseRateEstimatesResponse
9
8
  extend Dry::Monads::Result::Mixin
10
9
 
11
10
  class << self
11
+ # @param request [Request] the request to attach to the API result
12
+ # @param response [Response] the response to parse
13
+ # @param options [RateEstimatesOptions] the options to use when parsing
14
+ # @return [Success<ApiResult<Array<Rate>>>, Failure<ApiResult<Array<String>>>] the parsed rate estimates or errors
12
15
  def call(response:, request:, options:)
13
16
  error_messages = []
14
17
  parsed_json = JSON.parse(response.body)
@@ -23,6 +26,7 @@ module FriendlyShipping
23
26
 
24
27
  shipping_method = FriendlyShipping::ShippingMethod.new(
25
28
  carrier: carrier,
29
+ name: rate['service_type'],
26
30
  service_code: rate['service_code']
27
31
  )
28
32
 
@@ -31,9 +35,19 @@ module FriendlyShipping
31
35
  shipping_method: shipping_method,
32
36
  amounts: amounts,
33
37
  remote_service_id: rate['rate_id'],
34
- delivery_date: Time.parse(rate['estimated_delivery_date']),
38
+ pickup_date: rate['ship_date'] && Time.parse(rate['ship_date']),
39
+ delivery_date: rate['estimated_delivery_date'] && Time.parse(rate['estimated_delivery_date']),
40
+ guaranteed: rate['guaranteed_service'],
35
41
  warnings: rate['warning_messages'],
36
- errors: rate['error_messages']
42
+ errors: rate['error_messages'],
43
+ data: {
44
+ package_type: rate['package_type'],
45
+ delivery_days: rate['delivery_days'],
46
+ carrier_delivery_days: rate['carrier_delivery_days'],
47
+ negotiated_rate: rate['negotiated_rate'],
48
+ trackable: rate['trackable'],
49
+ validation_status: rate['validation_status']
50
+ }
37
51
  )
38
52
  end.compact
39
53
 
@@ -47,7 +61,7 @@ module FriendlyShipping
47
61
  )
48
62
  else
49
63
  Failure(
50
- ApiFailure.new(
64
+ ApiResult.new(
51
65
  error_messages,
52
66
  original_request: request,
53
67
  original_response: response
@@ -58,21 +72,25 @@ module FriendlyShipping
58
72
 
59
73
  private
60
74
 
75
+ # @param parsed_json [Hash] the parsed JSON
76
+ # @return [Boolean] whether the rate estimates are valid
61
77
  def valid_rates(parsed_json)
62
78
  parsed_json.map do |rate|
63
- ["valid", "has_warnings", "unknown"].include? rate['validation_status']
79
+ %w[valid has_warnings unknown].include? rate['validation_status']
64
80
  end.any?
65
81
  end
66
82
 
83
+ # @param rate_hash [Hash] the rate hash
84
+ # @return [Hash<Symbol,Money>] the amounts in the hash
67
85
  def get_amounts(rate_hash)
68
- [:shipping, :other, :insurance, :confirmation].map do |name|
86
+ [:shipping, :other, :insurance, :confirmation].to_h do |name|
69
87
  currency = Money::Currency.new(rate_hash["#{name}_amount"]["currency"])
70
88
  amount = rate_hash["#{name}_amount"]["amount"] * currency.subunit_to_unit
71
89
  [
72
90
  name,
73
91
  Money.new(amount, currency)
74
92
  ]
75
- end.to_h
93
+ end
76
94
  end
77
95
  end
78
96
  end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ # Parses the rates API response.
7
+ class ParseRatesResponse
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
+ # @param options [RatesOptions] the options to use when parsing
14
+ # @return [Success<ApiResult<Array<Rate>>>, Failure<ApiResult<String>>] the parsed rates or error
15
+ def call(request:, response:, options:)
16
+ parsed_json = JSON.parse(response.body)
17
+ rates = build_rates(parsed_json, options)
18
+ if rates.any?
19
+ Success(
20
+ ApiResult.new(
21
+ rates,
22
+ original_request: request,
23
+ original_response: response
24
+ )
25
+ )
26
+ else
27
+ error = parsed_json.dig("rate_response", "errors", 0, "message") || "Unknown error"
28
+ Failure(
29
+ ApiResult.new(
30
+ error,
31
+ original_request: request,
32
+ original_response: response
33
+ )
34
+ )
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ # @param parsed_json [String] the parsed JSON
41
+ # @param options [RateOptions] the options to use when parsing
42
+ # @return [Array<Rate>] the parsed rates
43
+ def build_rates(parsed_json, options)
44
+ returned_rates = parsed_json.dig('rate_response', 'rates')
45
+ return [] unless returned_rates
46
+
47
+ returned_rates.map do |rate|
48
+ if rate['validation_status'] == 'invalid'
49
+ error_messages.concat rate['error_messages']
50
+ next
51
+ end
52
+
53
+ carrier = options.carriers.detect { |c| c.id == rate['carrier_id'] }
54
+ next unless carrier
55
+
56
+ shipping_method = FriendlyShipping::ShippingMethod.new(
57
+ carrier: carrier,
58
+ service_code: rate['service_code']
59
+ )
60
+
61
+ # validate service code is available/matches shipping method?
62
+
63
+ amounts = get_amounts(rate)
64
+ FriendlyShipping::Rate.new(
65
+ shipping_method: shipping_method,
66
+ amounts: amounts,
67
+ remote_service_id: rate['rate_id'],
68
+ delivery_date: rate['estimated_delivery_date'] && Time.parse(rate['estimated_delivery_date']),
69
+ warnings: rate['warning_messages'],
70
+ errors: rate['error_messages'],
71
+ data: {
72
+ package_type: rate['package_type'],
73
+ delivery_days: rate['delivery_days'],
74
+ carrier_delivery_days: rate['carrier_delivery_days'],
75
+ negotiated_rate: rate['negotiated_rate'],
76
+ trackable: rate['trackable'],
77
+ validation_status: rate['validation_status']
78
+ }
79
+ )
80
+ end.compact
81
+ end
82
+
83
+ # @param rate_hash [Hash] the rate hash
84
+ # @return [Hash<Symbol,Money>] the amounts in the hash
85
+ def get_amounts(rate_hash)
86
+ [:shipping, :other, :insurance, :confirmation].to_h do |name|
87
+ currency = Money::Currency.new(rate_hash["#{name}_amount"]["currency"])
88
+ amount = rate_hash["#{name}_amount"]["amount"] * currency.subunit_to_unit
89
+ [
90
+ name,
91
+ Money.new(amount, currency)
92
+ ]
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -3,9 +3,13 @@
3
3
  module FriendlyShipping
4
4
  module Services
5
5
  class ShipEngine
6
+ # Parses the API response for voiding a label.
6
7
  class ParseVoidResponse
7
8
  extend Dry::Monads::Result::Mixin
8
9
 
10
+ # @param request [Request] the request to attach to the API result
11
+ # @param response [Response] the response to parse
12
+ # @return [Success<ApiResult<String>>, Failure<ApiResult<String>>] the success or failure message
9
13
  def self.call(request:, response:)
10
14
  parsed_json = JSON.parse(response.body)
11
15
  approved, message = parsed_json["approved"], parsed_json["message"]
@@ -15,7 +19,7 @@ module FriendlyShipping
15
19
  )
16
20
  else
17
21
  Failure(
18
- ApiFailure.new(message, original_request: request, original_response: response)
22
+ ApiResult.new(message, original_request: request, original_response: response)
19
23
  )
20
24
  end
21
25
  end
@@ -1,21 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/shipment_options'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class ShipEngine
8
- # options for the rate estimates call
9
- #
10
- # @attribute carriers [Array<FriendlyShipping::Carrier] a list of the carriers we want to get IDs from.
6
+ # Options for the rate estimates API call.
11
7
  class RateEstimatesOptions < ShipmentOptions
8
+ # @return [Array<Carrier>] the carriers for these rate estimates
12
9
  attr_reader :carriers
13
10
 
14
- def initialize(carriers:, **kwargs)
11
+ # @return [#strftime]
12
+ attr_reader :ship_date
13
+
14
+ # @param carriers [Array<Carrier] the carriers for these rate estimates
15
+ # @param ship_date [#strftime] the date we want to ship on
16
+ # @param kwargs [Hash]
17
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
18
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
19
+ def initialize(
20
+ carriers:,
21
+ ship_date: Date.today,
22
+ **kwargs
23
+ )
15
24
  @carriers = carriers
25
+ @ship_date = ship_date
16
26
  super(**kwargs)
17
27
  end
18
28
 
29
+ # @return [Array<String>] the carrier IDs for these rate estimates
19
30
  def carrier_ids
20
31
  carriers.map(&:id)
21
32
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ # Options for each item passed in the rates API call.
7
+ class RatesItemOptions < FriendlyShipping::ItemOptions
8
+ # @return [String] the HS or NMFC code for international shipments
9
+ attr_reader :commodity_code
10
+
11
+ # @return [String] the country of origin for international shipments
12
+ attr_reader :country_of_origin
13
+
14
+ # @param commodity_code [String] the HS or NMFC code for international shipments
15
+ # @param country_of_origin [String] the country of origin for international shipments
16
+ # @param kwargs [Hash]
17
+ # @option kwargs [String] :item_id the ID for the item that belongs to these options
18
+ def initialize(commodity_code: nil, country_of_origin: nil, **kwargs)
19
+ @commodity_code = commodity_code
20
+ @country_of_origin = country_of_origin
21
+ super(**kwargs)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ # Options for the rates API call.
7
+ class RatesOptions < FriendlyShipping::ShipmentOptions
8
+ # @return [Array<Carrier>] the carriers from which to get rates
9
+ attr_reader :carriers
10
+
11
+ # @return [String] the service code for which to get rates
12
+ attr_reader :service_code
13
+
14
+ # @return [#strftime]
15
+ attr_reader :ship_date
16
+
17
+ # @return [String]
18
+ attr_reader :comparison_rate_type
19
+
20
+ # @param carriers [Array<Carrier>] the carriers for these rates
21
+ # @param service_code [String] the service code to use when getting rates
22
+ # @param ship_date [#strftime] the date we want to ship on
23
+ # @param comparison_rate_type [String] set to "retail" for retail rates (UPS/USPS only)
24
+ # @param kwargs [Hash]
25
+ # @option kwargs [Array<PackageOptions>] :package_options the options for packages in this shipment
26
+ # @option kwargs [Class] :package_options_class the class to use for package options when none are provided
27
+ def initialize(
28
+ carriers:,
29
+ service_code:,
30
+ ship_date: Date.today,
31
+ comparison_rate_type: nil,
32
+ **kwargs
33
+ )
34
+ @carriers = carriers
35
+ @service_code = service_code
36
+ @ship_date = ship_date
37
+ @comparison_rate_type = comparison_rate_type
38
+ validate_comparison_rate_type!
39
+ super(**kwargs.reverse_merge(package_options_class: RatesPackageOptions))
40
+ end
41
+
42
+ # @return [Array<String>] the carrier IDs for these rates
43
+ def carrier_ids
44
+ carriers.map(&:id)
45
+ end
46
+
47
+ private
48
+
49
+ # @raise [ArgumentError] invalid comparison rate type
50
+ # @return [nil]
51
+ def validate_comparison_rate_type!
52
+ return if comparison_rate_type.nil? || comparison_rate_type == "retail"
53
+
54
+ raise ArgumentError, "Invalid comparison rate type: #{comparison_rate_type}"
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ # Options for each package passed in the rates API call.
7
+ class RatesPackageOptions < FriendlyShipping::PackageOptions
8
+ # @param kwargs [Hash]
9
+ # @option kwargs [String] :package_id the ID for the package that belongs to these options
10
+ # @option kwargs [Array<ItemOptions>] :item_options the options for items in this package
11
+ # @option kwargs [Class] :item_options_class the class to use for item options when none are provided
12
+ def initialize(**kwargs)
13
+ super(**kwargs.reverse_merge(item_options_class: RatesItemOptions))
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ class SerializeAddressResidentialIndicator
7
+ class << self
8
+ # @param location [Physical::Location]
9
+ # @return [Hash]
10
+ def call(location)
11
+ { address_residential_indicator: residential_indicator(location) }
12
+ end
13
+
14
+ private
15
+
16
+ # @param location [Physical::Location]
17
+ # @return [String]
18
+ def residential_indicator(location)
19
+ return "unknown" if location&.address_type.nil?
20
+
21
+ location.address_type == "residential" ? "yes" : "no"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class ShipEngine
6
+ class SerializeAddressValidationRequest
7
+ class << self
8
+ # @param location [Physical::Location]
9
+ # @return [Array<Hash>]
10
+ def call(location:)
11
+ [
12
+ {
13
+ name: location.name,
14
+ phone: location.phone,
15
+ email: location.email,
16
+ company_name: location.company_name,
17
+ address_line1: location.address1,
18
+ address_line2: location.address2,
19
+ address_line3: location.address3,
20
+ city_locality: location.city,
21
+ state_province: location.region&.code,
22
+ postal_code: location.zip,
23
+ country_code: location.country&.code
24
+ }.merge(SerializeAddressResidentialIndicator.call(location))
25
+ ]
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end