bdgtr-cli 0.1.0 → 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 +3 -3
- data/bin/bdgtr +26 -10
- data/data/bdgtr.sqlite +0 -0
- data/lib/bdgtr/cli/config.rb +188 -0
- data/lib/bdgtr/cli/config.sample.yml +15 -0
- data/lib/bdgtr/cli/gem_home.rb +0 -25
- 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,174 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "tty-cursor"
|
|
4
|
+
require "tty-reader"
|
|
5
|
+
require "tty-screen"
|
|
6
|
+
|
|
7
|
+
require_relative "preview_window"
|
|
8
|
+
|
|
9
|
+
module PortfolioPerformanceApi
|
|
10
|
+
class RowPreview
|
|
11
|
+
Section = Struct.new(:title, :lines, :counts, :page_size, :scroll, keyword_init: true)
|
|
12
|
+
|
|
13
|
+
def initialize(heading, sections, page_size:, prompt:, choices:, help: nil, acknowledge: false)
|
|
14
|
+
@heading = heading
|
|
15
|
+
@sections = Array(sections).map { |section| coerce_section(section) }
|
|
16
|
+
raise ArgumentError, "preview needs at least one section" if @sections.empty?
|
|
17
|
+
|
|
18
|
+
requested = [Integer(page_size), 1].max
|
|
19
|
+
page_sizes = fit_page_sizes(requested)
|
|
20
|
+
@window = PreviewWindow.new(
|
|
21
|
+
@sections.map(&:lines),
|
|
22
|
+
page_size: requested,
|
|
23
|
+
page_sizes: page_sizes,
|
|
24
|
+
locked: @sections.map { |section| section.scroll == false }
|
|
25
|
+
)
|
|
26
|
+
@prompt = prompt
|
|
27
|
+
@choices = Array(choices).map { |choice| choice.to_s.upcase }
|
|
28
|
+
@acknowledge = acknowledge
|
|
29
|
+
@help = help
|
|
30
|
+
@reader = TTY::Reader.new(interrupt: :exit)
|
|
31
|
+
@cursor = TTY::Cursor
|
|
32
|
+
@drawn_lines = 0
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def run
|
|
36
|
+
return static_choice unless $stdin.tty?
|
|
37
|
+
|
|
38
|
+
$stdout.print @cursor.hide
|
|
39
|
+
$stdout.sync = true
|
|
40
|
+
choice = nil
|
|
41
|
+
@reader.on(:keyup) { @window.up }
|
|
42
|
+
@reader.on(:keydown) { @window.down }
|
|
43
|
+
@reader.on(:keyescape) { choice = "N" }
|
|
44
|
+
@reader.on(:keypress) do |event|
|
|
45
|
+
choice = key_choice(event)
|
|
46
|
+
@window.page_up if %w[u U].include?(event.value)
|
|
47
|
+
@window.page_down if %w[v V].include?(event.value)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
begin
|
|
51
|
+
until choice
|
|
52
|
+
draw
|
|
53
|
+
@reader.read_keypress
|
|
54
|
+
end
|
|
55
|
+
ensure
|
|
56
|
+
clear_frame
|
|
57
|
+
$stdout.print @cursor.show
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
choice
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def coerce_section(section)
|
|
66
|
+
return section if section.is_a?(Section)
|
|
67
|
+
|
|
68
|
+
Section.new(
|
|
69
|
+
title: section.fetch(:title),
|
|
70
|
+
lines: Array(section[:lines]),
|
|
71
|
+
counts: section[:counts],
|
|
72
|
+
page_size: section[:page_size],
|
|
73
|
+
scroll: section.fetch(:scroll, true)
|
|
74
|
+
)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def fit_page_sizes(requested)
|
|
78
|
+
sizes = @sections.map { |section| section.page_size ? [Integer(section.page_size), 1].max : requested }
|
|
79
|
+
return sizes unless $stdin.tty?
|
|
80
|
+
|
|
81
|
+
usable = [TTY::Screen.height - 5, 4].max
|
|
82
|
+
locked = @sections.each_with_index.sum do |section, index|
|
|
83
|
+
next 0 unless section.scroll == false
|
|
84
|
+
|
|
85
|
+
[sizes[index], [section.lines.size, 1].max].min
|
|
86
|
+
end
|
|
87
|
+
remaining = [usable - locked, 1].max
|
|
88
|
+
scrollable = [@sections.count { |section| section.scroll != false }, 1].max
|
|
89
|
+
per = [remaining / scrollable, 1].max
|
|
90
|
+
@sections.each_with_index.map do |section, index|
|
|
91
|
+
next sizes[index] if section.scroll == false
|
|
92
|
+
|
|
93
|
+
[sizes[index], per].min
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def static_choice
|
|
98
|
+
render_lines.each { |line| $stdout.puts line }
|
|
99
|
+
return "N" if @acknowledge
|
|
100
|
+
|
|
101
|
+
loop do
|
|
102
|
+
$stderr.print "#{@prompt} "
|
|
103
|
+
answer = $stdin.gets.to_s.strip.upcase
|
|
104
|
+
return answer if @choices.include?(answer)
|
|
105
|
+
return "N" if answer.empty? || answer == "\e" || answer == "Q"
|
|
106
|
+
|
|
107
|
+
warn "type #{@choices.join(", ")} or press Esc/Q to skip"
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def key_choice(event)
|
|
112
|
+
return "N" if skip_key?(event)
|
|
113
|
+
return "N" if @acknowledge && enter_key?(event)
|
|
114
|
+
|
|
115
|
+
value = event.value.to_s.upcase
|
|
116
|
+
return value if @choices.include?(value)
|
|
117
|
+
|
|
118
|
+
nil
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def enter_key?(event)
|
|
122
|
+
return true if event.key&.name == :return || event.key&.name == :enter
|
|
123
|
+
|
|
124
|
+
["\r", "\n"].include?(event.value)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def skip_key?(event)
|
|
128
|
+
return true if event.key&.name == :escape || event.value == "\e"
|
|
129
|
+
|
|
130
|
+
event.value.to_s.upcase == "Q" && !@choices.include?("Q")
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def draw
|
|
134
|
+
lines = render_lines
|
|
135
|
+
clear_frame
|
|
136
|
+
lines.each { |line| $stdout.puts line }
|
|
137
|
+
@drawn_lines = lines.size
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def clear_frame
|
|
141
|
+
return if @drawn_lines.zero?
|
|
142
|
+
|
|
143
|
+
$stdout.print @cursor.up(@drawn_lines)
|
|
144
|
+
@drawn_lines.times do
|
|
145
|
+
$stdout.print @cursor.clear_line
|
|
146
|
+
$stdout.print @cursor.next_line
|
|
147
|
+
end
|
|
148
|
+
$stdout.print @cursor.up(@drawn_lines)
|
|
149
|
+
@drawn_lines = 0
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def render_lines
|
|
153
|
+
section_lines = @sections.each_with_index.flat_map do |section, index|
|
|
154
|
+
render_section(section, @window.visible(index), @window.range(index))
|
|
155
|
+
end
|
|
156
|
+
[
|
|
157
|
+
@heading,
|
|
158
|
+
section_lines,
|
|
159
|
+
@prompt,
|
|
160
|
+
@help || "↑/↓ row u/v page ±#{@window.page_size} esc/q skip"
|
|
161
|
+
].flatten
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def render_section(section, rows, range)
|
|
165
|
+
from, to, total = range
|
|
166
|
+
parts = [section.title]
|
|
167
|
+
if section.counts
|
|
168
|
+
parts << "+#{section.counts.fetch(:create, 0)}/~#{section.counts.fetch(:update, 0)}"
|
|
169
|
+
end
|
|
170
|
+
parts << "#{from}-#{to}/#{total}" if total.positive?
|
|
171
|
+
[parts.join(" ")] + (rows.empty? ? [" (none)"] : rows.map { |row| " #{row}" })
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module PortfolioPerformanceApi
|
|
7
|
+
class SheetsClient
|
|
8
|
+
SPREADSHEET_URI = "https://sheets.googleapis.com/v4/spreadsheets/%s"
|
|
9
|
+
EDIT_URI = "https://docs.google.com/spreadsheets/d/%s/edit"
|
|
10
|
+
DEPOSIT_PREFIX = "deposit - "
|
|
11
|
+
SECURITIES_PREFIX = "securities - "
|
|
12
|
+
TITLE_MAX = 100
|
|
13
|
+
|
|
14
|
+
attr_reader :spreadsheet_id
|
|
15
|
+
|
|
16
|
+
def initialize(session:, spreadsheet_id:)
|
|
17
|
+
raise NotConfigured, "SYNC_GOOGLE_DRIVE_FILE_ID is not configured" if spreadsheet_id.to_s.empty?
|
|
18
|
+
|
|
19
|
+
@session = session
|
|
20
|
+
@spreadsheet_id = spreadsheet_id
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def sheet_titles
|
|
24
|
+
sheet_properties.map { |props| props["title"] }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def sheet_gids
|
|
28
|
+
sheet_properties.to_h { |props| [props["title"], props["sheetId"]] }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def ensure_sheet(title, known_titles: nil)
|
|
32
|
+
titles = known_titles || sheet_titles
|
|
33
|
+
return title if titles.include?(title)
|
|
34
|
+
|
|
35
|
+
batch_update([{ addSheet: { properties: { title: title } } }])
|
|
36
|
+
title
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def sheet_properties
|
|
40
|
+
uri = URI(format(SPREADSHEET_URI, @spreadsheet_id))
|
|
41
|
+
uri.query = URI.encode_www_form("fields" => "sheets.properties(sheetId,title,gridProperties(rowCount,columnCount))")
|
|
42
|
+
payload = JSON.parse(@session.api_request(uri).body)
|
|
43
|
+
Array(payload["sheets"]).map { |sheet| sheet["properties"] || {} }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def reset_sheets(titles)
|
|
47
|
+
wanted = Array(titles)
|
|
48
|
+
raise ArgumentError, "reset_sheets needs at least one title" if wanted.empty?
|
|
49
|
+
|
|
50
|
+
current = sheet_properties
|
|
51
|
+
existing = current.map { |props| props["title"] }
|
|
52
|
+
requests = []
|
|
53
|
+
wanted.each do |title|
|
|
54
|
+
next if existing.include?(title)
|
|
55
|
+
|
|
56
|
+
requests << { addSheet: { properties: { title: title } } }
|
|
57
|
+
end
|
|
58
|
+
current.each do |props|
|
|
59
|
+
next if wanted.include?(props["title"])
|
|
60
|
+
|
|
61
|
+
requests << { deleteSheet: { sheetId: props["sheetId"] } }
|
|
62
|
+
end
|
|
63
|
+
batch_update(requests) unless requests.empty?
|
|
64
|
+
wanted.each { |title| clear_sheet(title) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def read_rows(title)
|
|
68
|
+
uri = URI("#{format(SPREADSHEET_URI, @spreadsheet_id)}/values/#{encode_range(title, "A:Z")}")
|
|
69
|
+
uri.query = URI.encode_www_form(
|
|
70
|
+
"valueRenderOption" => "UNFORMATTED_VALUE",
|
|
71
|
+
"dateTimeRenderOption" => "FORMATTED_STRING",
|
|
72
|
+
"majorDimension" => "ROWS"
|
|
73
|
+
)
|
|
74
|
+
payload = JSON.parse(@session.api_request(uri).body)
|
|
75
|
+
Array(payload["values"])
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def write_chunks(title, grid, start_row:, chunk_size: 100)
|
|
79
|
+
chunks = TransactionSync.sheet_chunks(grid, start_row: start_row, chunk_size: chunk_size)
|
|
80
|
+
return if chunks.empty?
|
|
81
|
+
|
|
82
|
+
columns = [Array(grid).map { |row| Array(row).size }.max.to_i, 1].max
|
|
83
|
+
last_col = self.class.column_letter(columns)
|
|
84
|
+
ensure_grid(title, rows: chunks.last[:end_row], columns: columns)
|
|
85
|
+
chunks.each do |chunk|
|
|
86
|
+
write_range(title, "A#{chunk[:start_row]}:#{last_col}#{chunk[:end_row]}", chunk[:values])
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def clear_sheet(title)
|
|
91
|
+
uri = URI("#{format(SPREADSHEET_URI, @spreadsheet_id)}/values/#{encode_range(title, "A:Z")}:clear")
|
|
92
|
+
with_retry do
|
|
93
|
+
@session.api_request(uri, method: :post, body: "{}")
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def clear_data_rows(title, skip_rows: 0)
|
|
98
|
+
start_row = Integer(skip_rows) + 2
|
|
99
|
+
uri = URI("#{format(SPREADSHEET_URI, @spreadsheet_id)}/values/#{encode_range(title, "A#{start_row}:Z")}:clear")
|
|
100
|
+
with_retry do
|
|
101
|
+
@session.api_request(uri, method: :post, body: "{}")
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.spreadsheet_url(spreadsheet_id, gid: nil)
|
|
106
|
+
url = format(EDIT_URI, spreadsheet_id)
|
|
107
|
+
return url if gid.nil? || gid.to_s.empty?
|
|
108
|
+
|
|
109
|
+
"#{url}?gid=#{gid}#gid=#{gid}"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def self.terminal_link(text, url)
|
|
113
|
+
label = text.to_s
|
|
114
|
+
return label if url.to_s.empty?
|
|
115
|
+
|
|
116
|
+
"\e]8;;#{url}\e\\#{label}\e]8;;\e\\"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def self.quote_sheet(title)
|
|
120
|
+
"'#{title.to_s.gsub("'", "''")}'"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def self.prefix_for(kind)
|
|
124
|
+
kind.to_s == "securities" ? SECURITIES_PREFIX : DEPOSIT_PREFIX
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def self.sheet_kind(title)
|
|
128
|
+
text = title.to_s
|
|
129
|
+
return :securities if text.downcase.start_with?(SECURITIES_PREFIX)
|
|
130
|
+
return :deposit if text.downcase.start_with?(DEPOSIT_PREFIX)
|
|
131
|
+
|
|
132
|
+
nil
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def self.account_name(title)
|
|
136
|
+
text = title.to_s
|
|
137
|
+
prefix = prefix_for(sheet_kind(text)) if sheet_kind(text)
|
|
138
|
+
return text unless prefix && text.downcase.start_with?(prefix.downcase)
|
|
139
|
+
|
|
140
|
+
text[prefix.size..]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def self.sheet_title(kind, name)
|
|
144
|
+
prefix = prefix_for(kind)
|
|
145
|
+
"#{prefix}#{sanitize_title(name, max: TITLE_MAX - prefix.size)}"
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def self.sanitize_title(name, max: TITLE_MAX)
|
|
149
|
+
title = name.to_s.gsub(%r{[:\\/?*\[\]]}, "-").strip
|
|
150
|
+
title = "Account" if title.empty?
|
|
151
|
+
title[0, max]
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def self.unique_titles(vehicles)
|
|
155
|
+
used = {}
|
|
156
|
+
Array(vehicles).map do |vehicle|
|
|
157
|
+
kind, name = kind_and_name(vehicle)
|
|
158
|
+
prefix = prefix_for(kind)
|
|
159
|
+
title = sheet_title(kind, name)
|
|
160
|
+
index = 2
|
|
161
|
+
while used[title]
|
|
162
|
+
suffix = " (#{index})"
|
|
163
|
+
name_max = [TITLE_MAX - prefix.size - suffix.size, 1].max
|
|
164
|
+
title = "#{prefix}#{sanitize_title(name, max: name_max)}#{suffix}"
|
|
165
|
+
index += 1
|
|
166
|
+
end
|
|
167
|
+
used[title] = true
|
|
168
|
+
title
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def self.column_letter(index)
|
|
173
|
+
number = Integer(index)
|
|
174
|
+
return "A" if number < 1
|
|
175
|
+
|
|
176
|
+
letters = +""
|
|
177
|
+
while number.positive?
|
|
178
|
+
number, remainder = (number - 1).divmod(26)
|
|
179
|
+
letters = ("A".ord + remainder).chr + letters
|
|
180
|
+
end
|
|
181
|
+
letters
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def self.kind_and_name(vehicle)
|
|
185
|
+
if vehicle.respond_to?(:kind) && vehicle.respond_to?(:name)
|
|
186
|
+
[vehicle.kind, vehicle.name]
|
|
187
|
+
else
|
|
188
|
+
[:deposit, vehicle.to_s]
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
private_class_method :kind_and_name
|
|
192
|
+
|
|
193
|
+
private
|
|
194
|
+
|
|
195
|
+
def ensure_grid(title, rows:, columns:)
|
|
196
|
+
props = sheet_properties.find { |item| item["title"] == title }
|
|
197
|
+
return unless props
|
|
198
|
+
|
|
199
|
+
current_rows = Integer(props.dig("gridProperties", "rowCount") || 0)
|
|
200
|
+
current_cols = Integer(props.dig("gridProperties", "columnCount") || 0)
|
|
201
|
+
needed_rows = Integer(rows)
|
|
202
|
+
needed_cols = Integer(columns)
|
|
203
|
+
updates = {}
|
|
204
|
+
fields = []
|
|
205
|
+
if needed_rows > current_rows
|
|
206
|
+
updates[:rowCount] = needed_rows
|
|
207
|
+
fields << "gridProperties.rowCount"
|
|
208
|
+
end
|
|
209
|
+
if needed_cols > current_cols
|
|
210
|
+
updates[:columnCount] = needed_cols
|
|
211
|
+
fields << "gridProperties.columnCount"
|
|
212
|
+
end
|
|
213
|
+
return if fields.empty?
|
|
214
|
+
|
|
215
|
+
batch_update(
|
|
216
|
+
[
|
|
217
|
+
{
|
|
218
|
+
updateSheetProperties: {
|
|
219
|
+
properties: {
|
|
220
|
+
sheetId: props["sheetId"],
|
|
221
|
+
gridProperties: updates
|
|
222
|
+
},
|
|
223
|
+
fields: fields.join(",")
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
]
|
|
227
|
+
)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def batch_update(requests)
|
|
231
|
+
uri = URI("#{format(SPREADSHEET_URI, @spreadsheet_id)}:batchUpdate")
|
|
232
|
+
with_retry do
|
|
233
|
+
@session.api_request(uri, method: :post, body: { requests: requests }.to_json)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def write_range(title, cells, values)
|
|
238
|
+
uri = URI("#{format(SPREADSHEET_URI, @spreadsheet_id)}/values/#{encode_range(title, cells)}")
|
|
239
|
+
uri.query = URI.encode_www_form("valueInputOption" => "RAW")
|
|
240
|
+
with_retry do
|
|
241
|
+
@session.api_request(
|
|
242
|
+
uri,
|
|
243
|
+
method: :put,
|
|
244
|
+
body: { range: "#{quote_sheet(title)}!#{cells}", majorDimension: "ROWS", values: values }.to_json
|
|
245
|
+
)
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def with_retry
|
|
250
|
+
attempts = 0
|
|
251
|
+
begin
|
|
252
|
+
yield
|
|
253
|
+
rescue DriveError => error
|
|
254
|
+
attempts += 1
|
|
255
|
+
raise unless error.message.include?("429") && attempts <= 4
|
|
256
|
+
|
|
257
|
+
delay = [20, 40, 60, 60][attempts - 1]
|
|
258
|
+
warn "Sheets quota hit, retrying in #{delay}s..."
|
|
259
|
+
sleep delay
|
|
260
|
+
retry
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def encode_range(title, cells)
|
|
265
|
+
URI.encode_www_form_component("#{self.class.quote_sheet(title)}!#{cells}")
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def quote_sheet(title)
|
|
269
|
+
self.class.quote_sheet(title)
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PortfolioPerformanceApi
|
|
4
|
+
class Snapshot
|
|
5
|
+
def initialize(source: PortfolioSource.new, cache: Cache.new, config: Config)
|
|
6
|
+
@source = source
|
|
7
|
+
@cache = cache
|
|
8
|
+
@config = config
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def accounts(nocache: false)
|
|
12
|
+
@cache.fetch(force: nocache) { load }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def load
|
|
18
|
+
downloaded = @source.fetch
|
|
19
|
+
parsed = FileReader.read(downloaded.fetch(:bytes), password: @config.portfolio_password)
|
|
20
|
+
client = Parser.parse(parsed)
|
|
21
|
+
Balances.compute(client, include_retired: @config.include_retired?).merge(
|
|
22
|
+
file: downloaded.fetch(:meta),
|
|
23
|
+
format: parsed.format.to_s
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|