freight_kit 0.1.29 → 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: bf3ea6e676c65d106c9054209d3dc987b68183fc02768ddaf17eefbae715d51e
4
- data.tar.gz: e6a948691289653bf0d800b0cd7a16a7965d8be9024125f4b7816f730b08a8aa
3
+ metadata.gz: 8db7693b75177a10d1a2df69f021dde4ba091293c9255a3eb2044df6cf7d86d6
4
+ data.tar.gz: 4a4473c56e05a33996538080f2efb27229b9bdcd0d77a4616afffae5cc331434
5
5
  SHA512:
6
- metadata.gz: 7723ff1d4188bd6068d8f643e96cd5dab5d9fb0a09db727f69d881bb6e387c44658d9c543fbb6b74a2111e9ac9921dd32e28e02a1f1f54f75dc4b57a57bf5731
7
- data.tar.gz: 62d7a36e391bf9274380af3ecb5c03bbc4909ecf603c07d21424bfc14f1d42fb9b2c15fb56dcafc25064019293d836591a55b8588d22b537a5f5d1aaaf47df2e
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.28)
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)
@@ -70,6 +70,8 @@ GEM
70
70
  date (3.5.1)
71
71
  diff-lcs (1.5.0)
72
72
  drb (2.2.3)
73
+ factory_bot (6.7.0)
74
+ activesupport (>= 7.0.0)
73
75
  faker (3.8.0)
74
76
  i18n (>= 1.8.11, < 2)
75
77
  faraday (2.14.2)
@@ -254,6 +256,7 @@ PLATFORMS
254
256
  arm64-darwin-27
255
257
 
256
258
  DEPENDENCIES
259
+ factory_bot (~> 6.7.0)
257
260
  faker (~> 3.8.0)
258
261
  freight_kit!
259
262
  rake (~> 13.4.2)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.29
1
+ 0.1.31
data/freight_kit.gemspec CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.files = Dir['[A-Z]*'] + Dir['configuration/*/*.yml'] + Dir['lib/**/*'] + Dir['schemas/*'] + Dir['test/**/*']
15
15
  spec.require_paths = ['lib']
16
16
 
17
+ spec.add_development_dependency('factory_bot', '~> 6.7.0')
17
18
  spec.add_development_dependency('faker', '~> 3.8.0')
18
19
  spec.add_development_dependency('rake', '~> 13.4.2')
19
20
  spec.add_development_dependency('redcarpet', '~> 3.6.0') # for yard
@@ -326,7 +326,7 @@ module FreightKit
326
326
  def initialize(credentials, customer_location: nil, tariff: nil)
327
327
  credentials = [credentials] if credentials.is_a?(Credential)
328
328
 
329
- if credentials.map(&:class).uniq != [Credential]
329
+ if !credentials.is_a?(Array) || credentials.any? { |credential| !credential.is_a?(Credential) }
330
330
  message = "#{name}#new: `credentials` must be a Credential or Array of Credential"
331
331
  raise ArgumentError, message
332
332
  end
@@ -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.29
4
+ version: 0.1.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Third Party Transportation Systems LLC
@@ -12,6 +12,20 @@ bindir: bin
12
12
  cert_chain: []
13
13
  date: 1980-01-02 00:00:00.000000000 Z
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: factory_bot
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: 6.7.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: 6.7.0
15
29
  - !ruby/object:Gem::Dependency
16
30
  name: faker
17
31
  requirement: !ruby/object:Gem::Requirement
@@ -954,7 +968,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
954
968
  - !ruby/object:Gem::Version
955
969
  version: '0'
956
970
  requirements: []
957
- rubygems_version: 4.0.10
971
+ rubygems_version: 4.0.20
958
972
  specification_version: 4
959
973
  summary: Freight carrier API and website abstraction library for transportation management
960
974
  systems (TMS)