friendly_shipping 0.8.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +3 -0
  4. data/.env.test.local.template +6 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop-relaxed.yml +7 -23
  7. data/.rubocop.yml +17 -2
  8. data/.rubocop_todo.yml +21 -0
  9. data/.yardopts +6 -0
  10. data/CHANGELOG.md +71 -0
  11. data/Gemfile +17 -0
  12. data/README.md +1 -1
  13. data/friendly_shipping.gemspec +8 -17
  14. data/lib/friendly_shipping/access_token.rb +24 -0
  15. data/lib/friendly_shipping/api_error.rb +8 -4
  16. data/lib/friendly_shipping/api_error_handler.rb +11 -9
  17. data/lib/friendly_shipping/api_failure.rb +2 -7
  18. data/lib/friendly_shipping/api_result.rb +26 -4
  19. data/lib/friendly_shipping/carrier.rb +28 -8
  20. data/lib/friendly_shipping/http_client.rb +25 -8
  21. data/lib/friendly_shipping/inflections.rb +10 -0
  22. data/lib/friendly_shipping/item_options.rb +3 -0
  23. data/lib/friendly_shipping/label.rb +41 -22
  24. data/lib/friendly_shipping/package_options.rb +21 -2
  25. data/lib/friendly_shipping/rate.rb +50 -15
  26. data/lib/friendly_shipping/request.rb +23 -7
  27. data/lib/friendly_shipping/response.rb +21 -6
  28. data/lib/friendly_shipping/services/rl/api_error.rb +31 -0
  29. data/lib/friendly_shipping/services/rl/bol_options.rb +103 -0
  30. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  31. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  32. data/lib/friendly_shipping/services/rl/item_options.rb +36 -0
  33. data/lib/friendly_shipping/services/rl/package_options.rb +38 -0
  34. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +43 -0
  35. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +44 -0
  37. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +44 -0
  38. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +93 -0
  39. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +73 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +82 -0
  41. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  42. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  43. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  44. data/lib/friendly_shipping/services/rl/serialize_location.rb +53 -0
  45. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  46. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  47. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  48. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  49. data/lib/friendly_shipping/services/rl/shipment_options.rb +48 -0
  50. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  51. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  52. data/lib/friendly_shipping/services/rl.rb +187 -0
  53. data/lib/friendly_shipping/services/ship_engine/api_error.rb +31 -0
  54. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  55. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  56. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -7
  57. data/lib/friendly_shipping/services/ship_engine/label_options.rb +30 -17
  58. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -14
  59. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +75 -0
  60. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -2
  61. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -2
  62. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +27 -9
  63. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +99 -0
  64. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +5 -1
  65. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -6
  66. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +26 -0
  67. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +59 -0
  68. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +18 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  70. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  71. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  72. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  73. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  74. data/lib/friendly_shipping/services/ship_engine.rb +89 -33
  75. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  76. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +24 -5
  77. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +39 -4
  78. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +4 -2
  79. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +10 -2
  80. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +29 -11
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +7 -2
  82. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +50 -16
  83. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  85. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  86. data/lib/friendly_shipping/services/ship_engine_ltl.rb +47 -39
  87. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +28 -0
  88. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +40 -0
  89. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  90. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +153 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +111 -0
  97. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +63 -0
  98. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  99. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  100. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +119 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +90 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  103. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +50 -0
  104. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +29 -0
  105. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +10 -0
  107. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +159 -0
  108. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +10 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  111. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +47 -0
  112. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  113. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  114. data/lib/friendly_shipping/services/tforce_freight.rb +176 -0
  115. data/lib/friendly_shipping/services/ups/label_item_options.rb +0 -2
  116. data/lib/friendly_shipping/services/ups/label_options.rb +14 -5
  117. data/lib/friendly_shipping/services/ups/label_package_options.rb +1 -3
  118. data/lib/friendly_shipping/services/ups/parse_address_validation_response.rb +1 -1
  119. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  120. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +11 -3
  121. data/lib/friendly_shipping/services/ups/parse_shipment_accept_response.rb +0 -3
  122. data/lib/friendly_shipping/services/ups/parse_shipment_confirm_response.rb +0 -2
  123. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  124. data/lib/friendly_shipping/services/ups/parse_void_shipment_response.rb +0 -2
  125. data/lib/friendly_shipping/services/ups/parse_xml_response.rb +1 -1
  126. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -3
  127. data/lib/friendly_shipping/services/ups/rate_estimate_package_options.rb +0 -2
  128. data/lib/friendly_shipping/services/ups/serialize_access_request.rb +0 -2
  129. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -5
  130. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  131. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +2 -4
  132. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  133. data/lib/friendly_shipping/services/ups.rb +2 -24
  134. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -7
  135. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  136. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +2 -23
  137. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +1 -27
  138. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  139. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  140. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  141. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -5
  142. data/lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb +0 -4
  143. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -3
  144. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  145. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +30 -24
  146. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -10
  147. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +44 -0
  148. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  149. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +47 -0
  150. data/lib/friendly_shipping/services/ups_freight.rb +2 -20
  151. data/lib/friendly_shipping/services/ups_json/access_token.rb +20 -0
  152. data/lib/friendly_shipping/services/ups_json/api_error.rb +27 -0
  153. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  154. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  155. data/lib/friendly_shipping/services/ups_json/generate_city_state_lookup_payload.rb +23 -0
  156. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +208 -0
  157. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  158. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +84 -0
  159. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  160. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  161. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  162. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +75 -0
  163. data/lib/friendly_shipping/services/ups_json/label_options.rb +174 -0
  164. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +49 -0
  165. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  166. data/lib/friendly_shipping/services/ups_json/parse_city_state_lookup_response.rb +44 -0
  167. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +51 -0
  168. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  169. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  170. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  171. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +107 -0
  172. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  173. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  174. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +20 -0
  175. data/lib/friendly_shipping/services/ups_json/rates_options.rb +111 -0
  176. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  177. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  178. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  179. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  180. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  181. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  182. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +6 -6
  183. data/lib/friendly_shipping/services/usps/parse_rate_response.rb +0 -4
  184. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +6 -9
  185. data/lib/friendly_shipping/services/usps/parse_xml_response.rb +1 -1
  186. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -3
  187. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -3
  188. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -4
  189. data/lib/friendly_shipping/services/usps/shipping_methods.rb +4 -3
  190. data/lib/friendly_shipping/services/usps.rb +2 -13
  191. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +3 -3
  192. data/lib/friendly_shipping/services/usps_international/parse_rate_response.rb +0 -3
  193. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -3
  194. data/lib/friendly_shipping/services/usps_international/rate_estimate_package_options.rb +0 -2
  195. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +1 -3
  196. data/lib/friendly_shipping/services/usps_international.rb +2 -6
  197. data/lib/friendly_shipping/services/usps_ship/access_token.rb +22 -0
  198. data/lib/friendly_shipping/services/usps_ship/api_error.rb +27 -0
  199. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +55 -0
  200. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  201. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +82 -0
  202. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  203. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +122 -0
  204. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +53 -0
  205. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  206. data/lib/friendly_shipping/services/{ship_engine_ltl/bad_request.rb → usps_ship/timing_options.rb} +2 -2
  207. data/lib/friendly_shipping/services/usps_ship.rb +185 -0
  208. data/lib/friendly_shipping/shipment_options.rb +13 -1
  209. data/lib/friendly_shipping/shipping_method.rb +38 -11
  210. data/lib/friendly_shipping/structure_options.rb +38 -0
  211. data/lib/friendly_shipping/timing.rb +42 -7
  212. data/lib/friendly_shipping/version.rb +1 -1
  213. data/lib/friendly_shipping.rb +20 -15
  214. metadata +143 -174
  215. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  216. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
  217. data/lib/friendly_shipping/services/ship_engine_ltl/bad_request_handler.rb +0 -33
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class TForceFreight
8
+ include Dry::Monads::Result::Mixin
9
+
10
+ # @return [AccessToken] the access token
11
+ attr_reader :access_token
12
+
13
+ # @return [Boolean] whether to use the test API version
14
+ attr_reader :test
15
+
16
+ # @return [HttpClient] the HTTP client
17
+ attr_reader :client
18
+
19
+ # The TForce Freight carrier
20
+ CARRIER = FriendlyShipping::Carrier.new(
21
+ id: 'tforce_freight',
22
+ name: 'TForce Freight',
23
+ code: 'tforce_freight-freight',
24
+ shipping_methods: SHIPPING_METHODS
25
+ )
26
+
27
+ # The base URL for TForce API requests
28
+ BASE_URL = 'https://api.tforcefreight.com'
29
+
30
+ # The TForce API endpoints
31
+ RESOURCES = {
32
+ rates: '/rating/getRate',
33
+ create_pickup: '/pickup/request',
34
+ create_bol: '/shipping/bol/create'
35
+ }.freeze
36
+
37
+ # @param access_token [AccessToken] the access token
38
+ # @param test [Boolean] whether to use the test API version
39
+ # @param client [HttpClient] optional HTTP client to use for requests
40
+ def initialize(access_token:, test: true, client: nil)
41
+ @access_token = access_token
42
+ @test = test
43
+
44
+ error_handler = ApiErrorHandler.new(api_error_class: TForceFreight::ApiError)
45
+ @client = client || HttpClient.new(error_handler: error_handler)
46
+ end
47
+
48
+ # @return [Array<Carrier>]
49
+ def carriers
50
+ Success([CARRIER])
51
+ end
52
+
53
+ # Creates an access token that can be used for future API requests.
54
+ # @see https://developer.tforcefreight.com/resources/integration-end-user End User Integration Guide
55
+ #
56
+ # @param token_endpoint [String] the Token Endpoint from the Application Integration section of your OAuth Client dialog
57
+ # @param client_id [String] the Client ID of your application from the OAuth Client dialog
58
+ # @param client_secret [String] the secret you created in the OAuth Client Secrets section of the OAuth Client dialog
59
+ # @param scope [String] the Scope value from the Application Integration section your OAuth Client dialog
60
+ # @param grant_type [String] defaults to "client_credentials" to indicate you wish to obtain a token representing your confidential client application
61
+ # @param debug [Boolean] whether to append debug information to the API result
62
+ # @return [ApiResult<AccessToken>] the access token
63
+ def create_access_token(
64
+ token_endpoint:,
65
+ client_id:,
66
+ client_secret:,
67
+ scope:,
68
+ grant_type: "client_credentials",
69
+ debug: false
70
+ )
71
+ request = FriendlyShipping::Request.new(
72
+ url: token_endpoint,
73
+ http_method: "POST",
74
+ body: "client_id=#{client_id}&" \
75
+ "client_secret=#{client_secret}&" \
76
+ "grant_type=#{grant_type}&" \
77
+ "scope=#{scope}",
78
+ headers: {
79
+ Content_Type: "application/x-www-form-urlencoded",
80
+ Accept: "application/json"
81
+ },
82
+ debug: debug
83
+ )
84
+ client.post(request).fmap do |response|
85
+ hash = JSON.parse(response.body)
86
+ FriendlyShipping::ApiResult.new(
87
+ AccessToken.new(
88
+ token_type: hash['token_type'],
89
+ expires_in: hash['expires_in'],
90
+ ext_expires_in: hash['ext_expires_in'],
91
+ raw_token: hash['access_token']
92
+ ),
93
+ original_request: request,
94
+ original_response: response
95
+ )
96
+ end
97
+ end
98
+
99
+ # Get rates for a shipment
100
+ # @see https://developer.tforcefreight.com/api-details#api=rating-v1&operation=get-rate API documentation
101
+ #
102
+ # @param shipment [Physical::Shipment] the shipment for which we want to get rates
103
+ # @param options [RatesOptions] options for obtaining rates for this shipment
104
+ # @param debug [Boolean] whether to append debug information to the API result
105
+ # @return [Result<ApiResult<Array<Rate>>>] the rates returned from TForce encoded in a `ApiResult` object
106
+ def rates(shipment, options:, debug: false)
107
+ freight_rate_request_hash = GenerateRatesRequestHash.call(shipment: shipment, options: options)
108
+ request = build_request(:rates, freight_rate_request_hash, debug)
109
+
110
+ client.post(request).fmap do |response|
111
+ ParseRatesResponse.call(response: response, request: request)
112
+ end
113
+ end
114
+
115
+ # Create a pickup request
116
+ # @see https://developer.tforcefreight.com/api-details#api=pickup-v1&operation=create-request API documentation
117
+ #
118
+ # @param shipment [Physical::Shipment] the shipment for which to create a pickup request
119
+ # @param options [PickupOptions] options for the pickup request
120
+ # @param debug [Boolean] whether to append debug information to the API result
121
+ # @return [Result<ApiResult>] the pickup returned from TForce encoded in a `ApiResult` object
122
+ def create_pickup(shipment, options:, debug: false)
123
+ pickup_request_hash = GeneratePickupRequestHash.call(shipment: shipment, options: options)
124
+ request = build_request(:create_pickup, pickup_request_hash, debug)
125
+
126
+ client.post(request).fmap do |response|
127
+ ParsePickupResponse.call(response: response, request: request)
128
+ end
129
+ end
130
+
131
+ # Create a Bill of Lading (BOL)
132
+ # @see https://developer.tforcefreight.com/api-details#api=shipping-cie-vnext&operation=shipping-create-bol API documentation
133
+ #
134
+ # @param shipment [Physical::Shipment] the shipment for which to create a BOL
135
+ # @param options [BOLOptions] options for the BOL
136
+ # @param debug [Boolean] whether to append debug information to the API result
137
+ # @return [Result<ApiResult>] the BOL returned from TForce encoded in a `ApiResult` object
138
+ def create_bol(shipment, options:, debug: false)
139
+ bol_request_hash = GenerateCreateBOLRequestHash.call(shipment: shipment, options: options)
140
+ request = build_request(:create_bol, bol_request_hash, debug)
141
+
142
+ client.post(request).fmap do |response|
143
+ ParseCreateBOLResponse.call(response: response, request: request)
144
+ end
145
+ end
146
+
147
+ alias_method :rate_estimates, :rates
148
+
149
+ private
150
+
151
+ # @param action [Symbol] the desired action key from {RESOURCES}
152
+ # @param payload [Hash] the payload to send to the API
153
+ # @param debug [Boolean] whether to append debug information to the API result
154
+ # @return [Request]
155
+ def build_request(action, payload, debug)
156
+ url = BASE_URL + RESOURCES[action] + "?api-version=#{api_version}"
157
+ FriendlyShipping::Request.new(
158
+ url: url,
159
+ http_method: "POST",
160
+ body: payload.to_json,
161
+ headers: {
162
+ Content_Type: "application/json",
163
+ Accept: "application/json",
164
+ Authorization: "Bearer #{access_token.raw_token}"
165
+ },
166
+ debug: debug
167
+ )
168
+ end
169
+
170
+ # @return [String] the API version to use
171
+ def api_version
172
+ test ? "cie-v1" : "v1"
173
+ end
174
+ end
175
+ end
176
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/item_options'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class Ups
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups/label_package_options'
4
- require 'friendly_shipping/services/ups/label_billing_options'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  # Option container for a generating UPS labels for a shipment
@@ -16,6 +13,7 @@ module FriendlyShipping
16
13
  # Optional:
17
14
  #
18
15
  # @param shipper [Physical::Location] The company sending the shipment. Defaults to the shipment's origin.
16
+ # @param sub_version [String] The UPS API sub-version to use for requests. Default: 1707
19
17
  # @param customer_context [String ] Optional element to identify transactions between client and server
20
18
  # @param validate_address [Boolean] Validate the city field with ZIP code and state. If false, only ZIP code
21
19
  # and state are validated. Default: true
@@ -40,6 +38,7 @@ module FriendlyShipping
40
38
  # @option reason_for_export [String] A reason to export the current shipment. Possible values: 'SALE', 'GIFT', 'SAMPLE',
41
39
  # 'RETURN', 'REPAIR', 'INTERCOMPANYDATA', Any other reason. Default: 'SALE'.
42
40
  # @option invoice_date [Date] The invoice date for the shipment
41
+ # @param declaration_statement [String ] Optional element to add customs declaration text
43
42
  #
44
43
  class Ups
45
44
  class LabelOptions < FriendlyShipping::ShipmentOptions
@@ -83,9 +82,12 @@ module FriendlyShipping
83
82
  ups_pack_collect_3_attemt_box_5: 20 # UPS Pack & Collect Service 1-Attempt Box 5
84
83
  }.freeze
85
84
 
85
+ SUB_VERSIONS = %w[1601 1607 1701 1707 1801 1807 2108 2205].freeze
86
+
86
87
  attr_reader :shipping_method,
87
88
  :shipper_number,
88
89
  :shipper,
90
+ :sub_version,
89
91
  :customer_context,
90
92
  :validate_address,
91
93
  :negotiated_rates,
@@ -97,12 +99,14 @@ module FriendlyShipping
97
99
  :carbon_neutral,
98
100
  :paperless_invoice,
99
101
  :reason_for_export,
100
- :invoice_date
102
+ :invoice_date,
103
+ :declaration_statement
101
104
 
102
105
  def initialize(
103
106
  shipping_method:,
104
107
  shipper_number:,
105
108
  shipper: nil,
109
+ sub_version: '1707',
106
110
  customer_context: nil,
107
111
  validate_address: true,
108
112
  negotiated_rates: false,
@@ -119,11 +123,15 @@ module FriendlyShipping
119
123
  reason_for_export: 'SALE',
120
124
  invoice_date: nil,
121
125
  package_options_class: LabelPackageOptions,
126
+ declaration_statement: nil,
122
127
  **kwargs
123
128
  )
129
+ raise ArgumentError, "Invalid sub-version: #{sub_version}" unless sub_version.in?(SUB_VERSIONS)
130
+
124
131
  @shipping_method = shipping_method
125
132
  @shipper_number = shipper_number
126
133
  @shipper = shipper
134
+ @sub_version = sub_version
127
135
  @customer_context = customer_context
128
136
  @validate_address = validate_address
129
137
  @negotiated_rates = negotiated_rates
@@ -139,7 +147,8 @@ module FriendlyShipping
139
147
  @terms_of_shipment = terms_of_shipment
140
148
  @reason_for_export = reason_for_export
141
149
  @invoice_date = invoice_date
142
- super(**kwargs.merge(package_options_class: package_options_class))
150
+ @declaration_statement = declaration_statement
151
+ super(**kwargs.reverse_merge(package_options_class: package_options_class))
143
152
  end
144
153
 
145
154
  def delivery_confirmation_code
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups/label_item_options'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class Ups
@@ -35,7 +33,7 @@ module FriendlyShipping
35
33
  @delivery_confirmation = delivery_confirmation
36
34
  @shipper_release = shipper_release
37
35
  @declared_value = declared_value
38
- super(**kwargs.merge(item_options_class: LabelItemOptions))
36
+ super(**kwargs.reverse_merge(item_options_class: LabelItemOptions))
39
37
  end
40
38
 
41
39
  def delivery_confirmation_code
@@ -15,7 +15,7 @@ module FriendlyShipping
15
15
  parsing_result.bind do |xml|
16
16
  if xml.at('NoCandidatesIndicator')
17
17
  Failure(
18
- FriendlyShipping::ApiFailure.new(
18
+ FriendlyShipping::ApiResult.new(
19
19
  'Address is probably invalid. No similar valid addresses found.',
20
20
  original_request: request,
21
21
  original_response: response
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class Ups
6
+ class ParseModifierElement
7
+ # @param [Nokogiri::XML::Element] element The modifier element from the source XML
8
+ # @param [String] currency_code The currency code for this modifier's amount (i.e. 'USD')
9
+ # @return [Array<String, Money>]
10
+ def self.call(element, currency_code:)
11
+ return unless element
12
+
13
+ amount = element.at('Amount').text.to_d
14
+ return if amount.zero?
15
+
16
+ currency = Money::Currency.new(currency_code)
17
+ amount = Money.new(amount * currency.subunit_to_unit, currency)
18
+
19
+ modifier_type = element.at('ModifierType').text
20
+ modifier_description = element.at('ModifierDesc').text
21
+
22
+ label = "#{modifier_type} (#{modifier_description})"
23
+
24
+ [label, amount]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups/parse_xml_response'
4
- require 'friendly_shipping/services/ups/parse_money_element'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  class Ups
@@ -68,6 +65,7 @@ module FriendlyShipping
68
65
 
69
66
  def build_packages(rated_shipment)
70
67
  rated_shipment.css('RatedPackage').map do |rated_package|
68
+ currency_code = rated_package.at('TotalCharges/CurrencyCode').text
71
69
  {
72
70
  transportation_charges: ParseMoneyElement.call(rated_package.at('TransportationCharges'))&.last,
73
71
  base_service_charge: ParseMoneyElement.call(rated_package.at('BaseServiceCharge'))&.last,
@@ -75,6 +73,7 @@ module FriendlyShipping
75
73
  itemized_charges: extract_charges(rated_package.xpath('ItemizedCharges')),
76
74
  total_charges: ParseMoneyElement.call(rated_package.at('TotalCharges'))&.last,
77
75
  negotiated_charges: extract_charges(rated_package.xpath('NegotiatedCharges/ItemizedCharges')),
76
+ rate_modifiers: extract_modifiers(rated_package.xpath('RateModifier'), currency_code: currency_code),
78
77
  weight: BigDecimal(rated_package.at('Weight').text),
79
78
  billing_weight: BigDecimal(rated_package.at('BillingWeight/Weight').text)
80
79
  }.compact
@@ -86,6 +85,15 @@ module FriendlyShipping
86
85
  ParseMoneyElement.call(element)
87
86
  end.compact.to_h
88
87
  end
88
+
89
+ # @param [Nokogiri::XML::NodeSet] node The RateModifier node set from the source XML
90
+ # @param [String] currency_code The currency code for the modifier amounts (i.e. 'USD')
91
+ # @return [Array<Hash>]
92
+ def extract_modifiers(node, currency_code:)
93
+ node.map do |element|
94
+ ParseModifierElement.call(element, currency_code: currency_code)
95
+ end.compact.to_h
96
+ end
89
97
  end
90
98
  end
91
99
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/monads'
4
- require 'friendly_shipping/services/ups/parse_money_element'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  class Ups
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/monads'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class Ups
@@ -37,7 +37,7 @@ module FriendlyShipping
37
37
  pickup: pickup,
38
38
  delivery: delivery,
39
39
  guaranteed: guaranteed,
40
- properties: {
40
+ data: {
41
41
  business_transit_days: business_transit_days
42
42
  }
43
43
  )
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/monads'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class Ups
@@ -36,7 +36,7 @@ module FriendlyShipping
36
36
 
37
37
  def wrap_failure(failure, request, response)
38
38
  Failure(
39
- FriendlyShipping::ApiFailure.new(
39
+ FriendlyShipping::ApiResult.new(
40
40
  failure,
41
41
  original_request: request,
42
42
  original_response: response
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups/rate_estimate_package_options'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  # Option container for rating a shipment via UPS
@@ -17,10 +15,13 @@ module FriendlyShipping
17
15
  # @option customer_classification [Symbol] Which kind of rates to request. See UPS docs for more details. Default: `shipper_number`
18
16
  # @option negotiated_rates [Boolean] if truthy negotiated rates will be requested from ups. Only valid if
19
17
  # shipper account has negotiated rates. Default: false
18
+ # @option pickup_type [String] UPS pickup type. See UPS docs for more details. Default: `daily_pickup`
19
+ # @option pickup_date [Time] UPS pickup date/time. Default: nil
20
20
  # @option saturday_delivery [Boolean] should we request Saturday delivery?. Default: false
21
21
  # @option saturday_pickup [Boolean] should we request Saturday pickup?. Default: false
22
22
  # @option shipping_method [FriendlyShipping::ShippingMethod] Request rates for a particular shipping method only?
23
23
  # Default is `nil`, which translates to 'All shipping methods' (The "Shop" option in UPS parlance)
24
+ # @option sub_version [String] The UPS API sub-version to use for requests. Default: 1707
24
25
  # @option with_time_in_transit [Boolean] Whether to request timing information alongside the rates
25
26
  # @option package_options_class [Class] See FriendlyShipping::ShipmentOptions
26
27
  #
@@ -45,15 +46,19 @@ module FriendlyShipping
45
46
  standard_rates: "53"
46
47
  }.freeze
47
48
 
49
+ SUB_VERSIONS = %w[1601 1607 1701 1707 1801 2108 2201 2205].freeze
50
+
48
51
  attr_reader :carbon_neutral,
49
52
  :customer_context,
50
53
  :destination_account,
51
54
  :negotiated_rates,
55
+ :pickup_date,
52
56
  :saturday_delivery,
53
57
  :saturday_pickup,
54
58
  :shipper,
55
59
  :shipper_number,
56
60
  :shipping_method,
61
+ :sub_version,
57
62
  :with_time_in_transit
58
63
 
59
64
  def initialize(
@@ -64,14 +69,18 @@ module FriendlyShipping
64
69
  destination_account: nil,
65
70
  negotiated_rates: false,
66
71
  pickup_type: :daily_pickup,
72
+ pickup_date: nil,
67
73
  saturday_delivery: false,
68
74
  saturday_pickup: false,
69
75
  shipper: nil,
70
76
  shipping_method: nil,
77
+ sub_version: "1707",
71
78
  with_time_in_transit: false,
72
79
  package_options_class: FriendlyShipping::Services::Ups::RateEstimatePackageOptions,
73
80
  **kwargs
74
81
  )
82
+ raise ArgumentError, "Invalid sub-version: #{sub_version}" unless sub_version.in?(SUB_VERSIONS)
83
+
75
84
  @carbon_neutral = carbon_neutral
76
85
  @customer_context = customer_context
77
86
  @customer_classification = customer_classification
@@ -79,12 +88,14 @@ module FriendlyShipping
79
88
  @negotiated_rates = negotiated_rates
80
89
  @shipper_number = shipper_number
81
90
  @pickup_type = pickup_type
91
+ @pickup_date = pickup_date
82
92
  @saturday_delivery = saturday_delivery
83
93
  @saturday_pickup = saturday_pickup
84
94
  @shipper = shipper
85
95
  @shipping_method = shipping_method
96
+ @sub_version = sub_version
86
97
  @with_time_in_transit = with_time_in_transit
87
- super(**kwargs.merge(package_options_class: package_options_class))
98
+ super(**kwargs.reverse_merge(package_options_class: package_options_class))
88
99
  end
89
100
 
90
101
  def pickup_type_code
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups/label_item_options'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class Ups
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'nokogiri'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class Ups
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'nokogiri'
4
- require 'friendly_shipping/services/ups/serialize_address_snippet'
5
- require 'friendly_shipping/services/ups/serialize_package_node'
6
-
7
3
  module FriendlyShipping
8
4
  module Services
9
5
  class Ups
@@ -18,7 +14,7 @@ module FriendlyShipping
18
14
  # If no shipping method is given, request all of them
19
15
  # I one is given, omit the request option. It then becomes "Rate", the default.
20
16
  xml.RequestOption('Shop') unless options.shipping_method
21
- xml.SubVersion('1707')
17
+ xml.SubVersion(options.sub_version)
22
18
  # Optional element to identify transactions between client and server.
23
19
  if options.customer_context
24
20
  xml.TransactionReference do
@@ -56,6 +52,15 @@ module FriendlyShipping
56
52
  end
57
53
  end
58
54
 
55
+ if options.pickup_date && options.sub_version.to_i >= 2205
56
+ xml.DeliveryTimeInformation do
57
+ xml.Pickup do
58
+ xml.Date options.pickup_date.strftime('%Y%m%d')
59
+ xml.Time options.pickup_date.strftime('%H%M')
60
+ end
61
+ end
62
+ end
63
+
59
64
  shipment.packages.each do |package|
60
65
  package_options = options.options_for_package(package)
61
66
  SerializePackageNode.call(
@@ -9,7 +9,7 @@ module FriendlyShipping
9
9
  xml.ShipmentAcceptRequest do
10
10
  xml.Request do
11
11
  xml.RequestAction('ShipAccept')
12
- xml.SubVersion('1707')
12
+ xml.SubVersion(options.sub_version)
13
13
  if options.customer_context
14
14
  xml.TransactionReference do
15
15
  xml.CustomerContext(options.customer_context)
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/services/ups/serialize_shipment_address_snippet'
4
- require 'friendly_shipping/services/ups/serialize_package_node'
5
-
6
3
  module FriendlyShipping
7
4
  module Services
8
5
  class Ups
@@ -27,7 +24,7 @@ module FriendlyShipping
27
24
  xml.RequestAction('ShipConfirm')
28
25
  # Required element controls level of address validation.
29
26
  xml.RequestOption(options.validate_address ? 'validate' : 'nonvalidate')
30
- xml.SubVersion('1707')
27
+ xml.SubVersion(options.sub_version)
31
28
  # Optional element to identify transactions between client and server.
32
29
  if options.customer_context
33
30
  xml.TransactionReference do
@@ -242,6 +239,7 @@ module FriendlyShipping
242
239
  xml.InvoiceDate(invoice_date.strftime('%Y%m%d'))
243
240
  xml.ReasonForExport(reason_for_export)
244
241
  xml.CurrencyCode(options.billing_options.currency || 'USD')
242
+ xml.DeclarationStatement(options.declaration_statement) if options.declaration_statement
245
243
 
246
244
  if options.terms_of_shipment_code
247
245
  xml.TermsOfShipment(options.terms_of_shipment_code)
@@ -4,7 +4,7 @@ module FriendlyShipping
4
4
  module Services
5
5
  class Ups
6
6
  EU_COUNTRIES = %w(
7
- AT BE BG CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU LV MT NL PO PT RO SE SI SK
7
+ AT BE BG CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU LV MT NL PL PT RO SE SI SK
8
8
  ).map { |country_code| Carmen::Country.coded(country_code) }
9
9
 
10
10
  class << self
@@ -1,33 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/monads'
4
- require 'friendly_shipping/http_client'
5
- require 'friendly_shipping/services/ups/serialize_access_request'
6
- require 'friendly_shipping/services/ups/serialize_city_state_lookup_request'
7
- require 'friendly_shipping/services/ups/serialize_address_validation_request'
8
- require 'friendly_shipping/services/ups/serialize_rating_service_selection_request'
9
- require 'friendly_shipping/services/ups/serialize_shipment_accept_request'
10
- require 'friendly_shipping/services/ups/serialize_shipment_confirm_request'
11
- require 'friendly_shipping/services/ups/serialize_time_in_transit_request'
12
- require 'friendly_shipping/services/ups/serialize_void_shipment_request'
13
- require 'friendly_shipping/services/ups/parse_address_validation_response'
14
- require 'friendly_shipping/services/ups/parse_address_classification_response'
15
- require 'friendly_shipping/services/ups/parse_city_state_lookup_response'
16
- require 'friendly_shipping/services/ups/parse_rate_response'
17
- require 'friendly_shipping/services/ups/parse_shipment_confirm_response'
18
- require 'friendly_shipping/services/ups/parse_shipment_accept_response'
19
- require 'friendly_shipping/services/ups/parse_time_in_transit_response'
20
- require 'friendly_shipping/services/ups/parse_void_shipment_response'
21
- require 'friendly_shipping/services/ups/shipping_methods'
22
- require 'friendly_shipping/services/ups/label'
23
- require 'friendly_shipping/services/ups/label_options'
24
- require 'friendly_shipping/services/ups/rate_estimate_options'
25
- require 'friendly_shipping/services/ups/timing_options'
3
+ require 'nokogiri'
26
4
 
27
5
  module FriendlyShipping
28
6
  module Services
29
7
  class Ups
30
- include Dry::Monads[:result]
8
+ include Dry::Monads::Result::Mixin
31
9
 
32
10
  attr_reader :test, :key, :login, :password, :client
33
11
 
@@ -1,23 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'friendly_shipping/api_error'
4
-
5
3
  module FriendlyShipping
6
4
  module Services
7
5
  class UpsFreight
8
6
  class ApiError < FriendlyShipping::ApiError
9
7
  # @param [RestClient::Exception] cause
10
8
  def initialize(cause)
11
- super cause, parse_message(cause)
9
+ super(cause, parse_message(cause))
12
10
  end
13
11
 
14
12
  private
15
13
 
16
- # @param [RestClient::Exception] cause
17
- def parse_message(cause)
18
- return cause.message unless cause.response
14
+ # @param [RestClient::Exception] error
15
+ # @return [String]
16
+ def parse_message(error)
17
+ return error.message unless error.response
19
18
 
20
- parsed_json = JSON.parse(cause.response.body)
19
+ parsed_json = JSON.parse(error.response.body)
21
20
 
22
21
  if parsed_json['httpCode'].present?
23
22
  status = [parsed_json['httpCode'], parsed_json['httpMessage']].compact.join(" ")
@@ -31,6 +30,8 @@ module FriendlyShipping
31
30
  [status, desc].compact.join(": ").presence || "UPS Freight could not process the request."
32
31
  end.join("\n")
33
32
  end
33
+ rescue JSON::ParserError, KeyError => _e
34
+ nil
34
35
  end
35
36
  end
36
37
  end