banco 1.0.3 → 2.0.0
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/CHANGELOG.md +45 -0
- data/README.md +141 -0
- data/bin/banco +95 -44
- data/lib/banco/bank_format.rb +67 -0
- data/lib/banco/cli.rb +92 -0
- data/lib/banco/file_picker.rb +45 -0
- data/lib/banco/formatter/base.rb +37 -0
- data/lib/banco/formatter/csv_formatter.rb +21 -0
- data/lib/banco/formatter/json_formatter.rb +29 -0
- data/lib/banco/formatter/text.rb +143 -0
- data/lib/banco/grouper.rb +50 -0
- data/lib/banco/parser.rb +39 -0
- data/lib/banco/report.rb +10 -0
- data/lib/banco/transaction.rb +46 -25
- data/lib/banco/version.rb +5 -0
- data/lib/banco.rb +17 -0
- metadata +231 -30
- data/README +0 -12
- data/lib/banco/get_file.rb +0 -43
- data/lib/banco/reader.rb +0 -64
- data/lib/banco/reporter.rb +0 -74
- data/lib/banco/viewable.rb +0 -181
- data/spec/banco/reader_spec.rb +0 -16
- data/spec/banco/reporter_spec.rb +0 -55
- data/spec/banco/transaction_spec.rb +0 -49
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1a0ed6a5911c8dc0c9e17a3b8ba2bf26ab89aa34497ebd47a8231d07cbb2c476
|
|
4
|
+
data.tar.gz: cd21ee3ddc4b78443e92283317b91b9bccc1b1b80d49ac588a331b91b37f38ea
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c8088fda6603bcea15842bbd2a1fad2942fdc0532f0cb130e89732b2dacca5d4a276af78f2b69729d1878a5d40169249504e94cb6701eb77e32f46c36674a47b
|
|
7
|
+
data.tar.gz: 982967cdf8772f3c5c95648efc6093f9ad3b901cf0c12e3d64f5d515afb728d127c1c6578fc3e23c56d61445ab20f4060492744c70b1386fbf36c7ab688dc4d0
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
6
|
+
|
|
7
|
+
## [2.0.0] — 2026-10-27
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `Transaction` is now a `Data.define` value object — frozen, comparable by value, keyword-constructed
|
|
12
|
+
- `Parser` — single-responsibility CSV reader; raises `ArgumentError` on bad money values or missing files
|
|
13
|
+
- `Grouper` — pure aggregation: `Array<Transaction>` → `Report`; accepts `from:`/`to:` date filters
|
|
14
|
+
- `Report` — `Data.define` value object holding all computed totals; no display logic
|
|
15
|
+
- `Formatter::Base` with `inherited` self-registration hook — adding a formatter auto-updates `--format` validation and `--help` output
|
|
16
|
+
- `Formatter::Text` — all display logic in one place; interactive menu returns Strings (no direct IO)
|
|
17
|
+
- `Formatter::JSON` and `Formatter::CSV` — pipe-friendly output; program exits after printing
|
|
18
|
+
- `CLI` — full `OptionParser` interface: `--file`, `--format`, `--bank`, `--prefix-length`, `--header`, `--from`, `--to`, `--version`, `--help`
|
|
19
|
+
- `FilePicker` — numbered CSV list when no `--file` given; testable via stdin/stdout injection
|
|
20
|
+
- `BankFormat` DSL — `BankFormat.define(:name) { ... }` via `instance_eval`; three built-ins (`:legacy`, `:split_columns`, `:signed_amount`); user formats load from `~/.banco/formats.rb`
|
|
21
|
+
- `--from`/`--to` date filtering; dates parsed using the selected bank format's `date_fmt`
|
|
22
|
+
- `Transaction#parsed_date` — `Date` object computed at parse time from `fmt.date_fmt`
|
|
23
|
+
- GitHub Actions CI matrix across Ruby 3.2, 3.3, 3.4
|
|
24
|
+
- RuboCop + rubocop-rspec + rubocop-performance enforced on every push
|
|
25
|
+
- `spec/fixtures/` synthetic CSV data; no real bank statements in the repo
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- Money stored as integer pence throughout (`credit_pence`/`debit_pence`); `bigdecimal` runtime dependency removed
|
|
30
|
+
- `bin/banco` uses `CLI.parse` + `FilePicker.pick` instead of interactive stdin prompting at startup
|
|
31
|
+
- Non-text output formats (`--format json`, `--format csv`) write to stdout and exit — pipe-friendly
|
|
32
|
+
- `required_ruby_version` tightened to `>= 3.2`
|
|
33
|
+
- Spec files excluded from the built gem (`s.files` no longer includes `spec/`)
|
|
34
|
+
|
|
35
|
+
### Removed
|
|
36
|
+
|
|
37
|
+
- `Reader` — replaced by `Parser`
|
|
38
|
+
- `Reporter` — replaced by `Grouper` + `Report`
|
|
39
|
+
- `Viewable` mixin — replaced by `Formatter::Text`
|
|
40
|
+
- `GetFile` — replaced by `FilePicker`
|
|
41
|
+
- `bigdecimal` runtime dependency
|
|
42
|
+
|
|
43
|
+
## [1.0.3] — 2019-11-01
|
|
44
|
+
|
|
45
|
+
Initial public release.
|
data/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Banco
|
|
2
|
+
|
|
3
|
+
Summarise bank statement CSV files from the command line.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
gem install banco
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Ruby >= 3.3.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Navigate to the directory containing your CSV file and run:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
banco
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Banco scans for CSV files, lets you pick one (or pass `--file`), then
|
|
22
|
+
presents an interactive menu:
|
|
23
|
+
|
|
24
|
+
| Key | Action |
|
|
25
|
+
|-----|--------|
|
|
26
|
+
| `a` | All transactions |
|
|
27
|
+
| `o` | Outgoing transactions |
|
|
28
|
+
| `i` | Incoming transactions |
|
|
29
|
+
| `m` | Money-out summary (grouped) |
|
|
30
|
+
| `n` | Money-in summary (grouped) |
|
|
31
|
+
| `b` | Bottom line (net position) |
|
|
32
|
+
| `s` | Save summary as CSV (prompts for path, default is `<filename>_summary.csv`) |
|
|
33
|
+
| `t` | Save transactions as CSV (prompts for path, default is `<filename>_transactions.csv`) |
|
|
34
|
+
| `l` | Load a new file |
|
|
35
|
+
| `+` | Merge another file into the current dataset |
|
|
36
|
+
| `q` | Quit |
|
|
37
|
+
|
|
38
|
+
Use `--format json` or `--format csv` for pipe-friendly output (no menu, exits after printing).
|
|
39
|
+
|
|
40
|
+
## CLI flags
|
|
41
|
+
|
|
42
|
+
| Flag | Default | Description |
|
|
43
|
+
|------|---------|-------------|
|
|
44
|
+
| `-f`, `--file FILE` | — | CSV file to analyse |
|
|
45
|
+
| `-F`, `--format FORMAT` | `text` | Output format: `text` / `json` / `csv` |
|
|
46
|
+
| `-b`, `--bank NAME` | `legacy` | Bank CSV format (see Bank formats below) |
|
|
47
|
+
| `-p`, `--prefix-length N` | `9` | Description prefix length for grouping |
|
|
48
|
+
| `-H`, `--header` | off | Skip first row as header (auto-detected when the first row is non-numeric) |
|
|
49
|
+
| `--from DATE` | — | Filter from date (format matches bank format) |
|
|
50
|
+
| `--to DATE` | — | Filter to date (format matches bank format) |
|
|
51
|
+
| `-v`, `--version` | — | Print version and exit |
|
|
52
|
+
| `-h`, `--help` | — | Show help and exit |
|
|
53
|
+
|
|
54
|
+
### Examples
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
# Summarise all transactions in statements.csv
|
|
58
|
+
banco --file statements.csv
|
|
59
|
+
|
|
60
|
+
# September only, JSON output
|
|
61
|
+
banco --file statements.csv --from 01/09/17 --to 30/09/17 --format json
|
|
62
|
+
|
|
63
|
+
# Skip header row, group by first 6 characters
|
|
64
|
+
banco --file export.csv --header --prefix-length 6
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Bank formats
|
|
68
|
+
|
|
69
|
+
| Name | Date format | Columns | Notes |
|
|
70
|
+
|------|-------------|---------|-------|
|
|
71
|
+
| `legacy` | `%d/%m/%y` | date, description, type, credit, debit | Default — Barclays-style, separate money-in / money-out columns |
|
|
72
|
+
| `split_columns` | `%d/%m/%Y` | date, description, type, credit, debit | Like `legacy` with a 4-digit year |
|
|
73
|
+
| `signed_amount` | `%Y-%m-%d` | date, description, amount | Single signed amount column, no type column |
|
|
74
|
+
| `date_description_amount` | `%d/%m/%Y` | date, description, amount, trailing | Signed amount at second-to-last position; trailing column (balance, reference, etc.) ignored. Tolerates unquoted commas in descriptions. |
|
|
75
|
+
|
|
76
|
+
### Column ordering
|
|
77
|
+
|
|
78
|
+
`date_description_amount` uses `Date | Description | Amount | trailing column`:
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
Date Description Amount Balance/Reference
|
|
82
|
+
07/10/2026 SAINSBURYS S/MKTS LEWES ROAD -22.05 72.73
|
|
83
|
+
07/10/2026 MARLEY EI Loan/rent 450.00 1093.70
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The trailing column (Balance or Reference) is always ignored. The Amount column is read
|
|
87
|
+
from the **second-to-last** position, so unquoted commas inside a description (which
|
|
88
|
+
can appear in credit card exports) do not shift the Amount value.
|
|
89
|
+
|
|
90
|
+
`legacy` and `split_columns` use `Date | Description | Type | Credit | Debit`:
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
Date Description Type Credit Debit
|
|
94
|
+
21/09/17 Incoming Transaction credit 12.34
|
|
95
|
+
21/09/17 Outgoing Transaction Purchase 12.34
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Signed amount columns
|
|
99
|
+
|
|
100
|
+
When a format maps `credit_column` and `debit_column` to the **same** column
|
|
101
|
+
(as `signed_amount` and `date_description_amount` do), that column is read as a
|
|
102
|
+
single **signed** amount: positive values are money in (credit) and negative values
|
|
103
|
+
are money out (debit). Formats with separate columns (`legacy`, `split_columns`)
|
|
104
|
+
read each column directly.
|
|
105
|
+
|
|
106
|
+
### Custom formats
|
|
107
|
+
|
|
108
|
+
Define your own format in `~/.banco/formats.rb`:
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
Banco::BankFormat.define(:my_bank) do
|
|
112
|
+
date_column 0
|
|
113
|
+
description_column 2
|
|
114
|
+
type_column 1
|
|
115
|
+
credit_column 3
|
|
116
|
+
debit_column 4
|
|
117
|
+
date_format '%Y-%m-%d'
|
|
118
|
+
end
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Then use it with `banco --bank my_bank`.
|
|
122
|
+
|
|
123
|
+
## CSV format (legacy)
|
|
124
|
+
|
|
125
|
+
Banco's default (`--bank legacy`) expects columns in this order:
|
|
126
|
+
|
|
127
|
+
| # | Content |
|
|
128
|
+
|---|---------|
|
|
129
|
+
| 1 | Date (`dd/mm/yy`) |
|
|
130
|
+
| 2 | Description |
|
|
131
|
+
| 3 | Type |
|
|
132
|
+
| 4 | Money in |
|
|
133
|
+
| 5 | Money out |
|
|
134
|
+
|
|
135
|
+
Header rows are auto-detected and skipped. Pass `--header` explicitly to suppress the warning. Any columns beyond the fifth are ignored.
|
|
136
|
+
|
|
137
|
+
## Licence
|
|
138
|
+
|
|
139
|
+
MIT — see [LICENSE](LICENSE).
|
|
140
|
+
|
|
141
|
+
Hope your numbers are positive!
|
data/bin/banco
CHANGED
|
@@ -1,53 +1,104 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
require_relative '../lib/banco/reader'
|
|
6
|
-
require_relative '../lib/banco/reporter'
|
|
4
|
+
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
|
|
5
|
+
require 'banco'
|
|
7
6
|
|
|
8
7
|
module Banco
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
begin
|
|
9
|
+
options = CLI.parse
|
|
10
|
+
options[:file] ||= FilePicker.pick
|
|
11
|
+
|
|
12
|
+
fmt_class = Formatter::Base.for_name(options[:format])
|
|
13
|
+
bank_fmt = BankFormat[options[:bank]]
|
|
14
|
+
from_date = CLI.filter_date(options[:from], bank_fmt.date_fmt)
|
|
15
|
+
to_date = CLI.filter_date(options[:to], bank_fmt.date_fmt)
|
|
16
|
+
transactions = Parser.new(options[:file], skip_header: options[:skip_header],
|
|
17
|
+
bank_format: bank_fmt).parse
|
|
18
|
+
report = Grouper.new(transactions, prefix_length: options[:prefix_length],
|
|
19
|
+
from: from_date, to: to_date).call
|
|
20
|
+
fmt = fmt_class.new(report)
|
|
21
|
+
|
|
22
|
+
unless options[:format] == 'text'
|
|
23
|
+
puts fmt.call
|
|
24
|
+
exit
|
|
19
25
|
end
|
|
20
|
-
end
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
print
|
|
30
|
-
|
|
31
|
-
print
|
|
32
|
-
|
|
33
|
-
print
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
27
|
+
CLI.hello
|
|
28
|
+
|
|
29
|
+
loop do
|
|
30
|
+
print fmt.menu
|
|
31
|
+
answer = $stdin.gets.chomp.downcase
|
|
32
|
+
case answer
|
|
33
|
+
when 'a' then print fmt.transactions_all
|
|
34
|
+
when 'o' then print fmt.transactions_out
|
|
35
|
+
when 'i' then print fmt.transactions_in
|
|
36
|
+
when 'm' then print fmt.money_out_summary
|
|
37
|
+
when 'n' then print fmt.money_in_summary
|
|
38
|
+
when 'b' then print fmt.bottom_line
|
|
39
|
+
when 's'
|
|
40
|
+
default = "#{File.basename(options[:file], '.csv')}_summary.csv"
|
|
41
|
+
print "\nSave summary as [#{default}]\n" \
|
|
42
|
+
'(press Enter to save here, or type a full path e.g. ~/Desktop/summary.csv): '
|
|
43
|
+
input = $stdin.gets.chomp
|
|
44
|
+
path = File.expand_path(input.empty? ? default : input)
|
|
45
|
+
unless File.directory?(File.dirname(path))
|
|
46
|
+
warn "\nError: directory does not exist — #{File.dirname(path)}"
|
|
47
|
+
next
|
|
48
|
+
end
|
|
49
|
+
fmt.save_summary_to_file(path)
|
|
50
|
+
puts "\nfile saved as #{path}\n\n"
|
|
51
|
+
when 't'
|
|
52
|
+
default = "#{File.basename(options[:file], '.csv')}_transactions.csv"
|
|
53
|
+
print "\nSave transactions as [#{default}]\n" \
|
|
54
|
+
'(press Enter to save here, or type a full path e.g. ~/Desktop/transactions.csv): '
|
|
55
|
+
input = $stdin.gets.chomp
|
|
56
|
+
path = File.expand_path(input.empty? ? default : input)
|
|
57
|
+
unless File.directory?(File.dirname(path))
|
|
58
|
+
warn "\nError: directory does not exist — #{File.dirname(path)}"
|
|
59
|
+
next
|
|
60
|
+
end
|
|
61
|
+
fmt.save_transactions_to_file(path)
|
|
62
|
+
puts "\nfile saved as #{path}\n\n"
|
|
63
|
+
when 'l'
|
|
64
|
+
new_file = FilePicker.pick
|
|
65
|
+
begin
|
|
66
|
+
new_transactions = Parser.new(new_file, skip_header: options[:skip_header],
|
|
67
|
+
bank_format: bank_fmt).parse
|
|
68
|
+
rescue ArgumentError => e
|
|
69
|
+
warn "\nError: #{e.message}"
|
|
70
|
+
warn 'The current file is still loaded — try again.'
|
|
71
|
+
next
|
|
72
|
+
end
|
|
73
|
+
options[:file] = new_file
|
|
74
|
+
transactions = new_transactions
|
|
75
|
+
report = Grouper.new(transactions, prefix_length: options[:prefix_length],
|
|
76
|
+
from: from_date, to: to_date).call
|
|
77
|
+
fmt = fmt_class.new(report)
|
|
78
|
+
when '+'
|
|
79
|
+
extra_file = FilePicker.pick
|
|
80
|
+
begin
|
|
81
|
+
extra = Parser.new(extra_file, skip_header: options[:skip_header],
|
|
82
|
+
bank_format: bank_fmt).parse
|
|
83
|
+
rescue ArgumentError => e
|
|
84
|
+
warn "\nError: #{e.message}"
|
|
85
|
+
warn 'Nothing was merged — the current set is unchanged.'
|
|
86
|
+
next
|
|
87
|
+
end
|
|
88
|
+
transactions += extra
|
|
89
|
+
report = Grouper.new(transactions, prefix_length: options[:prefix_length],
|
|
90
|
+
from: from_date, to: to_date).call
|
|
91
|
+
fmt = fmt_class.new(report)
|
|
92
|
+
puts "\n#{extra.size} transactions merged — #{transactions.size} total\n\n"
|
|
93
|
+
when 'q' then break
|
|
94
|
+
else print fmt.menu
|
|
95
|
+
end
|
|
50
96
|
end
|
|
97
|
+
|
|
98
|
+
CLI.farewell
|
|
99
|
+
rescue ArgumentError => e
|
|
100
|
+
warn "\nError: #{e.message}"
|
|
101
|
+
warn 'Check the file matches the --bank format, or try a different --bank option.'
|
|
102
|
+
exit 1
|
|
51
103
|
end
|
|
52
|
-
Viewable::farewell
|
|
53
104
|
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Banco
|
|
4
|
+
class BankFormat
|
|
5
|
+
attr_reader :date_col, :description_col, :type_col, :credit_col, :debit_col, :date_fmt
|
|
6
|
+
|
|
7
|
+
def self.define(name, &)
|
|
8
|
+
fmt = new
|
|
9
|
+
fmt.instance_eval(&)
|
|
10
|
+
formats[name.to_sym] = fmt
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.[](name)
|
|
14
|
+
formats.fetch(name.to_sym) { raise ArgumentError, "Unknown bank format: #{name}" }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.names
|
|
18
|
+
formats.keys
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.formats
|
|
22
|
+
@formats ||= {}
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# DSL methods — called inside instance_eval blocks
|
|
26
|
+
def date_column(col) = @date_col = col
|
|
27
|
+
def description_column(col) = @description_col = col
|
|
28
|
+
def type_column(col) = @type_col = col
|
|
29
|
+
def credit_column(col) = @credit_col = col
|
|
30
|
+
def debit_column(col) = @debit_col = col
|
|
31
|
+
def date_format(fmt) = @date_fmt = fmt
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
BankFormat.define(:legacy) do
|
|
35
|
+
date_column 0
|
|
36
|
+
description_column 1
|
|
37
|
+
type_column 2
|
|
38
|
+
credit_column 3
|
|
39
|
+
debit_column 4
|
|
40
|
+
date_format '%d/%m/%y'
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
BankFormat.define(:split_columns) do
|
|
44
|
+
date_column 0
|
|
45
|
+
description_column 1
|
|
46
|
+
type_column 2
|
|
47
|
+
credit_column 3
|
|
48
|
+
debit_column 4
|
|
49
|
+
date_format '%d/%m/%Y'
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
BankFormat.define(:signed_amount) do
|
|
53
|
+
date_column 0
|
|
54
|
+
description_column 1
|
|
55
|
+
credit_column 2
|
|
56
|
+
debit_column 2
|
|
57
|
+
date_format '%Y-%m-%d'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
BankFormat.define(:date_description_amount) do
|
|
61
|
+
date_column 0
|
|
62
|
+
description_column 1
|
|
63
|
+
credit_column(-2)
|
|
64
|
+
debit_column(-2)
|
|
65
|
+
date_format '%d/%m/%Y'
|
|
66
|
+
end
|
|
67
|
+
end
|
data/lib/banco/cli.rb
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'optparse'
|
|
4
|
+
require 'date'
|
|
5
|
+
require 'banco/version'
|
|
6
|
+
require 'banco/bank_format'
|
|
7
|
+
require 'banco/formatter/base'
|
|
8
|
+
require 'banco/formatter/text'
|
|
9
|
+
require 'banco/formatter/json_formatter'
|
|
10
|
+
require 'banco/formatter/csv_formatter'
|
|
11
|
+
|
|
12
|
+
module Banco
|
|
13
|
+
class CLI
|
|
14
|
+
def self.parse(argv = ARGV)
|
|
15
|
+
options = { format: 'text', prefix_length: 9, skip_header: false, bank: :legacy }
|
|
16
|
+
parser = build_parser(options)
|
|
17
|
+
parser.parse!(argv)
|
|
18
|
+
options
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Parse a --from/--to value strictly against the bank's date format.
|
|
22
|
+
# Rejects values that don't fully match (e.g. a 4-digit year against a
|
|
23
|
+
# 2-digit-year format) or that are out of range, exiting with a message
|
|
24
|
+
# rather than silently misparsing or crashing with a backtrace.
|
|
25
|
+
def self.filter_date(value, fmt)
|
|
26
|
+
return nil unless value
|
|
27
|
+
|
|
28
|
+
parts = Date._strptime(value, fmt)
|
|
29
|
+
raise Date::Error if parts.nil? || parts[:leftover]
|
|
30
|
+
|
|
31
|
+
Date.strptime(value, fmt)
|
|
32
|
+
rescue Date::Error
|
|
33
|
+
warn "Invalid date #{value.inspect} — expected a date matching #{fmt}"
|
|
34
|
+
exit 1
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.hello
|
|
38
|
+
puts "\n\n"
|
|
39
|
+
puts 'Banco will summarize your bank statements.'.center(55)
|
|
40
|
+
puts 'import a .csv file for review'.center(55)
|
|
41
|
+
puts "\n\n"
|
|
42
|
+
puts 'the .csv file should have :'.center(55)
|
|
43
|
+
puts 'with columns ordered (left to right)'.center(55)
|
|
44
|
+
puts 'date - description - type - money in - money out'.center(55)
|
|
45
|
+
puts 'all additional columns will be ignored'.center(55)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.farewell
|
|
49
|
+
puts "\n\n"
|
|
50
|
+
puts '*'.center(55, '*')
|
|
51
|
+
puts ' Banco '.center(55, '*')
|
|
52
|
+
puts ' hope your numbers were positive '.center(55, '*')
|
|
53
|
+
puts ' ♥️ code@s33d.co '.center(56, '*')
|
|
54
|
+
puts '*'.center(55, '*')
|
|
55
|
+
puts "\n\n\n"
|
|
56
|
+
exit
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class << self
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def build_parser(options)
|
|
63
|
+
fmt_names = Formatter::Base.names
|
|
64
|
+
bank_names = BankFormat.names.map(&:to_s)
|
|
65
|
+
OptionParser.new do |opts|
|
|
66
|
+
opts.banner = 'Usage: banco [options]'
|
|
67
|
+
opts.on('-f', '--file FILE', 'CSV file to analyse') { |v| options[:file] = v }
|
|
68
|
+
opts.on('-p', '--prefix-length N', Integer, 'Grouping prefix length (default 9)') do |v|
|
|
69
|
+
options[:prefix_length] = v
|
|
70
|
+
end
|
|
71
|
+
opts.on('-H', '--header', 'Skip first row as header') { options[:skip_header] = true }
|
|
72
|
+
opts.on('--from DATE', 'Filter from date (format matches --bank)') { |v| options[:from] = v }
|
|
73
|
+
opts.on('--to DATE', 'Filter to date (format matches --bank)') { |v| options[:to] = v }
|
|
74
|
+
opts.on('-F', '--format FORMAT', fmt_names, "Output format (#{fmt_names.join('/')})") do |v|
|
|
75
|
+
options[:format] = v
|
|
76
|
+
end
|
|
77
|
+
opts.on('-b', '--bank NAME', bank_names, "Bank CSV format (#{bank_names.join('/')})") do |v|
|
|
78
|
+
options[:bank] = v.to_sym
|
|
79
|
+
end
|
|
80
|
+
opts.on('-v', '--version', 'Print version and exit') do
|
|
81
|
+
puts Banco::VERSION
|
|
82
|
+
exit
|
|
83
|
+
end
|
|
84
|
+
opts.on('-h', '--help', 'Show this help') do
|
|
85
|
+
puts opts
|
|
86
|
+
exit
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Banco
|
|
4
|
+
class FilePicker
|
|
5
|
+
def self.pick(stdin: $stdin, stdout: $stdout)
|
|
6
|
+
files = Dir.glob('*.csv')
|
|
7
|
+
case files.size
|
|
8
|
+
when 0 then prompt_for_path(stdin:, stdout:)
|
|
9
|
+
when 1 then confirm_single(files.first, stdin:, stdout:)
|
|
10
|
+
else numbered_list(files, stdin:, stdout:)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def prompt_for_path(stdin:, stdout:)
|
|
18
|
+
stdout.print 'No CSV files found. Enter a full path (e.g. ~/Downloads/statement.csv), or q to quit: '
|
|
19
|
+
input = read_input(stdin)
|
|
20
|
+
exit if input.downcase == 'q'
|
|
21
|
+
File.expand_path(input)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def confirm_single(file, stdin:, stdout:)
|
|
25
|
+
stdout.print "Found #{file} — press Enter to use it, or type a full path (e.g. ~/Downloads/statement.csv): "
|
|
26
|
+
input = read_input(stdin)
|
|
27
|
+
input.empty? ? file : File.expand_path(input)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def numbered_list(files, stdin:, stdout:)
|
|
31
|
+
stdout.puts "\nCSV files found:"
|
|
32
|
+
files.each_with_index { |f, i| stdout.puts " #{i + 1}. #{f}" }
|
|
33
|
+
stdout.print "\nEnter a number, a full path (e.g. ~/Downloads/statement.csv), or q to quit: "
|
|
34
|
+
input = read_input(stdin)
|
|
35
|
+
exit if input.downcase == 'q'
|
|
36
|
+
index = input.to_i
|
|
37
|
+
index.between?(1, files.size) ? files[index - 1] : File.expand_path(input)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def read_input(stdin)
|
|
41
|
+
stdin.gets.chomp
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Banco
|
|
4
|
+
module Formatter
|
|
5
|
+
class Base
|
|
6
|
+
def self.inherited(subclass)
|
|
7
|
+
super
|
|
8
|
+
registry << subclass
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.registry
|
|
12
|
+
@registry ||= []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.for_name(name)
|
|
16
|
+
registry.find { |f| f.format_name == name.to_s } ||
|
|
17
|
+
raise(ArgumentError, "Unknown format: #{name}. Available: #{names.join(', ')}")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.names
|
|
21
|
+
registry.map(&:format_name)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.format_name
|
|
25
|
+
name.split('::').last.downcase
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(report)
|
|
29
|
+
@report = report
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def call
|
|
33
|
+
raise NotImplementedError, "#{self.class} must implement #call"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'csv'
|
|
4
|
+
require_relative 'base'
|
|
5
|
+
|
|
6
|
+
module Banco
|
|
7
|
+
module Formatter
|
|
8
|
+
class CsvFormatter < Base
|
|
9
|
+
def self.format_name = 'csv'
|
|
10
|
+
|
|
11
|
+
def call
|
|
12
|
+
CSV.generate do |csv|
|
|
13
|
+
csv << %w[date description type credit_pence debit_pence]
|
|
14
|
+
@report.all_transactions.each do |t|
|
|
15
|
+
csv << [t.date, t.description.strip, t.type.strip, t.credit_pence, t.debit_pence]
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require_relative 'base'
|
|
5
|
+
|
|
6
|
+
module Banco
|
|
7
|
+
module Formatter
|
|
8
|
+
class JSONFormatter < Base
|
|
9
|
+
def self.format_name = 'json'
|
|
10
|
+
|
|
11
|
+
def call
|
|
12
|
+
{
|
|
13
|
+
date_range: @report.date_range,
|
|
14
|
+
transactions: @report.all_transactions.map { |txn| transaction_hash(txn) },
|
|
15
|
+
outgoing_total_pence: @report.outgoing_total,
|
|
16
|
+
incoming_total_pence: @report.incoming_total,
|
|
17
|
+
surplus_pence: @report.surplus_pence
|
|
18
|
+
}.to_json
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def transaction_hash(txn)
|
|
24
|
+
{ date: txn.date, description: txn.description.strip, type: txn.type.strip,
|
|
25
|
+
credit_pence: txn.credit_pence, debit_pence: txn.debit_pence }
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|