datapipeline 2.0.0: Gem aus dem Projekt etl herausgelöst

This commit is contained in:
wkh
2026-10-08 15:30:02 +02:00
commit cce7b03ca9
56 changed files with 5205 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
#
# Kunden aus CSV prüfen und in Paketen per POST an eine REST-API senden
#
# ruby examples/demo_api.rb &
# bin/etl run examples/rest_schreiben.rb [--url=http://127.0.0.1:4567] [--dry-run]
#
require 'fileutils'
require 'datapipeline'
API = DataPipeline.params.fetch(:url, 'http://127.0.0.1:4567')
AUTH = "Bearer #{ENV.fetch('DEMO_API_TOKEN', 'demo-token')}"
DATA = File.expand_path('data', __dir__)
out = DataPipeline.params.fetch(:out, 'tmp')
FileUtils.mkdir_p(out)
DataPipeline.new do
version 'rest_schreiben 1.0'
read container(:csv, file: "#{DATA}/kunden.csv", csv: { col_sep: ';' })
# vor dem Senden prüfen: was die API ablehnen würde, landet hier in der Fehlerdatei
on_error :collect, to: container(:csv, file: "#{out}/import_fehler.csv")
validate 'kundennr' => { required: true, format: /\A\d{6}\z/ }, 'name' => { required: true }
filter { |row| row['status'] == 'aktiv' }
map do |row|
{ 'kundennr' => row['kundennr'], 'name' => row['name'].strip, 'land' => row['land'], 'umsatz' => row['umsatz'].to_f }
end
# POST /api/import mit je 2 Kunden als JSON-Array; im Dry-Run wird nichts gesendet
write container(:rest, url: "#{API}/api/import", auth: AUTH, batch_size: 2)
on_finish do |stats|
puts stats.dry_run ? 'Dry-Run: nichts gesendet' : "gesendet: #{stats.written} Kunden in #{(stats.written / 2.0).ceil} Paketen"
end
end.run