freight_kit 0.1.30 → 0.1.31

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4198d2ed3080d8fbaf6ebfd9541f0ac10acadb46b93c68940e11b095ef297625
4
- data.tar.gz: cb64622fd0d657d98baf2a7da9f26b7a99f8f4c52bfb677a739db983ff16a52f
3
+ metadata.gz: 8db7693b75177a10d1a2df69f021dde4ba091293c9255a3eb2044df6cf7d86d6
4
+ data.tar.gz: 4a4473c56e05a33996538080f2efb27229b9bdcd0d77a4616afffae5cc331434
5
5
  SHA512:
6
- metadata.gz: 0c26f9666370d8598132ff2095c588df139188a6849c3e310fca67f4a9dd984131cb552cdff1b9795dbb71cabecb3b145a648302d473c0719d0cefd4ae78e2ee
7
- data.tar.gz: d896cea40ae9744ca150162f573cc855f30810e75f9d82aa6bc79017638d0245ec8f1512901e084062b5bcbdd15aa3e906af9ee4638bc354186940eb8cde2680
6
+ metadata.gz: 5494d549e10785a1dc6cc8cfadb24180f128192c221caee99c4bff270d9bd7d4a8fa76e24320b043fcd1dfd9d773fac8d9f922f246abf8fd86ba9fc5ab2b3cf1
7
+ data.tar.gz: 23ab4f66b1f9b67fa8defd25a361fe8e1437e8e23f9dc54d3cd7016631f7895c7652f66efd20b2a48b1f0f86a4b502c08a5a844500c143a740563d47f132da80
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- freight_kit (0.1.29)
4
+ freight_kit (0.1.30)
5
5
  StreetAddress (~> 2.0)
6
6
  active_utils (>= 3.3.1, < 3.7.0)
7
7
  activemodel (>= 7.1, < 8.1.5)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.30
1
+ 0.1.31
@@ -114,7 +114,11 @@ module FreightKit
114
114
  def website_url = 'https://arcb.com'
115
115
  end
116
116
 
117
- API = { domain: 'www.abfs.com', endpoints: { rates: '/xml/aquotexml.asp' }, methods: { rates: 'GET' } }.freeze
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.deep_transform_keys! { |key| key.downcase.to_sym }[:abf] if response&.parsed_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.assign_attributes(content_type: response.headers['content-type'], data: response.body)
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
 
@@ -327,7 +327,7 @@ module FreightKit
327
327
  end
328
328
 
329
329
  decoded_pdf_data = Base64.decode64(document['Content'])
330
- document_response.assign_attributes(content_type: 'application/pdf', data: decoded_pdf_data)
330
+ document_response.parts = [DocumentResponse::Part.new(content_type: 'application/pdf', data: decoded_pdf_data)]
331
331
 
332
332
  document_response
333
333
  end
@@ -379,7 +379,7 @@ module FreightKit
379
379
  return document_response
380
380
  end
381
381
 
382
- document_response.assign_attributes(content_type: response.headers['content-type'], data: response.body)
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, request: last_request)
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:)
@@ -452,7 +452,7 @@ module FreightKit
452
452
 
453
453
  data = Base64.decode64(doc.css('img').first['src'].split('data:image/jpg;base64,').last)
454
454
 
455
- document_response.assign_attributes(content_type: 'image/jpeg', data:)
455
+ document_response.parts = [DocumentResponse::Part.new(content_type: 'image/jpeg', data:)]
456
456
  document_response
457
457
  end
458
458
 
@@ -409,7 +409,7 @@ module FreightKit
409
409
 
410
410
  data = Base64.decode64(file_bytes)
411
411
 
412
- document_response.assign_attributes(content_type: 'application/pdf', data:)
412
+ document_response.parts = [DocumentResponse::Part.new(content_type: 'application/pdf', data:)]
413
413
  document_response
414
414
  end
415
415
 
@@ -528,7 +528,7 @@ module FreightKit
528
528
  return document_response
529
529
  end
530
530
 
531
- document_response.assign_attributes(content_type: response.headers['content-type'], data: response.body)
531
+ document_response.parts = [DocumentResponse::Part.new(content_type: response.headers['content-type'], data: response.body)]
532
532
  document_response
533
533
  end
534
534
 
@@ -141,7 +141,7 @@ module FreightKit
141
141
  return document_response
142
142
  end
143
143
 
144
- document_response.assign_attributes(content_type: response.headers['content-type'], data: response.body)
144
+ document_response.parts = [DocumentResponse::Part.new(content_type: response.headers['content-type'], data: response.body)]
145
145
  document_response
146
146
  end
147
147
 
@@ -5,12 +5,20 @@ module FreightKit
5
5
  # - {FreightKit::Carrier#pod}
6
6
  # - {FreightKit::Carrier#scanned_bol}
7
7
  #
8
- # @attribute content_type
9
- # @return [String] The HTTP `Content-Type`
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
- attr_accessor :content_type, :data
11
+ # One file of a document, such as a page image or a PDF.
12
+ #
13
+ # @attribute content_type
14
+ # @return [String] The HTTP `Content-Type`.
15
+ #
16
+ # @attribute data
17
+ # @return [String] Raw file data.
18
+ Part = Data.define(:content_type, :data)
19
+
20
+ attr_writer :parts
21
+
22
+ def parts = @parts || []
15
23
  end
16
24
  end
@@ -228,7 +228,7 @@ module FreightKit
228
228
  return document_response
229
229
  end
230
230
 
231
- document_response.assign_attributes(content_type: response.headers['content-type'], data: response.body)
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.assign_attributes(content_type: response.content_type, data: response.body) }
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
@@ -216,7 +215,7 @@ module FreightKit
216
215
  document_response = DocumentResponse.new(request: last_request)
217
216
  return document_response.tap { |item| item.error = DocumentNotFoundError.new } unless response.body.start_with?('%PDF')
218
217
 
219
- document_response.tap { |item| item.assign_attributes(content_type: 'application/pdf', data: response.body) }
218
+ document_response.tap { |item| item.parts = [DocumentResponse::Part.new(content_type: 'application/pdf', data: response.body)] }
220
219
  end
221
220
 
222
221
  def invoice_line(detail)
@@ -334,7 +334,7 @@ module FreightKit
334
334
  end
335
335
 
336
336
  decoded_pdf_data = Base64.decode64(base64_document_data)
337
- document_response.assign_attributes(content_type: 'application/pdf', data: decoded_pdf_data)
337
+ document_response.parts = [DocumentResponse::Part.new(content_type: 'application/pdf', data: decoded_pdf_data)]
338
338
 
339
339
  document_response
340
340
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freight_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.30
4
+ version: 0.1.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Third Party Transportation Systems LLC