Skip to content

Commit

Permalink
Feat: Support downloading Statistic xlsx files
Browse files Browse the repository at this point in the history
  • Loading branch information
zealot128 committed Oct 14, 2024
1 parent e71099d commit c44635d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,23 @@ end
```ruby
#!/usr/bin/env ruby
require 'ba_upload'
connection = BaUpload.open_connection(file_path: 'config/Zertifikat-1XXXX.p12', passphrase: 'YOURPASSPHRASE')
BaUpload.open_connection(file_path: 'config/Zertifikat-1XXXX.p12', passphrase: 'YOURPASSPHRASE')
connection.upload(file: File.open(ARGV[0]))
```

Save to a file and just run it with the xml file as argument
Save to a file and just run it with the xml file as argument. It's important, because the file upload of BA validates the original file name.


### Memory safety

because we are using Mechanize under the hood, it's a good idea to always close sockets, (with connection.shutdown), or use the open helper:

```ruby
BaUpload.open(file_path: 'config/Zertifikat-1XXXX.p12', passphrase:) do |c|
c.upload(file: file_path)
c.error_files....
end
```


### Downloading "misc" files
Expand All @@ -71,6 +83,19 @@ connection.misc.each do |link|
end
```

### Downloading 'Statistiken' xlsx reports

Download XLSX reports (validation errors and "Stellenübersicht") from BA; You might use another Gem, like roo, axlsx etc. to parse those files if necessary.

```ruby
connection.statistics.each do |link|
link.tempfile

# or:
link.read
end
```

### Usage with multiple client certificats

Since September 2022, users with multiple client certificats issued to the same email address need to provide their respective partner ID when using the API.
Expand Down Expand Up @@ -255,6 +280,7 @@ xsd.validate(doc)

<details>
<summary>Generate a filename</summary>

```ruby
# for historic reasons, you could transmit a bunch of files with the same timestamp using an index/offset, but usually, just putting 0 here should be enought
index = 0
Expand All @@ -264,6 +290,7 @@ flag = ended ? "E" : "C"
date = Time.zone.now.strftime "%Y-%m-%d_%H-%M-%S_F#{'%03d' % (index + 1)}#{flag}"
"DS#{SUPPLIER_ID}_#{date}.xml"
```

</details>

- Upload the file using this Gem. You should wait a "couple of minutes" (tip: enqueue a activeJob for 10 minutes later), to fetch the resulting **error file**, and analyse that.
Expand Down
7 changes: 7 additions & 0 deletions lib/ba_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def self.open_connection(file_path:, passphrase:)
cert = BaUpload.export_certificate(file_path: file_path, passphrase: passphrase)
BaUpload::Connection.new(cert[:key], cert[:cert], cert[:ca_cert])
end

def self.open(file_path:, passphrase:, &block)
conn = BaUpload.open_connection(file_path: file_path, passphrase: passphrase)
block.call(conn)
ensure
conn.shutdown
end
end

require 'ba_upload/connection'
13 changes: 11 additions & 2 deletions lib/ba_upload/connection.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'ba_upload/error_file'
require 'ba_upload/statistic_file'
module BaUpload
class Connection
attr_reader :m
Expand All @@ -16,21 +17,29 @@ def initialize(key_file, cert_file, ca_cert_file)

def upload(file: nil, partner_id: nil)
url = base_url(partner_id) + "in/"
m.get url
m.get(url)
form = m.page.forms.first
form.file_uploads.first.file_name = file
form.submit
end

def error_files(partner_id: nil)
url = base_url(partner_id)
m.get url
m.get(url)
links = m.page.links_with(text: /ESP|ESV/)
links.map do |link|
ErrorFile.new(link)
end
end

def statistics(partner_id: nil)
url = base_url(partner_id) + "Statistiken"
m.get(url)
m.page.links_with(text: /xlsx/).map do |link|
StatisticFile.new(link)
end
end

def misc(partner_id: nil)
url = base_url(partner_id)
m.get url
Expand Down
18 changes: 18 additions & 0 deletions lib/ba_upload/statistic_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'ba_upload/error_file'
module BaUpload
class StatisticFile < ErrorFile
def tempfile
tf = Tempfile.new(['statistic_file', '.xlsx'])
tf.binmode
tf.write(read)
tf.flush
tf.rewind
tf
end

def read
@mechanize_link.click.body
end
end
end

2 changes: 1 addition & 1 deletion lib/ba_upload/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module BaUpload
VERSION = "0.4.0"
VERSION = "0.5.0"
end

0 comments on commit c44635d

Please sign in to comment.