freight_kit 0.1.30 → 0.1.32
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/lib/freight_kit/carriers/abf_freight_system.rb +48 -2
- data/lib/freight_kit/carriers/clear_lane_freight_systems.rb +1 -1
- data/lib/freight_kit/carriers/cross_country_freight_solutions.rb +1 -2
- data/lib/freight_kit/carriers/dependable_highway_express.rb +1 -1
- data/lib/freight_kit/carriers/forward_air.rb +2 -1
- data/lib/freight_kit/carriers/roadrunner_transportation_services.rb +12 -21
- data/lib/freight_kit/carriers/t_force_worldwide.rb +2 -4
- data/lib/freight_kit/carriers/western_regional_delivery_service.rb +98 -147
- data/lib/freight_kit/http_client.rb +17 -8
- data/lib/freight_kit/models/document_response.rb +30 -6
- data/lib/freight_kit/platforms/carrier_logistics.rb +1 -1
- data/lib/freight_kit/platforms/next_tms.rb +1 -1
- data/lib/freight_kit/platforms/road_vision.rb +4 -5
- data/lib/freight_kit/platforms/the_great_information_factory.rb +1 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 944f3675adffe1ca4d0b40807f0d91dea0f80343c21fa567ba629fd37c4ab1e4
|
|
4
|
+
data.tar.gz: 580956fbc512420ccc3903c3f3409442e5819ca2072b7fb0a15b23fb0d334e33
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: af2781960a31a4903af221d0bed9fb66489baed89bdcf50f4545440c69fd1fe40fe5a4730992bf8b6c35020a6141c82ada6df8cc8a3a3c85f2886a4094afa8a9
|
|
7
|
+
data.tar.gz: 42f13062fbf2afe38b17c3c71c0c71b72a903138e4c8e31bebf976188ddc99b17c4bf543b3a3d4b89973ed7fbac2a743c7213e202b64e3ef43ccce59418a4852
|
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.1.
|
|
1
|
+
0.1.32
|
|
@@ -114,7 +114,11 @@ module FreightKit
|
|
|
114
114
|
def website_url = 'https://arcb.com'
|
|
115
115
|
end
|
|
116
116
|
|
|
117
|
-
API = {
|
|
117
|
+
API = {
|
|
118
|
+
domain: 'www.abfs.com',
|
|
119
|
+
endpoints: { documents: '/xml/docretxml.asp', rates: '/xml/aquotexml.asp' },
|
|
120
|
+
methods: { documents: 'GET', rates: 'GET' }
|
|
121
|
+
}.freeze
|
|
118
122
|
DELIVERY_LIMITED_ACCESS_TYPES = {
|
|
119
123
|
airport_delivery: 'O',
|
|
120
124
|
amusement_park_delivery: 'O',
|
|
@@ -141,6 +145,8 @@ module FreightKit
|
|
|
141
145
|
university_delivery: 'O',
|
|
142
146
|
utility_site_delivery: 'O'
|
|
143
147
|
}.freeze
|
|
148
|
+
DOCUMENT_ERRORS = { '9' => DocumentNotFoundError, '15' => ShipmentNotFoundError, '93' => InvalidCredentialsError }.freeze
|
|
149
|
+
DOCUMENT_TYPES = { bol: 'BOL', pod: 'DR' }.freeze
|
|
144
150
|
PACKAGE_TYPES = {
|
|
145
151
|
box: 'BX',
|
|
146
152
|
bundle: 'BDL',
|
|
@@ -183,6 +189,9 @@ module FreightKit
|
|
|
183
189
|
|
|
184
190
|
# Documents
|
|
185
191
|
|
|
192
|
+
def pod(tracking_number) = document(:pod, tracking_number)
|
|
193
|
+
def scanned_bol(tracking_number) = document(:bol, tracking_number)
|
|
194
|
+
|
|
186
195
|
# Pickups
|
|
187
196
|
|
|
188
197
|
# Rates
|
|
@@ -204,6 +213,18 @@ module FreightKit
|
|
|
204
213
|
|
|
205
214
|
protected
|
|
206
215
|
|
|
216
|
+
def build_document_request(type, tracking_number)
|
|
217
|
+
params = {
|
|
218
|
+
'APP_ID' => fetch_credential(:api_key)&.api_key,
|
|
219
|
+
'DocType' => DOCUMENT_TYPES.fetch(type),
|
|
220
|
+
'ID' => fetch_credential(:api).username,
|
|
221
|
+
'RefNum' => tracking_number,
|
|
222
|
+
'RefType' => 'A'
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
build_request(:documents, { params: params.compact })
|
|
226
|
+
end
|
|
227
|
+
|
|
207
228
|
def build_rate_request(shipment:)
|
|
208
229
|
broker_credential = fetch_credential(:api)
|
|
209
230
|
tms_credential = fetch_credential(:api_key)
|
|
@@ -382,7 +403,32 @@ module FreightKit
|
|
|
382
403
|
HTTPClient.get(url, headers:, proxy_url:)
|
|
383
404
|
end
|
|
384
405
|
|
|
385
|
-
response
|
|
406
|
+
response&.parsed_response&.deep_transform_keys! { |key| key.downcase.to_sym }&.dig(:abf)
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
# ABF lists one URL per page, which downloads without credentials, so each page becomes a part.
|
|
410
|
+
def document(type, tracking_number)
|
|
411
|
+
response = commit(build_document_request(type, tracking_number))
|
|
412
|
+
document_response = DocumentResponse.new(request: last_request, response:)
|
|
413
|
+
|
|
414
|
+
if response[:numerrors].to_i.positive?
|
|
415
|
+
error = response.dig(:errors, :errorcode)
|
|
416
|
+
message = response.dig(:errors, :errormessage)
|
|
417
|
+
return document_response.tap { |item| item.error = DOCUMENT_ERRORS.fetch(error, ResponseError).new(message) }
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
document = Array.wrap(response.dig(:documents, :document)).find { |item| item[:type] == DOCUMENT_TYPES[type] }
|
|
421
|
+
urls = Array.wrap(document&.dig(:pages, :page)).compact_blank
|
|
422
|
+
return document_response.tap { |item| item.error = DocumentNotFoundError.new } if urls.empty?
|
|
423
|
+
|
|
424
|
+
document_response.tap { |item| item.parts = urls.map { |url| download(url) } }
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
def download(url)
|
|
428
|
+
response = HTTPClient.get(url, proxy_url:)
|
|
429
|
+
raise HTTPError.new(body: response.body, code: response.code) unless response.code == 200
|
|
430
|
+
|
|
431
|
+
DocumentResponse::Part.new(content_type: response.content_type, data: response.body)
|
|
386
432
|
end
|
|
387
433
|
|
|
388
434
|
def parse_rate_response(shipment:, response:)
|
|
@@ -386,7 +386,7 @@ module FreightKit
|
|
|
386
386
|
return document_response
|
|
387
387
|
end
|
|
388
388
|
|
|
389
|
-
document_response.
|
|
389
|
+
document_response.parts = [DocumentResponse::Part.new(content_type: response.headers['content-type'], data: response.body)]
|
|
390
390
|
document_response
|
|
391
391
|
end
|
|
392
392
|
|
|
@@ -326,8 +326,7 @@ module FreightKit
|
|
|
326
326
|
return document_response
|
|
327
327
|
end
|
|
328
328
|
|
|
329
|
-
|
|
330
|
-
document_response.assign_attributes(content_type: 'application/pdf', data: decoded_pdf_data)
|
|
329
|
+
document_response.parts = [DocumentResponse::Part.new(data_base64: document['Content'])]
|
|
331
330
|
|
|
332
331
|
document_response
|
|
333
332
|
end
|
|
@@ -379,7 +379,7 @@ module FreightKit
|
|
|
379
379
|
return document_response
|
|
380
380
|
end
|
|
381
381
|
|
|
382
|
-
document_response.
|
|
382
|
+
document_response.parts = [DocumentResponse::Part.new(content_type: response.headers['content-type'], data: response.body)]
|
|
383
383
|
document_response
|
|
384
384
|
end
|
|
385
385
|
|
|
@@ -620,7 +620,8 @@ module FreightKit
|
|
|
620
620
|
end
|
|
621
621
|
|
|
622
622
|
def parse_document_response(_type, _tracking_number, response)
|
|
623
|
-
DocumentResponse.new(content_type: response.headers['content-type'], data: response.body
|
|
623
|
+
parts = [DocumentResponse::Part.new(content_type: response.headers['content-type'], data: response.body)]
|
|
624
|
+
DocumentResponse.new(parts:, request: last_request)
|
|
624
625
|
end
|
|
625
626
|
|
|
626
627
|
def parse_locations_response(country:, response:)
|
|
@@ -205,22 +205,21 @@ module FreightKit
|
|
|
205
205
|
API = {
|
|
206
206
|
actions: { rates: :rate_quote_by_account },
|
|
207
207
|
domains: {
|
|
208
|
-
|
|
209
|
-
pod: 'tools.rrts.com',
|
|
208
|
+
documents: 'tools.rrts.com',
|
|
210
209
|
rates: 'webservices.rrts.com',
|
|
211
210
|
track: 'webservices.rrts.com',
|
|
212
211
|
tracking_number_from_pickup_number: 'tools.rrts.com'
|
|
213
212
|
},
|
|
214
213
|
endpoints: {
|
|
215
|
-
|
|
216
|
-
pod: '/LTLTrack/Home/ShowImage?proNumber=%%TRACKING_NUMBER%%&imageType=POD',
|
|
214
|
+
documents: '/LTLTrack/TrackingShipment/ViewDocument',
|
|
217
215
|
rates: '/rating/ratequote.asmx?wsdl',
|
|
218
216
|
track: '/TrackWebApi/api/values',
|
|
219
217
|
tracking_number_from_pickup_number: '/LTLTrack/?searchValues=%%PICKUP_NUMBER%%'
|
|
220
218
|
},
|
|
221
219
|
soap: { namespaces: { rates: 'https://webservices.rrts.com/ratequote/' } },
|
|
222
|
-
use_ssl: {
|
|
220
|
+
use_ssl: { documents: true, rates: false, track: true, tracking_number_from_pickup_number: false }
|
|
223
221
|
}.freeze
|
|
222
|
+
DOCUMENT_TYPES = { bol: 'BOL', pod: 'POD' }.freeze
|
|
224
223
|
EVENTS = {
|
|
225
224
|
types: {
|
|
226
225
|
arrived_at_terminal: 'ARV',
|
|
@@ -435,25 +434,17 @@ module FreightKit
|
|
|
435
434
|
end
|
|
436
435
|
|
|
437
436
|
def parse_document_response(type, tracking_number)
|
|
438
|
-
url = request_url(
|
|
439
|
-
|
|
437
|
+
url = request_url(:documents)
|
|
438
|
+
query = { appIDEncoded: '', imageType: DOCUMENT_TYPES.fetch(type), isRevOnly: false, proNumber: tracking_number }
|
|
439
|
+
document_response = DocumentResponse.new(request: { query:, url: })
|
|
440
440
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
rescue OpenURI::HTTPError
|
|
444
|
-
document_response.error = FreightKit::DocumentNotFoundError.new
|
|
445
|
-
return document_response
|
|
446
|
-
end
|
|
447
|
-
|
|
448
|
-
if doc.css('img').blank?
|
|
449
|
-
document_response.error = FreightKit::DocumentNotFoundError.new
|
|
450
|
-
return document_response
|
|
451
|
-
end
|
|
441
|
+
response = HTTPClient.get(url, proxy_url:, query:)
|
|
442
|
+
raise HTTPError.new(body: response.body, code: response.code) unless response.code == 200
|
|
452
443
|
|
|
453
|
-
|
|
444
|
+
sources = Nokogiri::HTML(response.body.to_s).css('img[src^="data:"]').map { |img| img['src'] }
|
|
445
|
+
return document_response.tap { |item| item.error = DocumentNotFoundError.new } if sources.empty?
|
|
454
446
|
|
|
455
|
-
document_response.
|
|
456
|
-
document_response
|
|
447
|
+
document_response.tap { |item| item.parts = sources.map { |source| DocumentResponse::Part.new(data_base64: source.split(',', 2).last) } }
|
|
457
448
|
end
|
|
458
449
|
|
|
459
450
|
def parse_rate_response(shipment:, response:)
|
|
@@ -407,9 +407,7 @@ module FreightKit
|
|
|
407
407
|
return document_response
|
|
408
408
|
end
|
|
409
409
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
document_response.assign_attributes(content_type: 'application/pdf', data:)
|
|
410
|
+
document_response.parts = [DocumentResponse::Part.new(data_base64: file_bytes)]
|
|
413
411
|
document_response
|
|
414
412
|
end
|
|
415
413
|
|
|
@@ -528,7 +526,7 @@ module FreightKit
|
|
|
528
526
|
return document_response
|
|
529
527
|
end
|
|
530
528
|
|
|
531
|
-
document_response.
|
|
529
|
+
document_response.parts = [DocumentResponse::Part.new(content_type: response.headers['content-type'], data: response.body)]
|
|
532
530
|
document_response
|
|
533
531
|
end
|
|
534
532
|
|
|
@@ -54,7 +54,7 @@ module FreightKit
|
|
|
54
54
|
|
|
55
55
|
def legal_name = 'South Coast Transportation & Dist. , Inc.'
|
|
56
56
|
def name = 'Western Regional Delivery Service'
|
|
57
|
-
def required_credential_types = %i[
|
|
57
|
+
def required_credential_types = %i[website]
|
|
58
58
|
def requirements = %i[credentials]
|
|
59
59
|
def scac = 'WRDS'
|
|
60
60
|
def services = %i[standard]
|
|
@@ -64,10 +64,9 @@ module FreightKit
|
|
|
64
64
|
|
|
65
65
|
API = {
|
|
66
66
|
domain: 'tracking.wrds.com',
|
|
67
|
-
endpoints: {
|
|
68
|
-
methods: { track: :post },
|
|
69
|
-
use_ssl: { bol: false, pod: false, track: true }
|
|
67
|
+
endpoints: { documents: '/zDocImages.aspx', login: '/zLogin.aspx', track: '/zTrack2.aspx' }
|
|
70
68
|
}.freeze
|
|
69
|
+
DOCUMENT_INDEXES = { bol: 0..0, pod: 2.. }.freeze
|
|
71
70
|
EVENTS = {
|
|
72
71
|
types: {
|
|
73
72
|
arrived_at_terminal: 'arrived',
|
|
@@ -78,25 +77,59 @@ module FreightKit
|
|
|
78
77
|
}.freeze
|
|
79
78
|
|
|
80
79
|
# Tracking
|
|
81
|
-
def find_tracking_info(tracking_number)
|
|
80
|
+
def find_tracking_info(tracking_number)
|
|
81
|
+
cookies = HTTPClient.cookie_jar
|
|
82
|
+
details = search(cookies, fetch(cookies, request_url(:track)), tracking_number)
|
|
83
|
+
return TrackingResponse.new(carrier: self, error: ShipmentNotFoundError.new, tracking_number:) if details.nil?
|
|
84
|
+
|
|
85
|
+
parse_tracking_response(tracking_number, details)
|
|
86
|
+
end
|
|
82
87
|
|
|
83
88
|
# Documents
|
|
84
|
-
def pod(tracking_number) =
|
|
89
|
+
def pod(tracking_number) = document(:pod, tracking_number)
|
|
90
|
+
def scanned_bol(tracking_number) = document(:bol, tracking_number)
|
|
85
91
|
|
|
86
92
|
protected
|
|
87
93
|
|
|
88
|
-
def
|
|
94
|
+
def document(type, tracking_number)
|
|
95
|
+
document_response = DocumentResponse.new(request: { tracking_number:, type: })
|
|
96
|
+
cookies = HTTPClient.cookie_jar
|
|
97
|
+
details = search(cookies, log_in(cookies), tracking_number)
|
|
98
|
+
return document_response.tap { |item| item.error = DocumentNotFoundError.new } if details&.at('a[href*="btnImageYes"]').nil?
|
|
99
|
+
|
|
100
|
+
submit(cookies, details, '__EVENTTARGET' => 'ctl00$cphMain$btnImageYes')
|
|
101
|
+
viewer = fetch(cookies, request_url(:documents))
|
|
102
|
+
indexes = (0...viewer.at('#cphMain_lblAmount').text.to_i).to_a[DOCUMENT_INDEXES.fetch(type)].to_a
|
|
103
|
+
return document_response.tap { |item| item.error = DocumentNotFoundError.new } if indexes.empty?
|
|
104
|
+
|
|
105
|
+
parts = (0..indexes.max).filter_map do |index|
|
|
106
|
+
viewer = submit(cookies, viewer, 'ctl00$cphMain$btnNext' => 'Next') if index.positive?
|
|
107
|
+
download(cookies, viewer.at('#cphMain_imgImage')['src']) if indexes.include?(index)
|
|
108
|
+
end
|
|
89
109
|
|
|
90
|
-
|
|
91
|
-
|
|
110
|
+
document_response.tap { |item| item.parts = parts }
|
|
111
|
+
end
|
|
92
112
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
else
|
|
96
|
-
HTTPClient.get(url, proxy_url:)
|
|
97
|
-
end
|
|
113
|
+
def download(cookies, path)
|
|
114
|
+
response = request(:get, URI.join(request_url(:login), path).to_s, cookies:)
|
|
98
115
|
|
|
99
|
-
response.
|
|
116
|
+
DocumentResponse::Part.new(content_type: response.content_type, data: response.body)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def fetch(cookies, url) = Nokogiri::HTML(request(:get, url, cookies:).body)
|
|
120
|
+
|
|
121
|
+
def log_in(cookies)
|
|
122
|
+
credential = fetch_credential(:website)
|
|
123
|
+
page = submit(
|
|
124
|
+
cookies,
|
|
125
|
+
fetch(cookies, request_url(:login)),
|
|
126
|
+
'ctl00$cphMain$btnLogIn' => 'Log In',
|
|
127
|
+
'ctl00$cphMain$txtPassword' => credential.password,
|
|
128
|
+
'ctl00$cphMain$txtUserName' => credential.username
|
|
129
|
+
)
|
|
130
|
+
raise InvalidCredentialsError if page.text.include?('Username or password is invalid.')
|
|
131
|
+
|
|
132
|
+
page
|
|
100
133
|
end
|
|
101
134
|
|
|
102
135
|
def parse_api_city_state(str)
|
|
@@ -130,156 +163,74 @@ module FreightKit
|
|
|
130
163
|
::FreightKit::DateTime.new(local_date_time:, location:)
|
|
131
164
|
end
|
|
132
165
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
rescue StandardError => e
|
|
140
|
-
document_response.error = e
|
|
141
|
-
return document_response
|
|
166
|
+
def parse_tracking_response(tracking_number, details)
|
|
167
|
+
destination = parse_api_city_state_zip(details.at('#cphMain_lblConsCSZ')&.text&.squish)
|
|
168
|
+
origin = parse_api_city_state_zip(details.at('#cphMain_lblShipperCSZ')&.text&.squish)
|
|
169
|
+
notes = details.css('#cphMain_grvLogNotes tr').filter_map do |row|
|
|
170
|
+
cells = row.css('td').map { |cell| cell.text.squish }
|
|
171
|
+
cells if cells.size == 2
|
|
142
172
|
end
|
|
143
173
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
def parse_pod_response(tracking_number)
|
|
149
|
-
selenoid_credentials = fetch_credential(:selenoid)
|
|
150
|
-
website_credentials = fetch_credential(:website)
|
|
174
|
+
shipment_events = notes.filter_map do |date_time, event|
|
|
175
|
+
next if event.downcase.include?('estimated')
|
|
151
176
|
|
|
152
|
-
|
|
153
|
-
|
|
177
|
+
type_code, text = EVENTS[:types].find { |_key, value| event.downcase.include?(value) }
|
|
178
|
+
next if type_code.nil?
|
|
154
179
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
180
|
+
location = unless type_code == :delivery_appointment_scheduled
|
|
181
|
+
city_state = event.downcase.split(text).last.delete(',')
|
|
182
|
+
parse_api_city_state(city_state) unless city_state.include?('carrier')
|
|
183
|
+
end
|
|
158
184
|
|
|
159
|
-
|
|
160
|
-
browser.close
|
|
161
|
-
raise InvalidCredentialsError
|
|
185
|
+
ShipmentEvent.new(date_time: parse_api_date_time(date_time, location), location:, type_code:)
|
|
162
186
|
end
|
|
163
187
|
|
|
164
|
-
|
|
165
|
-
browser.button(name: 'ctl00$cphMain$btnSearchProNumber').wait_until(&:present?).click
|
|
166
|
-
browser.element(xpath: '/html/body/form/div[3]/div/div/table/tbody/tr[2]/td[1]/a').wait_until(&:present?).click
|
|
167
|
-
browser.element(xpath: '/html/body/form/div[3]/table[2]/tbody/tr[16]/td[2]/a').wait_until(&:present?).click
|
|
168
|
-
|
|
169
|
-
image_url = nil
|
|
170
|
-
browser.switch_window.use do
|
|
171
|
-
page_count = browser.element(xpath: '/html/body/form/div[3]/b/span').text.strip.to_i
|
|
172
|
-
(page_count - 1).times { browser.element(xpath: '/html/body/form/div[3]/input[2]').wait_until(&:present?).click }
|
|
173
|
-
image_url = browser.element(css: '#cphMain_imgImage').attribute_value('src')
|
|
174
|
-
end
|
|
175
|
-
browser.close
|
|
176
|
-
|
|
177
|
-
parse_document_response(image_url)
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
def parse_tracking_response(tracking_number)
|
|
181
|
-
tracking_response = TrackingResponse.new(carrier: self)
|
|
182
|
-
|
|
183
|
-
selenoid_credentials = fetch_credential(:selenoid)
|
|
184
|
-
|
|
185
|
-
browser = Watir::Browser.new(*selenoid_credentials.watir_args)
|
|
186
|
-
browser.goto(build_url(:track))
|
|
187
|
-
|
|
188
|
-
browser.text_field(name: 'ctl00$cphMain$txtProNumber').set(tracking_number)
|
|
189
|
-
browser.button(name: 'ctl00$cphMain$btnSearchProNumber').wait_until(&:present?).click
|
|
190
|
-
browser.element(xpath: '/html/body/form/div[3]/div/div/table/tbody/tr[2]/td[1]/a').wait_until(&:present?).click
|
|
191
|
-
|
|
192
|
-
tracking_response.response = browser.html
|
|
193
|
-
|
|
194
|
-
html = browser.table(id: 'cphMain_grvLogNotes').inner_html
|
|
195
|
-
html = Nokogiri::HTML(html)
|
|
196
|
-
|
|
197
|
-
api_city_state_zip = browser.element(xpath: '/html/body/form/div[3]/table[2]/tbody/tr[14]/td[1]/span').text
|
|
198
|
-
shipper_location = parse_api_city_state_zip(api_city_state_zip)
|
|
199
|
-
|
|
200
|
-
api_city_state_zip = browser.element(xpath: '/html/body/form/div[3]/table[2]/tbody/tr[14]/td[2]/span').text
|
|
201
|
-
receiver_location = parse_api_city_state_zip(api_city_state_zip)
|
|
202
|
-
|
|
203
|
-
actual_delivery_date = nil
|
|
204
|
-
delivery_appointment_scheduled = false
|
|
205
|
-
scheduled_delivery_date = nil
|
|
206
|
-
ship_time = nil
|
|
207
|
-
|
|
208
|
-
shipment_events = []
|
|
209
|
-
|
|
210
|
-
html.css('tr').each do |tr|
|
|
211
|
-
next if tr.text.include?('DateNotes')
|
|
212
|
-
|
|
213
|
-
event = tr.css('td')[1].text
|
|
214
|
-
event_key = nil
|
|
215
|
-
|
|
216
|
-
EVENTS[:types].each do |key, val|
|
|
217
|
-
if event.downcase.include?(val) && !event.downcase.include?('estimated')
|
|
218
|
-
event_key = key
|
|
219
|
-
break
|
|
220
|
-
end
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
next if event_key.blank?
|
|
224
|
-
|
|
225
|
-
location = nil
|
|
226
|
-
|
|
227
|
-
unless event_key == :delivery_appointment_scheduled
|
|
228
|
-
api_city_state = event.downcase.split(EVENTS.dig(:types, event_key)).last
|
|
229
|
-
api_city_state = api_city_state.downcase.sub(event_key.to_s, '')
|
|
230
|
-
api_city_state = api_city_state.gsub(',', '')
|
|
231
|
-
|
|
232
|
-
location = api_city_state.downcase.include?('carrier') ? nil : parse_api_city_state(api_city_state)
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
api_date_time = tr.css('td')[0].text
|
|
236
|
-
date_time = parse_api_date_time(api_date_time, location)
|
|
237
|
-
|
|
238
|
-
actual_delivery_date = date_time if event_key == :delivered
|
|
239
|
-
delivery_appointment_scheduled = true if event_key == :delivery_appointment_scheduled
|
|
240
|
-
|
|
241
|
-
# API doesn't provide pickup information
|
|
242
|
-
ship_time = date_time if event_key == :arrived_at_terminal && ship_time.blank?
|
|
243
|
-
|
|
244
|
-
shipment_event = ShipmentEvent.new(date_time:, location:, type_code: event_key)
|
|
245
|
-
shipment_events << shipment_event
|
|
246
|
-
end
|
|
188
|
+
actual_delivery_date = shipment_events.find { |event| event.type_code == :delivered }&.date_time
|
|
247
189
|
|
|
248
190
|
# API doesn't provide appointment information on :delivery_appointment_scheduled
|
|
249
|
-
if delivery_appointment_scheduled
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
191
|
+
scheduled_delivery_date = if shipment_events.any? { |event| event.type_code == :delivery_appointment_scheduled }
|
|
192
|
+
estimate = notes.find { |_date_time, event| event.include?('Estimated Delivery Date') }
|
|
193
|
+
parse_api_date(estimate&.first&.split&.first, shipment_events.last.location)
|
|
194
|
+
end
|
|
253
195
|
|
|
254
|
-
|
|
255
|
-
scheduled_delivery_date = parse_api_date(api_date, shipment_events.last.location)
|
|
256
|
-
|
|
257
|
-
break
|
|
258
|
-
end
|
|
259
|
-
end
|
|
260
|
-
|
|
261
|
-
browser.close
|
|
262
|
-
|
|
263
|
-
# API events sometimes appear after delivered
|
|
264
|
-
status = actual_delivery_date.blank? ? shipment_events.last&.type_code : :delivered
|
|
265
|
-
|
|
266
|
-
tracking_response.assign_attributes(
|
|
196
|
+
TrackingResponse.new(
|
|
267
197
|
actual_delivery_date:,
|
|
268
|
-
|
|
269
|
-
|
|
198
|
+
carrier: self,
|
|
199
|
+
destination:,
|
|
200
|
+
origin:,
|
|
201
|
+
response: details.to_html,
|
|
270
202
|
scheduled_delivery_date:,
|
|
271
|
-
|
|
203
|
+
# API doesn't provide pickup information
|
|
204
|
+
ship_time: shipment_events.find { |event| event.type_code == :arrived_at_terminal }&.date_time,
|
|
272
205
|
shipment_events:,
|
|
273
|
-
|
|
206
|
+
# API events sometimes appear after delivered
|
|
207
|
+
status: actual_delivery_date ? :delivered : shipment_events.last&.type_code,
|
|
274
208
|
tracking_number:
|
|
275
209
|
)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def request(verb, url, **)
|
|
213
|
+
response = HTTPClient.request(verb, url, proxy_url:, **)
|
|
214
|
+
raise HTTPError.new(body: response.body, code: response.code) unless response.code == 200
|
|
276
215
|
|
|
277
|
-
|
|
216
|
+
response
|
|
278
217
|
end
|
|
279
218
|
|
|
280
|
-
def request_url(action)
|
|
281
|
-
|
|
282
|
-
|
|
219
|
+
def request_url(action) = "https://#{API[:domain]}#{API.dig(:endpoints, action)}"
|
|
220
|
+
|
|
221
|
+
def search(cookies, page, tracking_number)
|
|
222
|
+
results = submit(cookies, page, 'ctl00$cphMain$btnSearchProNumber' => 'Search', 'ctl00$cphMain$txtProNumber' => tracking_number)
|
|
223
|
+
return if results.at('a[href*="grvInvoice"]').nil?
|
|
224
|
+
|
|
225
|
+
submit(cookies, results, '__EVENTARGUMENT' => 'Select$0', '__EVENTTARGET' => 'ctl00$cphMain$grvInvoice')
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def submit(cookies, page, fields)
|
|
229
|
+
form = page.at('form')
|
|
230
|
+
inputs = form.css('input[name]').reject { |input| ['button', 'image', 'submit'].include?(input['type']) }
|
|
231
|
+
data = inputs.to_h { |input| [input['name'], input['value'].to_s] }.merge(fields)
|
|
232
|
+
|
|
233
|
+
Nokogiri::HTML(request(:post, URI.join(request_url(:login), form['action']).to_s, body: data, cookies:).body)
|
|
283
234
|
end
|
|
284
235
|
end
|
|
285
236
|
end
|
|
@@ -22,6 +22,9 @@ module FreightKit
|
|
|
22
22
|
reset
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
+
# @return [HTTPX::Plugins::Cookies::Jar] A jar for `cookies:`, which keeps the cookies that responses set.
|
|
26
|
+
def cookie_jar = session(nil, cookies: true).make_jar
|
|
27
|
+
|
|
25
28
|
def get(url, **) = request(:get, url, **)
|
|
26
29
|
def logger = @logger || framework_logger || ::Logger.new($stdout, level: ENV.fetch('LOG_LEVEL', 'info'))
|
|
27
30
|
|
|
@@ -32,7 +35,8 @@ module FreightKit
|
|
|
32
35
|
|
|
33
36
|
def post(url, **) = request(:post, url, **)
|
|
34
37
|
|
|
35
|
-
|
|
38
|
+
# @param cookies [HTTPX::Plugins::Cookies::Jar, nil] A jar from {.cookie_jar}. Responses are not cached with it.
|
|
39
|
+
def request(verb, url, body: nil, cookies: nil, headers: {}, proxy_url: nil, query: nil, read_timeout: nil)
|
|
36
40
|
options = { headers: }
|
|
37
41
|
options[:params] = query if query.present?
|
|
38
42
|
options[:timeout] = { read_timeout:, total_request_timeout: TIMEOUTS[:connect_timeout] + read_timeout } if read_timeout
|
|
@@ -44,7 +48,10 @@ module FreightKit
|
|
|
44
48
|
options[:body] = body
|
|
45
49
|
end
|
|
46
50
|
|
|
47
|
-
|
|
51
|
+
client = session(proxy_url.presence, cookies: !cookies.nil?)
|
|
52
|
+
client = client.with(cookies:) if cookies
|
|
53
|
+
response = client.request(verb.to_s.upcase, url, **options)
|
|
54
|
+
client.cookies.each { |cookie| cookies.set(cookie) } if cookies
|
|
48
55
|
raise response.error if response.is_a?(HTTPX::ErrorResponse)
|
|
49
56
|
|
|
50
57
|
Response.new(response)
|
|
@@ -64,13 +71,15 @@ module FreightKit
|
|
|
64
71
|
end
|
|
65
72
|
|
|
66
73
|
# Sessions own their connection pool, so they are built once (per proxy) and reused
|
|
67
|
-
def session(proxy_url)
|
|
68
|
-
@sessions.compute_if_absent(proxy_url) do
|
|
74
|
+
def session(proxy_url, cookies: false)
|
|
75
|
+
@sessions.compute_if_absent([proxy_url, cookies]) do
|
|
69
76
|
session = HTTPX.plugin(:brotli).plugin(Zstd).plugin(:follow_redirects)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
session = if cookies
|
|
78
|
+
session.plugin(:cookies)
|
|
79
|
+
else
|
|
80
|
+
session.plugin(:response_cache, response_cache_store: CacheStore.new(cache)).plugin(Revalidation)
|
|
81
|
+
end
|
|
82
|
+
session = session.plugin(SafeRetries).with(close_on_fork: true, headers: { 'user-agent' => USER_AGENT }, timeout: TIMEOUTS)
|
|
74
83
|
|
|
75
84
|
logger = self.logger
|
|
76
85
|
session = session.with(debug: LogDevice.new(logger), debug_level: 2) if logger.level <= ::Logger::DEBUG
|
|
@@ -5,12 +5,36 @@ module FreightKit
|
|
|
5
5
|
# - {FreightKit::Carrier#pod}
|
|
6
6
|
# - {FreightKit::Carrier#scanned_bol}
|
|
7
7
|
#
|
|
8
|
-
# @attribute
|
|
9
|
-
# @return [
|
|
10
|
-
#
|
|
11
|
-
# @attribute data
|
|
12
|
-
# @return [String] Raw document data.
|
|
8
|
+
# @attribute parts
|
|
9
|
+
# @return [Array<Part>] The document's files in order, such as one image per page, for callers to merge.
|
|
13
10
|
class DocumentResponse < Response
|
|
14
|
-
|
|
11
|
+
# One file of a document, such as a page image or a PDF. Takes `data` or `data_base64`, and derives the other.
|
|
12
|
+
class Part
|
|
13
|
+
# @param content_type [String, nil] The HTTP `Content-Type`, detected from the data when nil.
|
|
14
|
+
# @param data [String, nil] Raw file data.
|
|
15
|
+
# @param data_base64 [String, nil] The data in Base64, which may have line breaks.
|
|
16
|
+
def initialize(content_type: nil, data: nil, data_base64: nil)
|
|
17
|
+
raise ArgumentError, 'data or data_base64 is required' if data.nil? && data_base64.nil?
|
|
18
|
+
|
|
19
|
+
@content_type = content_type
|
|
20
|
+
@data = data
|
|
21
|
+
@data_base64 = data_base64&.gsub(/\s/, '')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def ==(other) = other.is_a?(Part) && content_type == other.content_type && data == other.data
|
|
25
|
+
|
|
26
|
+
# @return [String, nil]
|
|
27
|
+
def content_type = @content_type ||= MimeMagic.by_magic(data)&.type
|
|
28
|
+
|
|
29
|
+
# @return [String] Raw file data.
|
|
30
|
+
def data = @data ||= Base64.strict_decode64(@data_base64)
|
|
31
|
+
|
|
32
|
+
# @return [String] The data in strict Base64.
|
|
33
|
+
def data_base64 = @data_base64 ||= Base64.strict_encode64(@data)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
attr_writer :parts
|
|
37
|
+
|
|
38
|
+
def parts = @parts || []
|
|
15
39
|
end
|
|
16
40
|
end
|
|
@@ -228,7 +228,7 @@ module FreightKit
|
|
|
228
228
|
return document_response
|
|
229
229
|
end
|
|
230
230
|
|
|
231
|
-
document_response.
|
|
231
|
+
document_response.parts = [DocumentResponse::Part.new(content_type: response.headers['content-type'], data: response.body)]
|
|
232
232
|
document_response
|
|
233
233
|
end
|
|
234
234
|
|
|
@@ -285,7 +285,7 @@ module FreightKit
|
|
|
285
285
|
return document_response.tap { |response| response.error = DocumentNotFoundError.new } if document.nil?
|
|
286
286
|
|
|
287
287
|
response = download(document[:url])
|
|
288
|
-
document_response.tap { |item| item.
|
|
288
|
+
document_response.tap { |item| item.parts = [DocumentResponse::Part.new(content_type: response.content_type, data: response.body)] }
|
|
289
289
|
end
|
|
290
290
|
|
|
291
291
|
# URLs are signed and expire after 5 minutes, so they are downloaded right away, without the API key.
|
|
@@ -79,13 +79,12 @@ module FreightKit
|
|
|
79
79
|
invoice_response.tap do |response|
|
|
80
80
|
response.assign_attributes(
|
|
81
81
|
balance_cents: cents(data[:invoice_balance]),
|
|
82
|
-
content_type: pdf.content_type,
|
|
83
82
|
currency: 'USD',
|
|
84
|
-
data: pdf.data,
|
|
85
83
|
error: pdf.error,
|
|
86
84
|
issued_on: date(data[:invoice_date]),
|
|
87
85
|
lines: data[:invoice_details].to_a.map { |detail| invoice_line(detail) },
|
|
88
86
|
number: text(data[:invoice_number]) || number,
|
|
87
|
+
parts: pdf.parts,
|
|
89
88
|
total_cents: cents(data[:invoice_amount])
|
|
90
89
|
)
|
|
91
90
|
end
|
|
@@ -184,7 +183,7 @@ module FreightKit
|
|
|
184
183
|
save_request(request)
|
|
185
184
|
read_timeout = api.dig(:read_timeouts, action)
|
|
186
185
|
response = HTTPClient.request(request[:method], url, **request.slice(:body, :headers, :query), proxy_url:, read_timeout:)
|
|
187
|
-
raise HTTPError.new(body: response.body, code: response.code) unless response.code
|
|
186
|
+
raise HTTPError.new(body: response.body, code: response.code) unless [200, 204].include?(response.code)
|
|
188
187
|
|
|
189
188
|
response
|
|
190
189
|
end
|
|
@@ -214,9 +213,9 @@ module FreightKit
|
|
|
214
213
|
def document(action, params)
|
|
215
214
|
response = commit(action, params)
|
|
216
215
|
document_response = DocumentResponse.new(request: last_request)
|
|
217
|
-
return document_response.tap { |item| item.error = DocumentNotFoundError.new } unless response.body.start_with?('%PDF')
|
|
216
|
+
return document_response.tap { |item| item.error = DocumentNotFoundError.new } unless response.body.to_s.start_with?('%PDF')
|
|
218
217
|
|
|
219
|
-
document_response.tap { |item| item.
|
|
218
|
+
document_response.tap { |item| item.parts = [DocumentResponse::Part.new(data: response.body)] }
|
|
220
219
|
end
|
|
221
220
|
|
|
222
221
|
def invoice_line(detail)
|
|
@@ -333,8 +333,7 @@ module FreightKit
|
|
|
333
333
|
return document_response
|
|
334
334
|
end
|
|
335
335
|
|
|
336
|
-
|
|
337
|
-
document_response.assign_attributes(content_type: 'application/pdf', data: decoded_pdf_data)
|
|
336
|
+
document_response.parts = [DocumentResponse::Part.new(data_base64: base64_document_data)]
|
|
338
337
|
|
|
339
338
|
document_response
|
|
340
339
|
end
|