bdgtr-cli 0.2 → 0.3
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/README.md +1 -1
- data/bin/bdgtr +26 -5
- data/lib/bdgtr/cli/config.rb +188 -0
- data/lib/bdgtr/cli/config.sample.yml +15 -0
- data/lib/bdgtr/cli/help.rb +168 -0
- data/lib/bdgtr/cli/ledger_sync.rb +102 -0
- data/lib/bdgtr/cli/prompt.rb +47 -0
- data/lib/bdgtr/cli/psd2.rb +530 -0
- data/lib/bdgtr/cli/rows.rb +38 -0
- data/lib/bdgtr/cli/runtime.rb +88 -0
- data/lib/bdgtr/cli/table.rb +458 -0
- data/lib/bdgtr/cli.rb +126 -0
- data/lib/bdgtr/core/binder.rb +79 -0
- data/lib/bdgtr/core/exchange.rb +66 -0
- data/lib/bdgtr/core/models/account.rb +177 -0
- data/lib/bdgtr/core/models/account_requisition.rb +12 -0
- data/lib/bdgtr/core/models/account_sync.rb +12 -0
- data/lib/bdgtr/core/models/import.rb +12 -0
- data/lib/bdgtr/core/models/ledger.rb +10 -0
- data/lib/bdgtr/core/models/requisition.rb +27 -0
- data/lib/bdgtr/core/models/transaction.rb +98 -0
- data/lib/bdgtr/core/models/transaction_category.rb +12 -0
- data/lib/bdgtr/core/record.rb +11 -0
- data/lib/bdgtr/core/schema.rb +290 -0
- data/lib/bdgtr/core/session.rb +76 -0
- data/lib/bdgtr/core/syncable.rb +272 -0
- data/lib/bdgtr/core/transfer.rb +90 -0
- data/lib/bdgtr/core.rb +18 -0
- data/lib/bdgtr/portfolio/Rakefile +20 -0
- data/lib/bdgtr/portfolio/cli/LICENSE +21 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/app.rb +77 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/import.rb +30 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/spreadsheet.rb +25 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli.rb +52 -0
- data/lib/bdgtr/portfolio/exe/bdgtr +7 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/accounts_csv.rb +67 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/app.rb +104 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/auth.rb +52 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/balances.rb +157 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/cache.rb +46 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_menu.rb +90 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_selection.rb +68 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/config.rb +138 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/decryptor.rb +70 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/drive_client.rb +182 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/errors.rb +10 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/file_reader.rb +80 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_import.rb +609 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_xls.rb +235 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import/fineco.rb +243 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import.rb +85 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/parser.rb +254 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_source.rb +30 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_store.rb +124 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/preview_window.rb +98 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/proto/client_pb.rb +29 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/row_preview.rb +174 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sheets_client.rb +272 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/snapshot.rb +27 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync.rb +247 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync_preview.rb +63 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_identity.rb +49 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_sync.rb +948 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_types.rb +142 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/version.rb +5 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api.rb +42 -0
- data/lib/bdgtr/portfolio/proto/client.proto +114 -0
- data/lib/bdgtr/psd2/account.rb +24 -0
- data/lib/bdgtr/psd2/account_snapshot.rb +96 -0
- data/lib/bdgtr/psd2/callback.rb +49 -0
- data/lib/bdgtr/psd2/connect.rb +75 -0
- data/lib/bdgtr/psd2/enable_banking.rb +250 -0
- data/lib/bdgtr/psd2/go_cardless.rb +133 -0
- data/lib/bdgtr/psd2/institution.rb +17 -0
- data/lib/bdgtr/psd2/platform.rb +37 -0
- data/lib/bdgtr/psd2/requisition.rb +21 -0
- data/lib/bdgtr/psd2/transaction.rb +29 -0
- data/lib/bdgtr/psd2.rb +25 -0
- data/lib/bdgtr/sync/importer.rb +125 -0
- data/lib/bdgtr/sync/library.rb +18 -0
- data/lib/bdgtr/sync/portfolio.rb +14 -0
- data/lib/bdgtr/sync/projection.rb +38 -0
- data/lib/bdgtr/sync/spreadsheet.rb +23 -0
- data/lib/bdgtr/sync.rb +25 -0
- data/lib/bdgtr/version.rb +1 -1
- metadata +353 -30
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "base64"
|
|
5
|
+
require "openssl"
|
|
6
|
+
require "net/http"
|
|
7
|
+
require "uri"
|
|
8
|
+
require "time"
|
|
9
|
+
|
|
10
|
+
module PortfolioPerformanceApi
|
|
11
|
+
class DriveClient
|
|
12
|
+
TOKEN_URI = URI("https://oauth2.googleapis.com/token")
|
|
13
|
+
SCOPE = "https://www.googleapis.com/auth/drive.readonly"
|
|
14
|
+
SYNC_SCOPE = "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/spreadsheets"
|
|
15
|
+
FILE_URI = "https://www.googleapis.com/drive/v3/files/%s"
|
|
16
|
+
UPLOAD_URI = "https://www.googleapis.com/upload/drive/v3/files/%s"
|
|
17
|
+
SHORTCUT_MIME = "application/vnd.google-apps.shortcut"
|
|
18
|
+
META_FIELDS = "id,name,modifiedTime,md5Checksum,size,mimeType,shortcutDetails"
|
|
19
|
+
|
|
20
|
+
def initialize(credentials_json: Config.service_account_json, file_id: Config.drive_file_id, scope: SCOPE)
|
|
21
|
+
raise NotConfigured, "Google Drive is not configured" if credentials_json.to_s.empty? || file_id.to_s.empty?
|
|
22
|
+
|
|
23
|
+
@credentials = JSON.parse(credentials_json)
|
|
24
|
+
@file_id = file_id
|
|
25
|
+
@scope = scope
|
|
26
|
+
@token = nil
|
|
27
|
+
@token_expires_at = Time.at(0)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def download
|
|
31
|
+
meta = resolve_meta(@file_id)
|
|
32
|
+
bytes = get_bytes(file_uri(meta.fetch("id"), "alt" => "media"))
|
|
33
|
+
{
|
|
34
|
+
bytes: bytes,
|
|
35
|
+
meta: {
|
|
36
|
+
source: "google_drive",
|
|
37
|
+
id: meta["id"],
|
|
38
|
+
name: meta["name"],
|
|
39
|
+
modified_at: meta["modifiedTime"],
|
|
40
|
+
md5: meta["md5Checksum"],
|
|
41
|
+
size: meta["size"]&.to_i
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def upload(bytes, mime_type: "application/octet-stream")
|
|
47
|
+
uri = URI(format(UPLOAD_URI, @file_id))
|
|
48
|
+
uri.query = URI.encode_www_form("uploadType" => "media", "supportsAllDrives" => "true")
|
|
49
|
+
request(uri, method: :patch, body: bytes, content_type: mime_type)
|
|
50
|
+
true
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def api_request(uri, method: :get, body: nil, content_type: "application/json")
|
|
54
|
+
request(uri, method: method, body: body, content_type: content_type)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def resolve_meta(file_id)
|
|
60
|
+
meta = get_json(file_uri(file_id, fields: META_FIELDS))
|
|
61
|
+
return meta unless meta["mimeType"] == SHORTCUT_MIME
|
|
62
|
+
|
|
63
|
+
target = meta.dig("shortcutDetails", "targetId").to_s
|
|
64
|
+
raise DriveError, "Drive shortcut #{file_id} has no target" if target.empty?
|
|
65
|
+
|
|
66
|
+
target_meta = get_json(file_uri(target, fields: META_FIELDS))
|
|
67
|
+
if target_meta["mimeType"] == SHORTCUT_MIME
|
|
68
|
+
raise DriveError, "nested Drive shortcuts are not supported"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
target_meta
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def file_uri(file_id, query = {})
|
|
75
|
+
uri = URI(format(FILE_URI, file_id))
|
|
76
|
+
params = { "supportsAllDrives" => "true" }.merge(query)
|
|
77
|
+
uri.query = URI.encode_www_form(params)
|
|
78
|
+
uri
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def get_json(uri)
|
|
82
|
+
response = request(uri)
|
|
83
|
+
JSON.parse(response.body)
|
|
84
|
+
rescue JSON::ParserError
|
|
85
|
+
raise DriveError, "invalid JSON from Google Drive"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def get_bytes(uri)
|
|
89
|
+
request(uri).body.b
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def request(uri, method: :get, body: nil, content_type: nil)
|
|
93
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
94
|
+
http.use_ssl = true
|
|
95
|
+
http.open_timeout = 10
|
|
96
|
+
http.read_timeout = 60
|
|
97
|
+
|
|
98
|
+
req = http_class(method).new(uri)
|
|
99
|
+
req["Authorization"] = "Bearer #{access_token}"
|
|
100
|
+
req["Content-Type"] = content_type if content_type
|
|
101
|
+
req.body = body if body
|
|
102
|
+
response = http.request(req)
|
|
103
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
104
|
+
raise DriveError, format_drive_error(response)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
response
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def http_class(method)
|
|
111
|
+
case method
|
|
112
|
+
when :get then Net::HTTP::Get
|
|
113
|
+
when :post then Net::HTTP::Post
|
|
114
|
+
when :put then Net::HTTP::Put
|
|
115
|
+
when :patch then Net::HTTP::Patch
|
|
116
|
+
else
|
|
117
|
+
raise ArgumentError, "unsupported HTTP method #{method}"
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def format_drive_error(response)
|
|
122
|
+
if response.code == "404"
|
|
123
|
+
email = @credentials["client_email"]
|
|
124
|
+
"Google Drive file not found for #{email}: #{@file_id}. " \
|
|
125
|
+
"Share the file (Viewer) with that service account. " \
|
|
126
|
+
"Drive returns 404 when the account has no access, even if the link opens in your browser."
|
|
127
|
+
else
|
|
128
|
+
"Google Drive download failed (#{response.code}): #{response.body.to_s.byteslice(0, 300)}"
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def access_token
|
|
133
|
+
return @token if @token && Time.now < @token_expires_at
|
|
134
|
+
|
|
135
|
+
assertion = jwt
|
|
136
|
+
response = Net::HTTP.post_form(
|
|
137
|
+
TOKEN_URI,
|
|
138
|
+
"grant_type" => "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
139
|
+
"assertion" => assertion
|
|
140
|
+
)
|
|
141
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
142
|
+
raise DriveError, "Google token exchange failed (#{response.code}): #{response.body.to_s.byteslice(0, 300)}"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
payload = JSON.parse(response.body)
|
|
146
|
+
@token = payload.fetch("access_token")
|
|
147
|
+
ttl = Integer(payload["expires_in"] || 3600)
|
|
148
|
+
@token_expires_at = Time.now + ttl - 60
|
|
149
|
+
@token
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def jwt
|
|
153
|
+
now = Time.now.to_i
|
|
154
|
+
header = b64({ alg: "RS256", typ: "JWT" }.to_json)
|
|
155
|
+
payload = b64({
|
|
156
|
+
iss: @credentials.fetch("client_email"),
|
|
157
|
+
scope: @scope,
|
|
158
|
+
aud: TOKEN_URI.to_s,
|
|
159
|
+
iat: now,
|
|
160
|
+
exp: now + 3600
|
|
161
|
+
}.to_json)
|
|
162
|
+
input = "#{header}.#{payload}"
|
|
163
|
+
key = self.class.signing_key(@credentials.fetch("private_key"))
|
|
164
|
+
signature = b64(key.sign(OpenSSL::Digest::SHA256.new, input))
|
|
165
|
+
"#{input}.#{signature}"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def self.signing_key(pem)
|
|
169
|
+
OpenSSL::PKey.read(normalize_pem(pem))
|
|
170
|
+
rescue OpenSSL::PKey::PKeyError => error
|
|
171
|
+
raise DriveError, "invalid Google service account private key (#{error.message})"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def self.normalize_pem(pem)
|
|
175
|
+
pem.to_s.gsub("\r\n", "\n").gsub('\\n', "\n")
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def b64(data)
|
|
179
|
+
Base64.urlsafe_encode64(data, padding: false)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PortfolioPerformanceApi
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
class Unauthorized < Error; end
|
|
6
|
+
class IncorrectPassword < Error; end
|
|
7
|
+
class NotAPortfolioFile < Error; end
|
|
8
|
+
class NotConfigured < Error; end
|
|
9
|
+
class DriveError < Error; end
|
|
10
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "stringio"
|
|
4
|
+
require "zip"
|
|
5
|
+
|
|
6
|
+
module PortfolioPerformanceApi
|
|
7
|
+
class FileReader
|
|
8
|
+
PROTO_MAGIC = "PPPBV1"
|
|
9
|
+
XML_ENTRY = "data.xml"
|
|
10
|
+
PROTO_ENTRY = "data.portfolio"
|
|
11
|
+
|
|
12
|
+
Result = Struct.new(:format, :bytes, :version, keyword_init: true)
|
|
13
|
+
|
|
14
|
+
def self.read(bytes, password: nil)
|
|
15
|
+
bytes = bytes.b
|
|
16
|
+
if Decryptor.encrypted?(bytes)
|
|
17
|
+
payload = Decryptor.decrypt(bytes, password)
|
|
18
|
+
inner = unzip(payload.zip_bytes, prefer: payload.content_type == 2 ? :protobuf : :xml)
|
|
19
|
+
Result.new(format: inner.fetch(:format), bytes: inner.fetch(:bytes), version: payload.version)
|
|
20
|
+
elsif zip?(bytes)
|
|
21
|
+
inner = unzip(bytes)
|
|
22
|
+
Result.new(format: inner.fetch(:format), bytes: inner.fetch(:bytes), version: nil)
|
|
23
|
+
elsif xml?(bytes)
|
|
24
|
+
Result.new(format: :xml, bytes: bytes, version: nil)
|
|
25
|
+
else
|
|
26
|
+
raise NotAPortfolioFile, "unrecognized portfolio file format"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.zip?(bytes)
|
|
31
|
+
bytes.start_with?("PK\x03\x04".b)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.xml?(bytes)
|
|
35
|
+
sample = bytes.byteslice(0, 64).to_s.force_encoding(Encoding::UTF_8)
|
|
36
|
+
sample.sub!("\uFEFF", "")
|
|
37
|
+
sample.lstrip.start_with?("<")
|
|
38
|
+
rescue Encoding::CompatibilityError
|
|
39
|
+
false
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.unzip(zip_bytes, prefer: nil)
|
|
43
|
+
entries = {}
|
|
44
|
+
Zip::File.open_buffer(StringIO.new(zip_bytes)) do |zip|
|
|
45
|
+
zip.each do |entry|
|
|
46
|
+
next if entry.directory?
|
|
47
|
+
|
|
48
|
+
entries[File.basename(entry.name)] = entry.get_input_stream.read
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
if prefer == :protobuf || entries.key?(PROTO_ENTRY)
|
|
53
|
+
data = entries[PROTO_ENTRY] || first_bytes(entries)
|
|
54
|
+
return { format: :protobuf, bytes: strip_proto_magic(data) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
if prefer == :xml || entries.key?(XML_ENTRY)
|
|
58
|
+
return { format: :xml, bytes: entries[XML_ENTRY] || first_bytes(entries) }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
data = first_bytes(entries)
|
|
62
|
+
if data.start_with?(PROTO_MAGIC) || (!xml?(data) && !data.lstrip.start_with?("<"))
|
|
63
|
+
{ format: :protobuf, bytes: strip_proto_magic(data) }
|
|
64
|
+
else
|
|
65
|
+
{ format: :xml, bytes: data }
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def self.strip_proto_magic(data)
|
|
70
|
+
data.start_with?(PROTO_MAGIC) ? data.byteslice(PROTO_MAGIC.bytesize..) : data
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.first_bytes(entries)
|
|
74
|
+
raise NotAPortfolioFile, "empty zip container" if entries.empty?
|
|
75
|
+
|
|
76
|
+
entries.fetch("data") { entries.values.first }
|
|
77
|
+
end
|
|
78
|
+
private_class_method :first_bytes
|
|
79
|
+
end
|
|
80
|
+
end
|