freight_kit 0.1.31 → 0.1.33
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/cross_country_freight_solutions.rb +1 -2
- data/lib/freight_kit/carriers/roadrunner_transportation_services.rb +12 -21
- data/lib/freight_kit/carriers/t_force_worldwide.rb +1 -3
- data/lib/freight_kit/carriers/western_regional_delivery_service.rb +94 -147
- data/lib/freight_kit/http_client.rb +17 -8
- data/lib/freight_kit/models/document_response.rb +24 -8
- data/lib/freight_kit/platforms/road_vision.rb +3 -3
- 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: 751810555f9e684de775dbb7be33c9860435ee093a1c372407c39c17ec18bc5e
|
|
4
|
+
data.tar.gz: '0548b0bfd294c843e140913a44319f81c9c63388a6fd53ab2ecd8ca75ec1bf85'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1e1aeb2e18f027ee2852fb7aaa3e16604ba62820fb0357ee1009251f96d8250e3940e186294d524aaae41a44d47d5ea34c58860c1709cab14b556a20d0185072
|
|
7
|
+
data.tar.gz: 3ef028c43bf9bce202f2343c5df1495b39adf5470f91ee99231889abe24865b3a77ded67f2a7247341e7bd40f47ff1110259df162f03e52cad4ced67264183f3
|
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.1.
|
|
1
|
+
0.1.33
|
|
@@ -326,8 +326,7 @@ module FreightKit
|
|
|
326
326
|
return document_response
|
|
327
327
|
end
|
|
328
328
|
|
|
329
|
-
|
|
330
|
-
document_response.parts = [DocumentResponse::Part.new(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
|
|
@@ -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.parts =
|
|
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.parts = [DocumentResponse::Part.new(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
|
|
|
@@ -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,9 +64,7 @@ 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
|
|
71
69
|
EVENTS = {
|
|
72
70
|
types: {
|
|
@@ -78,25 +76,56 @@ module FreightKit
|
|
|
78
76
|
}.freeze
|
|
79
77
|
|
|
80
78
|
# Tracking
|
|
81
|
-
def find_tracking_info(tracking_number)
|
|
79
|
+
def find_tracking_info(tracking_number)
|
|
80
|
+
cookies = HTTPClient.cookie_jar
|
|
81
|
+
details = search(cookies, fetch(cookies, request_url(:track)), tracking_number)
|
|
82
|
+
return TrackingResponse.new(carrier: self, error: ShipmentNotFoundError.new, tracking_number:) if details.nil?
|
|
83
|
+
|
|
84
|
+
parse_tracking_response(tracking_number, details)
|
|
85
|
+
end
|
|
82
86
|
|
|
83
87
|
# Documents
|
|
84
|
-
def pod(tracking_number) =
|
|
88
|
+
def pod(tracking_number) = document(:pod, tracking_number)
|
|
89
|
+
def scanned_bol(tracking_number) = document(:bol, tracking_number)
|
|
85
90
|
|
|
86
91
|
protected
|
|
87
92
|
|
|
88
|
-
def
|
|
93
|
+
def document(type, tracking_number)
|
|
94
|
+
document_response = DocumentResponse.new(request: { tracking_number:, type: })
|
|
95
|
+
cookies = HTTPClient.cookie_jar
|
|
96
|
+
details = search(cookies, log_in(cookies), tracking_number)
|
|
97
|
+
available = details&.at('a[href*="btnImageYes"]').present? && (type == :bol || details.at('#cphMain_lblActDelivery')&.text.present?)
|
|
98
|
+
return document_response.tap { |item| item.error = DocumentNotFoundError.new } unless available
|
|
99
|
+
|
|
100
|
+
submit(cookies, details, '__EVENTTARGET' => 'ctl00$cphMain$btnImageYes')
|
|
101
|
+
viewer = fetch(cookies, request_url(:documents))
|
|
102
|
+
count = viewer.at('#cphMain_lblAmount').text.to_i
|
|
103
|
+
return document_response.tap { |item| item.error = DocumentNotFoundError.new } if count < (type == :pod ? 2 : 1)
|
|
104
|
+
|
|
105
|
+
(type == :pod ? count - 1 : 0).times { viewer = submit(cookies, viewer, 'ctl00$cphMain$btnNext' => 'Next') }
|
|
106
|
+
document_response.tap { |item| item.parts = [download(cookies, viewer.at('#cphMain_imgImage')['src'])] }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def download(cookies, path)
|
|
110
|
+
response = request(:get, URI.join(request_url(:login), path).to_s, cookies:)
|
|
111
|
+
|
|
112
|
+
DocumentResponse::Part.new(content_type: response.content_type, data: response.body)
|
|
113
|
+
end
|
|
89
114
|
|
|
90
|
-
def
|
|
91
|
-
url = request_url(action)
|
|
115
|
+
def fetch(cookies, url) = Nokogiri::HTML(request(:get, url, cookies:).body)
|
|
92
116
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
117
|
+
def log_in(cookies)
|
|
118
|
+
credential = fetch_credential(:website)
|
|
119
|
+
page = submit(
|
|
120
|
+
cookies,
|
|
121
|
+
fetch(cookies, request_url(:login)),
|
|
122
|
+
'ctl00$cphMain$btnLogIn' => 'Log In',
|
|
123
|
+
'ctl00$cphMain$txtPassword' => credential.password,
|
|
124
|
+
'ctl00$cphMain$txtUserName' => credential.username
|
|
125
|
+
)
|
|
126
|
+
raise InvalidCredentialsError if page.text.include?('Username or password is invalid.')
|
|
98
127
|
|
|
99
|
-
|
|
128
|
+
page
|
|
100
129
|
end
|
|
101
130
|
|
|
102
131
|
def parse_api_city_state(str)
|
|
@@ -130,156 +159,74 @@ module FreightKit
|
|
|
130
159
|
::FreightKit::DateTime.new(local_date_time:, location:)
|
|
131
160
|
end
|
|
132
161
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
rescue StandardError => e
|
|
140
|
-
document_response.error = e
|
|
141
|
-
return document_response
|
|
162
|
+
def parse_tracking_response(tracking_number, details)
|
|
163
|
+
destination = parse_api_city_state_zip(details.at('#cphMain_lblConsCSZ')&.text&.squish)
|
|
164
|
+
origin = parse_api_city_state_zip(details.at('#cphMain_lblShipperCSZ')&.text&.squish)
|
|
165
|
+
notes = details.css('#cphMain_grvLogNotes tr').filter_map do |row|
|
|
166
|
+
cells = row.css('td').map { |cell| cell.text.squish }
|
|
167
|
+
cells if cells.size == 2
|
|
142
168
|
end
|
|
143
169
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
def parse_pod_response(tracking_number)
|
|
149
|
-
selenoid_credentials = fetch_credential(:selenoid)
|
|
150
|
-
website_credentials = fetch_credential(:website)
|
|
151
|
-
|
|
152
|
-
browser = Watir::Browser.new(*selenoid_credentials.watir_args)
|
|
153
|
-
browser.goto(build_url(:pod))
|
|
154
|
-
|
|
155
|
-
browser.text_field(name: 'ctl00$cphMain$txtUserName').set(website_credentials.username)
|
|
156
|
-
browser.text_field(name: 'ctl00$cphMain$txtPassword').set(website_credentials.password)
|
|
157
|
-
browser.button(name: 'ctl00$cphMain$btnLogIn').click
|
|
170
|
+
shipment_events = notes.filter_map do |date_time, event|
|
|
171
|
+
next if event.downcase.include?('estimated')
|
|
158
172
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
raise InvalidCredentialsError
|
|
162
|
-
end
|
|
173
|
+
type_code, text = EVENTS[:types].find { |_key, value| event.downcase.include?(value) }
|
|
174
|
+
next if type_code.nil?
|
|
163
175
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
176
|
+
location = unless type_code == :delivery_appointment_scheduled
|
|
177
|
+
city_state = event.downcase.split(text).last.delete(',')
|
|
178
|
+
parse_api_city_state(city_state) unless city_state.include?('carrier')
|
|
179
|
+
end
|
|
168
180
|
|
|
169
|
-
|
|
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')
|
|
181
|
+
ShipmentEvent.new(date_time: parse_api_date_time(date_time, location), location:, type_code:)
|
|
174
182
|
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
183
|
|
|
213
|
-
|
|
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
|
|
184
|
+
actual_delivery_date = shipment_events.find { |event| event.type_code == :delivered }&.date_time
|
|
247
185
|
|
|
248
186
|
# API doesn't provide appointment information on :delivery_appointment_scheduled
|
|
249
|
-
if delivery_appointment_scheduled
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
api_date = tr.css('td')[0].text.split&.first
|
|
255
|
-
scheduled_delivery_date = parse_api_date(api_date, shipment_events.last.location)
|
|
187
|
+
scheduled_delivery_date = if shipment_events.any? { |event| event.type_code == :delivery_appointment_scheduled }
|
|
188
|
+
estimate = notes.find { |_date_time, event| event.include?('Estimated Delivery Date') }
|
|
189
|
+
parse_api_date(estimate&.first&.split&.first, shipment_events.last.location)
|
|
190
|
+
end
|
|
256
191
|
|
|
257
|
-
|
|
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(
|
|
192
|
+
TrackingResponse.new(
|
|
267
193
|
actual_delivery_date:,
|
|
268
|
-
|
|
269
|
-
|
|
194
|
+
carrier: self,
|
|
195
|
+
destination:,
|
|
196
|
+
origin:,
|
|
197
|
+
response: details.to_html,
|
|
270
198
|
scheduled_delivery_date:,
|
|
271
|
-
|
|
199
|
+
# API doesn't provide pickup information
|
|
200
|
+
ship_time: shipment_events.find { |event| event.type_code == :arrived_at_terminal }&.date_time,
|
|
272
201
|
shipment_events:,
|
|
273
|
-
|
|
202
|
+
# API events sometimes appear after delivered
|
|
203
|
+
status: actual_delivery_date ? :delivered : shipment_events.last&.type_code,
|
|
274
204
|
tracking_number:
|
|
275
205
|
)
|
|
206
|
+
end
|
|
276
207
|
|
|
277
|
-
|
|
208
|
+
def request(verb, url, **)
|
|
209
|
+
response = HTTPClient.request(verb, url, proxy_url:, **)
|
|
210
|
+
raise HTTPError.new(body: response.body, code: response.code) unless response.code == 200
|
|
211
|
+
|
|
212
|
+
response
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def request_url(action) = "https://#{API[:domain]}#{API.dig(:endpoints, action)}"
|
|
216
|
+
|
|
217
|
+
def search(cookies, page, tracking_number)
|
|
218
|
+
results = submit(cookies, page, 'ctl00$cphMain$btnSearchProNumber' => 'Search', 'ctl00$cphMain$txtProNumber' => tracking_number)
|
|
219
|
+
return if results.at('a[href*="grvInvoice"]').nil?
|
|
220
|
+
|
|
221
|
+
submit(cookies, results, '__EVENTARGUMENT' => 'Select$0', '__EVENTTARGET' => 'ctl00$cphMain$grvInvoice')
|
|
278
222
|
end
|
|
279
223
|
|
|
280
|
-
def
|
|
281
|
-
|
|
282
|
-
|
|
224
|
+
def submit(cookies, page, fields)
|
|
225
|
+
form = page.at('form')
|
|
226
|
+
inputs = form.css('input[name]').reject { |input| ['button', 'image', 'submit'].include?(input['type']) }
|
|
227
|
+
data = inputs.to_h { |input| [input['name'], input['value'].to_s] }.merge(fields)
|
|
228
|
+
|
|
229
|
+
Nokogiri::HTML(request(:post, URI.join(request_url(:login), form['action']).to_s, body: data, cookies:).body)
|
|
283
230
|
end
|
|
284
231
|
end
|
|
285
232
|
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
|
|
@@ -8,14 +8,30 @@ module FreightKit
|
|
|
8
8
|
# @attribute parts
|
|
9
9
|
# @return [Array<Part>] The document's files in order, such as one image per page, for callers to merge.
|
|
10
10
|
class DocumentResponse < Response
|
|
11
|
-
# One file of a document, such as a page image or a PDF.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
|
19
35
|
|
|
20
36
|
attr_writer :parts
|
|
21
37
|
|
|
@@ -183,7 +183,7 @@ module FreightKit
|
|
|
183
183
|
save_request(request)
|
|
184
184
|
read_timeout = api.dig(:read_timeouts, action)
|
|
185
185
|
response = HTTPClient.request(request[:method], url, **request.slice(:body, :headers, :query), proxy_url:, read_timeout:)
|
|
186
|
-
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)
|
|
187
187
|
|
|
188
188
|
response
|
|
189
189
|
end
|
|
@@ -213,9 +213,9 @@ module FreightKit
|
|
|
213
213
|
def document(action, params)
|
|
214
214
|
response = commit(action, params)
|
|
215
215
|
document_response = DocumentResponse.new(request: last_request)
|
|
216
|
-
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')
|
|
217
217
|
|
|
218
|
-
document_response.tap { |item| item.parts = [DocumentResponse::Part.new(
|
|
218
|
+
document_response.tap { |item| item.parts = [DocumentResponse::Part.new(data: response.body)] }
|
|
219
219
|
end
|
|
220
220
|
|
|
221
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.parts = [DocumentResponse::Part.new(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
|