freight_kit 0.1.21 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/Rakefile +1 -3
  4. data/VERSION +1 -1
  5. data/freight_kit-0.1.21.gem +0 -0
  6. data/freight_kit-0.1.22.gem +0 -0
  7. data/freight_kit.gemspec +8 -19
  8. data/lib/freight_kit/address_normalizer.rb +1 -3
  9. data/lib/freight_kit/api_clients/soap_client.rb +30 -5
  10. data/lib/freight_kit/carrier.rb +36 -7
  11. data/lib/freight_kit/carriers/a1_express.rb +11 -0
  12. data/lib/freight_kit/carriers/abf_freight_system.rb +9 -26
  13. data/lib/freight_kit/carriers/aloha_freight_forwarders.rb +1 -5
  14. data/lib/freight_kit/carriers/amado_trucking.rb +1 -5
  15. data/lib/freight_kit/carriers/clear_lane_freight_systems.rb +6 -15
  16. data/lib/freight_kit/carriers/cpl_express.rb +1 -5
  17. data/lib/freight_kit/carriers/cross_country_freight_solutions.rb +6 -17
  18. data/lib/freight_kit/carriers/dependable_highway_express.rb +6 -18
  19. data/lib/freight_kit/carriers/forward_air.rb +4 -12
  20. data/lib/freight_kit/carriers/gsp_express.rb +13 -0
  21. data/lib/freight_kit/carriers/on_trac.rb +7 -25
  22. data/lib/freight_kit/carriers/peninsula_truck_lines.rb +4 -20
  23. data/lib/freight_kit/carriers/roadrunner_transportation_services.rb +2 -8
  24. data/lib/freight_kit/carriers/saia.rb +4 -19
  25. data/lib/freight_kit/carriers/southeastern_freight_lines.rb +4 -13
  26. data/lib/freight_kit/carriers/t_force_worldwide.rb +7 -19
  27. data/lib/freight_kit/carriers/top_ten_express.rb +13 -0
  28. data/lib/freight_kit/carriers/total_quality_logistics.rb +2 -1
  29. data/lib/freight_kit/carriers/western_regional_delivery_service.rb +26 -11
  30. data/lib/freight_kit/carriers/xpo_logistics.rb +5 -17
  31. data/lib/freight_kit/errors/rate_limit_error.rb +12 -0
  32. data/lib/freight_kit/errors/unserviceable_accessorials_error.rb +1 -3
  33. data/lib/freight_kit/helpers/pickupable.rb +1 -10
  34. data/lib/freight_kit/http_client/httpi_adapter.rb +37 -0
  35. data/lib/freight_kit/http_client/safe_retries.rb +17 -1
  36. data/lib/freight_kit/http_client.rb +13 -9
  37. data/lib/freight_kit/models/credential.rb +2 -15
  38. data/lib/freight_kit/models/lane.rb +20 -0
  39. data/lib/freight_kit/models/shipment.rb +1 -8
  40. data/lib/freight_kit/models/territory.rb +37 -0
  41. data/lib/freight_kit/package.rb +3 -13
  42. data/lib/freight_kit/packaging.rb +1 -16
  43. data/lib/freight_kit/platforms/carrier_logistics.rb +5 -10
  44. data/lib/freight_kit/platforms/dsdc.rb +82 -0
  45. data/lib/freight_kit/platforms/next_tms/graphql_client/mutation/quote_create.rb +32 -0
  46. data/lib/freight_kit/platforms/next_tms/graphql_client/mutation/shipment_create.rb +29 -0
  47. data/lib/freight_kit/platforms/next_tms/graphql_client/query/broker_carrier.rb +37 -0
  48. data/lib/freight_kit/platforms/next_tms/graphql_client/query/broker_carriers.rb +45 -0
  49. data/lib/freight_kit/platforms/next_tms/graphql_client/query/broker_customer.rb +54 -0
  50. data/lib/freight_kit/platforms/next_tms/graphql_client/query/broker_customers.rb +62 -0
  51. data/lib/freight_kit/platforms/next_tms/graphql_client/query/carrier.rb +22 -0
  52. data/lib/freight_kit/platforms/next_tms/graphql_client/query/carriers.rb +30 -0
  53. data/lib/freight_kit/platforms/next_tms/graphql_client/query/customer.rb +20 -0
  54. data/lib/freight_kit/platforms/next_tms/graphql_client/query/customers.rb +28 -0
  55. data/lib/freight_kit/platforms/next_tms/graphql_client/query/invoice.rb +31 -0
  56. data/lib/freight_kit/platforms/next_tms/graphql_client/query/invoices.rb +39 -0
  57. data/lib/freight_kit/platforms/next_tms/graphql_client/query/shipment.rb +102 -0
  58. data/lib/freight_kit/platforms/next_tms/graphql_client/query/shipment_documents.rb +29 -0
  59. data/lib/freight_kit/platforms/next_tms/graphql_client/query/shipments.rb +110 -0
  60. data/lib/freight_kit/platforms/next_tms/graphql_client.rb +22 -0
  61. data/lib/freight_kit/platforms/next_tms/orm/address.rb +22 -0
  62. data/lib/freight_kit/platforms/next_tms/orm/base.rb +120 -0
  63. data/lib/freight_kit/platforms/next_tms/orm/broker_carrier.rb +18 -0
  64. data/lib/freight_kit/platforms/next_tms/orm/broker_customer.rb +22 -0
  65. data/lib/freight_kit/platforms/next_tms/orm/carrier.rb +14 -0
  66. data/lib/freight_kit/platforms/next_tms/orm/collection.rb +335 -0
  67. data/lib/freight_kit/platforms/next_tms/orm/customer.rb +12 -0
  68. data/lib/freight_kit/platforms/next_tms/orm/invoice.rb +21 -0
  69. data/lib/freight_kit/platforms/next_tms/orm/record_not_found_error.rb +9 -0
  70. data/lib/freight_kit/platforms/next_tms/orm/shipment.rb +50 -0
  71. data/lib/freight_kit/platforms/next_tms/orm.rb +38 -0
  72. data/lib/freight_kit/platforms/next_tms.rb +414 -55
  73. data/lib/freight_kit/platforms/the_great_information_factory.rb +4 -12
  74. data/lib/freight_kit.rb +19 -0
  75. data/schemas/next_tms.json +110710 -0
  76. metadata +86 -5
@@ -155,29 +155,11 @@ module FreightKit
155
155
  service:,
156
156
  shipment:
157
157
  )
158
- request = build_shipment_request(
159
- delivery_from:,
160
- delivery_to:,
161
- dispatcher:,
162
- pickup_from:,
163
- pickup_to:,
164
- scac:,
165
- service:,
166
- shipment:
167
- )
158
+ request = build_shipment_request(delivery_from:, delivery_to:, dispatcher:, pickup_from:, pickup_to:, scac:, service:, shipment:)
168
159
 
169
160
  labels = parse_shipment_response(commit(request))
170
161
 
171
- request = build_pickup_request(
172
- delivery_from:,
173
- delivery_to:,
174
- dispatcher:,
175
- pickup_from:,
176
- pickup_to:,
177
- scac:,
178
- service:,
179
- shipment:
180
- )
162
+ request = build_pickup_request(delivery_from:, delivery_to:, dispatcher:, pickup_from:, pickup_to:, scac:, service:, shipment:)
181
163
 
182
164
  parse_pickup_response(response: commit(request), labels:)
183
165
  end
@@ -417,9 +399,9 @@ module FreightKit
417
399
 
418
400
  response = case method
419
401
  when :post
420
- HTTPClient.post(url, headers:, body:)
402
+ HTTPClient.post(url, headers:, body:, proxy_url:)
421
403
  else
422
- HTTPClient.get(url, headers:)
404
+ HTTPClient.get(url, headers:, proxy_url:)
423
405
  end
424
406
 
425
407
  response.parsed_response if response&.parsed_response
@@ -487,8 +469,8 @@ module FreightKit
487
469
  prices = []
488
470
  transit_days = nil
489
471
 
490
- api_shipments = response.dig('OnTracRateResponse', 'Shipments', 'Shipment')
491
- api_shipments = [api_shipments] unless api_shipments.is_a?(Array)
472
+ api_shipments = Array.wrap(response.dig('OnTracRateResponse', 'Shipments', 'Shipment'))
473
+ return rate_response.tap { |item| item.error = ResponseError.new('API Error: Blank shipment') } if api_shipments.empty?
492
474
 
493
475
  if api_shipments.any? { |api_shipment| api_shipment['Error']&.downcase&.include?('not serviced') }
494
476
  rate_response.error = UnserviceableError.new
@@ -546,7 +528,7 @@ module FreightKit
546
528
  end
547
529
 
548
530
  api_shipments = response.dig('OnTracShipmentResponse', 'Shipments', 'Shipment')
549
- api_shipments = [api_shipments] unless api_shipments.is_a?(Array)
531
+ api_shipments = Array.wrap(api_shipments)
550
532
 
551
533
  base64_labels = api_shipments&.map { |s| s['Label'] }
552
534
  raise FreightKit::ResponseError, 'API Error: Blank label' if base64_labels.blank?
@@ -100,21 +100,13 @@ module FreightKit
100
100
 
101
101
  call_args = { message: request }
102
102
 
103
- ::FreightKit::SoapClient.new(
104
- carrier: self,
105
- action:,
106
- client_args:,
107
- call_args:,
108
- soap_operation: API.dig(:actions, action)
109
- ).call
103
+ ::FreightKit::SoapClient.new(carrier: self, action:, client_args:, call_args:, soap_operation: API.dig(:actions, action)).call
110
104
  end
111
105
 
112
106
  def parse_amount(amount)
113
107
  negative = amount.start_with?('-$') || amount.start_with?('-')
114
108
 
115
- ['$', '-', ','].each do |char|
116
- amount = amount.sub(char, '')
117
- end
109
+ ['$', '-', ','].each { |char| amount = amount.sub(char, '') }
118
110
 
119
111
  return 0 if amount.blank?
120
112
 
@@ -182,17 +174,9 @@ module FreightKit
182
174
  )
183
175
  end
184
176
 
185
- prices << Price.new(
186
- blame: :api,
187
- cents: parse_amount(result.dig(:quote, :discount_amount)),
188
- description: 'Discount'
189
- )
177
+ prices << Price.new(blame: :api, cents: parse_amount(result.dig(:quote, :discount_amount)), description: 'Discount')
190
178
 
191
- prices << Price.new(
192
- blame: :api,
193
- cents: parse_amount(result.dig(:quote, :fsc_amount)),
194
- description: 'Fuel surcharge'
195
- )
179
+ prices << Price.new(blame: :api, cents: parse_amount(result.dig(:quote, :fsc_amount)), description: 'Fuel surcharge')
196
180
 
197
181
  rate = Rate.new(
198
182
  carrier: self,
@@ -17,11 +17,7 @@ module FreightKit
17
17
 
18
18
  def contacts
19
19
  [
20
- Contact.new(
21
- department: 'Dispatch - Mira Loma, CA',
22
- id: 'dc14714f-e381-4b8b-97a6-f002c0897409',
23
- phone: '+19513615900'
24
- ),
20
+ Contact.new(department: 'Dispatch - Mira Loma, CA', id: 'dc14714f-e381-4b8b-97a6-f002c0897409', phone: '+19513615900'),
25
21
  Contact.new(department: 'Main', id: '22203680-060c-41db-ba65-9925e8fa381d', phone: '+14146151696')
26
22
  ]
27
23
  end
@@ -398,9 +394,7 @@ module FreightKit
398
394
  def parse_amount(amount)
399
395
  negative = amount.start_with?('-$') || amount.start_with?('-')
400
396
 
401
- ['$', '-', ','].each do |char|
402
- amount = amount.sub(char, '')
403
- end
397
+ ['$', '-', ','].each { |char| amount = amount.sub(char, '') }
404
398
 
405
399
  return 0 if amount.blank?
406
400
 
@@ -265,9 +265,7 @@ module FreightKit
265
265
  end
266
266
 
267
267
  if declared_value.present?
268
- request = request.deep_merge(
269
- { request: { DeclaredValue: declared_value, FullValueCoverage: declared_value } }
270
- )
268
+ request = request.deep_merge({ request: { DeclaredValue: declared_value, FullValueCoverage: declared_value } })
271
269
  end
272
270
 
273
271
  save_request(request)
@@ -281,12 +279,7 @@ module FreightKit
281
279
  end
282
280
 
283
281
  def commit(action, request)
284
- client_args = {
285
- wsdl: request_url(action),
286
- convert_request_keys_to: :none,
287
- env_namespace: :soap,
288
- element_form_default: :qualified
289
- }
282
+ client_args = { wsdl: request_url(action), convert_request_keys_to: :none, env_namespace: :soap, element_form_default: :qualified }
290
283
 
291
284
  call_args = { message: request_blueprint.deep_merge(request) }
292
285
 
@@ -307,11 +300,7 @@ module FreightKit
307
300
  end
308
301
 
309
302
  def parse_api_location(api_event)
310
- Location.new(
311
- city: api_event[:city]&.titleize,
312
- province: api_event[:state]&.upcase,
313
- country: ActiveUtils::Country.find('USA')
314
- )
303
+ Location.new(city: api_event[:city]&.titleize, province: api_event[:state]&.upcase, country: ActiveUtils::Country.find('USA'))
315
304
  end
316
305
 
317
306
  def parse_rate_response(shipment:, response:)
@@ -477,11 +466,7 @@ module FreightKit
477
466
  api_events = search_result.dig(:history, :history_item)
478
467
 
479
468
  if api_events.blank?
480
- shipment_events << ShipmentEvent.new(
481
- date_time: pickup_date,
482
- location: shipper_location,
483
- type_code: :picked_up
484
- )
469
+ shipment_events << ShipmentEvent.new(date_time: pickup_date, location: shipper_location, type_code: :picked_up)
485
470
  else
486
471
  api_events = [api_events] if api_events.is_a?(Hash)
487
472
 
@@ -160,11 +160,7 @@ module FreightKit
160
160
  methods: { get_rate: :get, rates: :post },
161
161
  use_ssl: false
162
162
  }.freeze
163
- JSON_HEADERS = {
164
- Accept: 'application/json',
165
- charset: 'utf-8',
166
- 'Content-Type' => 'application/x-www-form-urlencoded'
167
- }.freeze
163
+ JSON_HEADERS = { Accept: 'application/json', charset: 'utf-8', 'Content-Type' => 'application/x-www-form-urlencoded' }.freeze
168
164
 
169
165
  # Documents
170
166
 
@@ -296,12 +292,7 @@ module FreightKit
296
292
  headers = headers.merge(options[:headers]) if options[:headers].present?
297
293
  body = URI.encode_www_form(options[:body]) if options[:body].present?
298
294
 
299
- request = {
300
- url: options[:url].presence || build_url(action),
301
- headers:,
302
- method: API.dig(:methods, action),
303
- body:
304
- }
295
+ request = { url: options[:url].presence || build_url(action), headers:, method: API.dig(:methods, action), body: }
305
296
 
306
297
  save_request(request)
307
298
  request
@@ -317,9 +308,9 @@ module FreightKit
317
308
 
318
309
  case method
319
310
  when :post
320
- HTTPClient.post(url, headers:, body:)
311
+ HTTPClient.post(url, headers:, body:, proxy_url:)
321
312
  else
322
- HTTPClient.get(url, headers:)
313
+ HTTPClient.get(url, headers:, proxy_url:)
323
314
  end
324
315
  end
325
316
 
@@ -190,16 +190,7 @@ module FreightKit
190
190
  service:,
191
191
  shipment:
192
192
  )
193
- request = build_pickup_request(
194
- delivery_from:,
195
- delivery_to:,
196
- dispatcher:,
197
- pickup_from:,
198
- pickup_to:,
199
- scac:,
200
- service:,
201
- shipment:
202
- )
193
+ request = build_pickup_request(delivery_from:, delivery_to:, dispatcher:, pickup_from:, pickup_to:, scac:, service:, shipment:)
203
194
 
204
195
  parse_pickup_response(commit(request))
205
196
  end
@@ -399,9 +390,9 @@ module FreightKit
399
390
 
400
391
  case method
401
392
  when :post
402
- HTTPClient.post(url, headers:, body:)
393
+ HTTPClient.post(url, headers:, body:, proxy_url:)
403
394
  else
404
- HTTPClient.get(url, headers:)
395
+ HTTPClient.get(url, headers:, proxy_url:)
405
396
  end
406
397
  end
407
398
 
@@ -468,8 +459,7 @@ module FreightKit
468
459
  logout_url = 'https://rrd.mercurygate.net/MercuryGate/login/urlRedirect.jsp?Logout=true'
469
460
 
470
461
  begin
471
- browser.element(xpath: '//*[@id="__AppFrameBaseTable"]/tbody/tr[2]/td/div[4]')
472
- .click
462
+ browser.element(xpath: '//*[@id="__AppFrameBaseTable"]/tbody/tr[2]/td/div[4]').click
473
463
  rescue Selenium::WebDriver::Error::UnexpectedAlertOpenError => e
474
464
  browser.close
475
465
 
@@ -484,8 +474,7 @@ module FreightKit
484
474
  browser.iframe(name: 'AppBody').frame(id: 'Header').select(name: 'column').select('Primary Reference')
485
475
  browser.iframe(name: 'AppBody').frame(id: 'Header').select(name: 'condition').select('=')
486
476
  browser.iframe(name: 'AppBody').frame(id: 'Header').text_field(name: 'filter').set(tracking_number)
487
- browser.iframe(name: 'AppBody').frame(id: 'Header').button(value: 'Find')
488
- .click
477
+ browser.iframe(name: 'AppBody').frame(id: 'Header').button(value: 'Find').click
489
478
 
490
479
  begin
491
480
  browser.iframe(name: 'AppBody').frame(id: 'Detail').iframe(id: 'transportsWin')
@@ -505,8 +494,7 @@ module FreightKit
505
494
  return document_response
506
495
  end
507
496
 
508
- html = browser.iframe(name: 'AppBody').frame(id: 'Detail').iframes[1].element(xpath: '/html/body/table[3]')
509
- .html
497
+ html = browser.iframe(name: 'AppBody').frame(id: 'Detail').iframes[1].element(xpath: '/html/body/table[3]').html
510
498
  html = Nokogiri::HTML(html)
511
499
 
512
500
  browser.goto(logout_url)
@@ -529,7 +517,7 @@ module FreightKit
529
517
  end
530
518
 
531
519
  begin
532
- response = HTTPClient.get(url)
520
+ response = HTTPClient.get(url, proxy_url:)
533
521
  rescue StandardError => e
534
522
  document_response.error = e
535
523
  return document_response
@@ -18,6 +18,19 @@ module FreightKit
18
18
  end
19
19
 
20
20
  def id = '06cb58f8-4542-4ffc-aebc-98272678c78b'
21
+
22
+ def lanes
23
+ country = Countries.find_by(iso31661_alpha2: 'US')
24
+
25
+ [
26
+ Lane.new(
27
+ destination: Territory.new(country:, region_abbreviation: 'NY'),
28
+ id: '876f6e13-8611-4b8c-9982-8f661bc2684c',
29
+ origin: Territory.new(country:, region_abbreviation: 'WI')
30
+ )
31
+ ]
32
+ end
33
+
21
34
  def legal_name = 'Top-ten Express Corp'
22
35
  def name = 'Top Ten Express'
23
36
  def us_dot_number = 1_625_877
@@ -214,7 +214,8 @@ module FreightKit
214
214
  request[:url],
215
215
  headers: request[:headers].merge(subscription_key_headers),
216
216
  query: request[:query],
217
- body: request[:body]
217
+ body: request[:body],
218
+ proxy_url:
218
219
  )
219
220
 
220
221
  parsed_response = JSON.parse(response.body)
@@ -31,6 +31,27 @@ module FreightKit
31
31
  end
32
32
 
33
33
  def id = '2d9b9c4c-717e-4832-8c14-2c4e56b01ae7'
34
+
35
+ def lanes
36
+ country = Countries.find_by(iso31661_alpha2: 'US')
37
+ origin = Territory.new(country:, region_abbreviation: 'CA')
38
+
39
+ {
40
+ 'CO' => '9709c88e-5019-4bd7-81f4-25a4fc3d0357',
41
+ 'ID' => '2da75d3b-7e6c-4f00-b6b1-42b57f1bdbd6',
42
+ 'KS' => '9da3069e-ef97-4f96-a6bc-b7be662350ef',
43
+ 'MT' => '755ed10f-5dec-426c-8d22-f64dc1565ccd',
44
+ 'ND' => '087be435-b4cf-448d-b047-7afd54aa7565',
45
+ 'NE' => '6aed2331-66aa-4baa-8e4a-40d3bd2ec989',
46
+ 'NM' => '49a7987e-8ef8-43c3-b6fb-d122cc421648',
47
+ 'SD' => '75d5539f-f15b-495d-a551-1c79fb91bf39',
48
+ 'UT' => 'f6f9fccd-8ce1-43ca-a4a3-36c12d3f309c',
49
+ 'WY' => '3eb6baaa-cc92-4aa2-ae5f-40176f09e0a9'
50
+ }.map do |region_abbreviation, id|
51
+ Lane.new(destination: Territory.new(country:, region_abbreviation:), id:, origin:)
52
+ end
53
+ end
54
+
34
55
  def legal_name = 'South Coast Transportation & Dist. , Inc.'
35
56
  def name = 'Western Regional Delivery Service'
36
57
  def required_credential_types = %i[selenoid website]
@@ -70,9 +91,9 @@ module FreightKit
70
91
  url = request_url(action)
71
92
 
72
93
  response = if API.dig(:methods, action) == :post
73
- options[:params].blank? ? HTTPClient.post(url) : HTTPClient.post(url, query: options[:params])
94
+ options[:params].blank? ? HTTPClient.post(url, proxy_url:) : HTTPClient.post(url, proxy_url:, query: options[:params])
74
95
  else
75
- HTTPClient.get(url)
96
+ HTTPClient.get(url, proxy_url:)
76
97
  end
77
98
 
78
99
  response.parsed_response if response&.parsed_response
@@ -81,11 +102,7 @@ module FreightKit
81
102
  def parse_api_city_state(str)
82
103
  return if str.blank?
83
104
 
84
- Location.new(
85
- city: str[..-3].strip.titleize,
86
- province: str[-2..].upcase,
87
- country: ActiveUtils::Country.find('USA')
88
- )
105
+ Location.new(city: str[..-3].strip.titleize, province: str[-2..].upcase, country: ActiveUtils::Country.find('USA'))
89
106
  end
90
107
 
91
108
  def parse_api_city_state_zip(str)
@@ -118,7 +135,7 @@ module FreightKit
118
135
  document_response = DocumentResponse.new(request: URI.parse(url))
119
136
 
120
137
  begin
121
- response = HTTPClient.get(url)
138
+ response = HTTPClient.get(url, proxy_url:)
122
139
  rescue StandardError => e
123
140
  document_response.error = e
124
141
  return document_response
@@ -152,9 +169,7 @@ module FreightKit
152
169
  image_url = nil
153
170
  browser.switch_window.use do
154
171
  page_count = browser.element(xpath: '/html/body/form/div[3]/b/span').text.strip.to_i
155
- (page_count - 1).times do
156
- browser.element(xpath: '/html/body/form/div[3]/input[2]').wait_until(&:present?).click
157
- end
172
+ (page_count - 1).times { browser.element(xpath: '/html/body/form/div[3]/input[2]').wait_until(&:present?).click }
158
173
  image_url = browser.element(css: '#cphMain_imgImage').attribute_value('src')
159
174
  end
160
175
  browser.close
@@ -17,11 +17,7 @@ module FreightKit
17
17
 
18
18
  def contacts
19
19
  [
20
- Contact.new(
21
- department: 'Customer Service - Kingman, AZ',
22
- id: '5bb9caaa-ae84-4ee6-90ef-3c14f6d434c6',
23
- phone: '+17609228255'
24
- )
20
+ Contact.new(department: 'Customer Service - Kingman, AZ', id: '5bb9caaa-ae84-4ee6-90ef-3c14f6d434c6', phone: '+17609228255')
25
21
  ]
26
22
  end
27
23
 
@@ -159,11 +155,7 @@ module FreightKit
159
155
  def website_url = 'https://www.xpo.com'
160
156
  end
161
157
 
162
- API = {
163
- domain: 'api.ltl.xpo.com',
164
- endpoints: { rates: '/rating/1.0/ratequotes', token: '/token' },
165
- use_ssl: true
166
- }.freeze
158
+ API = { domain: 'api.ltl.xpo.com', endpoints: { rates: '/rating/1.0/ratequotes', token: '/token' }, use_ssl: true }.freeze
167
159
 
168
160
  # Documents
169
161
 
@@ -223,11 +215,7 @@ module FreightKit
223
215
  def build_bearer_token_request
224
216
  api_credentials = fetch_credential(:api)
225
217
 
226
- body = URI.encode_www_form(
227
- grant_type: 'password',
228
- password: api_credentials.password,
229
- username: api_credentials.username
230
- )
218
+ body = URI.encode_www_form(grant_type: 'password', password: api_credentials.password, username: api_credentials.username)
231
219
 
232
220
  {
233
221
  body:,
@@ -301,9 +289,9 @@ module FreightKit
301
289
 
302
290
  response = case method
303
291
  when :post
304
- HTTPClient.post(url, headers:, body:)
292
+ HTTPClient.post(url, headers:, body:, proxy_url:)
305
293
  else
306
- HTTPClient.get(url, headers:)
294
+ HTTPClient.get(url, headers:, proxy_url:)
307
295
  end
308
296
 
309
297
  begin
@@ -0,0 +1,12 @@
1
+ module FreightKit
2
+ # The API refused a request because the caller sent too many. Wait {#retry_after} seconds before the next one.
3
+ class RateLimitError < HTTPError
4
+ attr_reader :retry_after
5
+
6
+ def initialize(body:, code:, retry_after: nil)
7
+ @retry_after = retry_after
8
+
9
+ super(body:, code:)
10
+ end
11
+ end
12
+ end
@@ -9,9 +9,7 @@ module FreightKit
9
9
  end
10
10
 
11
11
  def message
12
- @message ||= "Unable to service #{@accessorials.map do |accessorial|
13
- accessorial.to_s.gsub('_', ' ')
14
- end.join(', ')}"
12
+ @message ||= "Unable to service #{@accessorials.map { |accessorial| accessorial.to_s.gsub('_', ' ') }.join(', ')}"
15
13
  end
16
14
  end
17
15
  end
@@ -10,16 +10,7 @@ module FreightKit
10
10
  service:,
11
11
  shipment:
12
12
  )
13
- request = build_pickup_request(
14
- delivery_from:,
15
- delivery_to:,
16
- dispatcher:,
17
- pickup_from:,
18
- pickup_to:,
19
- scac:,
20
- service:,
21
- shipment:
22
- )
13
+ request = build_pickup_request(delivery_from:, delivery_to:, dispatcher:, pickup_from:, pickup_to:, scac:, service:, shipment:)
23
14
 
24
15
  begin
25
16
  # For SOAP APIs, the :action parameter is required
@@ -0,0 +1,37 @@
1
+ module FreightKit
2
+ class HTTPClient
3
+ # HTTPI adapter sending Savon's requests through {HTTPClient}, so SOAP calls get its compression,
4
+ # connection reuse, retries, cache, logger and User-Agent:
5
+ #
6
+ # Savon.client(adapter: FreightKit::HTTPClient::HTTPIAdapter::NAME, wsdl: url)
7
+ class HTTPIAdapter < HTTPI::Adapter::Base
8
+ NAME = :freight_kit
9
+
10
+ register NAME
11
+
12
+ attr_reader :client
13
+
14
+ def initialize(request)
15
+ super
16
+ raise HTTPI::NotSupportedError, "#{NAME} does not support #{request.auth.type} authentication" if request.auth.type
17
+
18
+ @client = HTTPClient
19
+ @request = request
20
+ end
21
+
22
+ def request(method)
23
+ response = client.request(
24
+ method,
25
+ @request.url.to_s,
26
+ body: @request.body,
27
+ headers: @request.headers.to_h,
28
+ proxy_url: @request.proxy&.to_s,
29
+ read_timeout: @request.read_timeout
30
+ )
31
+
32
+ # HTTPX already decoded the body, and HTTPI would gunzip it again
33
+ HTTPI::Response.new(response.code, response.headers.to_h.except('content-encoding'), response.body)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -6,13 +6,29 @@ module FreightKit
6
6
  # The persistent plugin retries any request failing with a connection error, but a POST may
7
7
  # already have been processed by then, so only idempotent requests are retried.
8
8
  # Streams refused by an HTTP/2 GOAWAY are resent by HTTPX itself, independently of retries.
9
+ #
10
+ # A 429 means the server did not process the request, so any request is retried once after its
11
+ # Retry-After seconds, unless that is longer than {MAX_RETRY_AFTER}.
9
12
  module SafeRetries
10
13
  IDEMPOTENT_METHODS = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT'].freeze
14
+ # Longest wait in seconds. Longer waits return the 429, so callers can reschedule instead of blocking.
15
+ MAX_RETRY_AFTER = 60
11
16
 
12
17
  module InstanceMethods
13
18
  private
14
19
 
15
- def retryable_request?(request, *) = super && IDEMPOTENT_METHODS.include?(request.verb)
20
+ def rate_limited?(response)
21
+ response.is_a?(HTTPX::Response) && response.status == 429 && retry_after(response)&.between?(0, MAX_RETRY_AFTER)
22
+ end
23
+
24
+ def retry_after(response) = Integer(response.headers['retry-after'], 10, exception: false)
25
+
26
+ def retryable_request?(request, response, *)
27
+ rate_limited?(response) || (super && IDEMPOTENT_METHODS.include?(request.verb))
28
+ end
29
+
30
+ def retryable_response?(response, *) = super || rate_limited?(response)
31
+ def when_to_retry(request, response, *) = rate_limited?(response) ? retry_after(response) : super
16
32
  end
17
33
 
18
34
  class << self
@@ -2,7 +2,7 @@ module FreightKit
2
2
  # Thin wrapper around HTTPX that negotiates Brotli/Zstandard compression, keeps connections
3
3
  # open between requests and revalidates cached responses with conditional requests (ETag/Last-Modified).
4
4
  #
5
- # The cache and logger are hookable; they default to the host Rails/Hanami app's logger:
5
+ # The cache defaults to {FreightKit.cache}, and the logger to the host Rails/Hanami app's logger. Both are hookable:
6
6
  #
7
7
  # FreightKit::HTTPClient.cache = Rails.cache
8
8
  # FreightKit::HTTPClient.logger = Rails.logger
@@ -12,11 +12,11 @@ module FreightKit
12
12
  @sessions = Concurrent::Map.new
13
13
 
14
14
  class << self
15
- def cache = @cache ||= ActiveSupport::Cache::MemoryStore.new
15
+ def cache = @cache || FreightKit.cache
16
16
 
17
17
  def cache=(store)
18
18
  @cache = store
19
- @sessions.clear
19
+ reset
20
20
  end
21
21
 
22
22
  def get(url, **) = request(:get, url, **)
@@ -24,14 +24,15 @@ module FreightKit
24
24
 
25
25
  def logger=(logger)
26
26
  @logger = logger
27
- @sessions.clear
27
+ reset
28
28
  end
29
29
 
30
30
  def post(url, **) = request(:post, url, **)
31
31
 
32
- def request(verb, url, body: nil, headers: {}, proxy: nil, query: nil)
32
+ def request(verb, url, body: nil, headers: {}, proxy_url: nil, query: nil, read_timeout: nil)
33
33
  options = { headers: }
34
34
  options[:params] = query if query.present?
35
+ options[:timeout] = { read_timeout: } if read_timeout
35
36
 
36
37
  # Like HTTParty, hashes are form-encoded regardless of the Content-Type header
37
38
  if body.is_a?(Hash)
@@ -40,12 +41,15 @@ module FreightKit
40
41
  options[:body] = body
41
42
  end
42
43
 
43
- response = session(proxy.presence).request(verb.to_s.upcase, url, **options)
44
+ response = session(proxy_url.presence).request(verb.to_s.upcase, url, **options)
44
45
  raise response.error if response.is_a?(HTTPX::ErrorResponse)
45
46
 
46
47
  Response.new(response)
47
48
  end
48
49
 
50
+ # Rebuilds sessions on their next request, e.g. after the cache or logger changes.
51
+ def reset = @sessions.clear
52
+
49
53
  private
50
54
 
51
55
  def framework_logger
@@ -57,8 +61,8 @@ module FreightKit
57
61
  end
58
62
 
59
63
  # Sessions own their connection pool, so they are built once (per proxy) and reused
60
- def session(proxy)
61
- @sessions.compute_if_absent(proxy) do
64
+ def session(proxy_url)
65
+ @sessions.compute_if_absent(proxy_url) do
62
66
  session = HTTPX.plugin(:brotli).plugin(Zstd).plugin(:follow_redirects)
63
67
  .plugin(:response_cache, response_cache_store: CacheStore.new(cache))
64
68
  .plugin(Revalidation)
@@ -68,7 +72,7 @@ module FreightKit
68
72
  logger = self.logger
69
73
  session = session.with(debug: LogDevice.new(logger), debug_level: 2) if logger.level <= ::Logger::DEBUG
70
74
 
71
- proxy ? session.plugin(:proxy).with(proxy: { uri: proxy }) : session
75
+ proxy_url ? session.plugin(:proxy).with(proxy: { uri: proxy_url }) : session
72
76
  end
73
77
  end
74
78
  end
@@ -51,7 +51,7 @@ module FreightKit
51
51
  when :api, :website
52
52
  { password: String }
53
53
  when :api_proxy
54
- { api_key: String, proxy_url: String }
54
+ { proxy_url: String }
55
55
  when :oauth2
56
56
  { access_token: String, expires_at: ::DateTime, scope: String }
57
57
  when :selenoid
@@ -96,20 +96,7 @@ module FreightKit
96
96
  url.path = '/wd/hub/'
97
97
  url = url.to_s
98
98
 
99
- @watir_args = [
100
- browser,
101
- {
102
- options: {
103
- prefs: {
104
- download: {
105
- directory_upgrade: true,
106
- prompt_for_download: false
107
- }
108
- }
109
- },
110
- url:
111
- }
112
- ]
99
+ @watir_args = [browser, { options: { prefs: { download: { directory_upgrade: true, prompt_for_download: false } } }, url: }]
113
100
  end
114
101
  end
115
102
  end