Class: Coinpare::Commands::Holdings

Inherits:
Coinpare::Command show all
Defined in:
lib/coinpare/commands/holdings.rb

Constant Summary collapse

DEFAULT_PIE_RADIUS =
6

Constants inherited from Coinpare::Command

Coinpare::Command::DEFAULT_INTERVAL, Coinpare::Command::SYMBOLS

Instance Method Summary collapse

Methods inherited from Coinpare::Command

#add_color, #banner, #config, #cursor, #editor, #number_to_currency, #percent, #percent_change, #pick_arrow, #pick_color, #precision, #round_to, #screen, #shorten_currency, #timestamp

Constructor Details

#initialize(options) ⇒ Holdings



20
21
22
23
24
25
26
27
28
# File 'lib/coinpare/commands/holdings.rb', line 20

def initialize(options)
  @options = options
  @pastel = Pastel.new
  @timers = Timers::Group.new
  @spinner = TTY::Spinner.new(":spinner Fetching data...",
                              format: :dots, clear: true)
  @interval = @options.fetch("watch", DEFAULT_INTERVAL).to_f
  config.set("settings", "color", value: !@options["no-color"])
end

Instance Method Details

#add_coin(input, output) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/coinpare/commands/holdings.rb', line 171

def add_coin(input, output)
  prompt = create_prompt(input, output)
  context = self
  data = prompt.collect(&context.ask_coin)
  output.print cursor.up(3)
  output.print cursor.clear_screen_down
  data
end

#ask_coinObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/coinpare/commands/holdings.rb', line 150

def ask_coin
  -> (prompt) do
    key("name").ask("What coin do you own?") do |q|
      q.default "BTC"
      q.required(true, "You need to provide a coin")
      q.validate(/\w{2,}/, "Currency can only be chars.")
      q.convert ->(coin) { coin.upcase }
    end
    key("amount").ask("What amount?") do |q|
      q.required(true, "You need to provide an amount")
      q.validate(/[\d.]+/, "Invalid amount provided")
      q.convert ->(am) { am.to_f }
    end
    key("price").ask("At what price per coin?") do |q|
      q.required(true, "You need to provide a price")
      q.validate(/[\d.]+/, "Invalid prince provided")
      q.convert ->(p) { p.to_f }
    end
  end
end

#clear_output(output, lines) ⇒ Object



130
131
132
133
134
# File 'lib/coinpare/commands/holdings.rb', line 130

def clear_output(output, lines)
  output.print cursor.clear_screen_down if @options["watch"]
  yield if block_given?
  output.print cursor.up(lines) if @options["watch"]
end

#create_pie_charts(raw_data, display_data) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/coinpare/commands/holdings.rb', line 226

def create_pie_charts(raw_data, display_data)
  colors = i[yellow blue green cyan magenta red]
  radius = @options["pie"].to_i > 0 ? @options["pie"].to_i : DEFAULT_PIE_RADIUS
  base   = @options.fetch("base", config.fetch("settings", "base")).upcase
  to_symbol = nil
  past_data = []
  curr_data = []

  config.fetch("holdings").each do |coin|
    coin_data = raw_data[coin["name"]][base]
    to_symbol = display_data[coin["name"]][base]["TOSYMBOL"]
    past_price = coin["amount"] * coin["price"]
    curr_price = coin["amount"] * coin_data["PRICE"]

    past_data << { name: coin["name"], value: past_price }
    curr_data << { name: coin["name"], value: curr_price }
  end

  options = {
    colors: !@options["no-color"] && colors,
    radius: radius,
    legend: {
      left: 2,
      format: "%<label>s %<name>s #{to_symbol} %<currency>s (%<percent>.0f%%)",
      precision: 2
    }
  }

  [
    TTY::Pie.new(**options.merge(data: past_data)),
    TTY::Pie.new(**options.merge(data: curr_data)),
    to_symbol
  ]
end

#create_prompt(input, output) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/coinpare/commands/holdings.rb', line 136

def create_prompt(input, output)
  prompt = TTY::Prompt.new(
    prefix: "[#{add_color('c', :yellow)}] ",
    input: input, output: output,
    interrupt: -> { puts; exit 1 },
    enable_color: !@options["no-color"]
  )
  prompt.on(:keypress) { |event|
    prompt.trigger(:keydown) if event.value == "j"
    prompt.trigger(:keyup) if event.value == "k"
  }
  prompt
end

#display_coins(output, names, overridden_settings) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/coinpare/commands/holdings.rb', line 106

def display_coins(output, names, overridden_settings)
  response = Fetcher.fetch_prices(names.join(","),
                                  overridden_settings["base"].upcase,
                                  overridden_settings)
  return unless response
  table = if @options["pie"]
            setup_table_with_pies(response["RAW"], response["DISPLAY"])
          else
            setup_table(response["RAW"], response["DISPLAY"])
          end

  @spinner.stop

  lines = banner(overridden_settings).lines.size + 1 + (table.rows_size + 3)
  clear_output(output, lines) do
    output.puts banner(overridden_settings)
    if @options["pie"]
      output.puts table.render(:unicode, padding: [0, 2])
    else
      output.puts table.render(:unicode, padding: [0, 1], alignment: :right)
    end
  end
end

#execute(input: $stdin, output: $stdout) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/coinpare/commands/holdings.rb', line 30

def execute(input: $stdin, output: $stdout)
  config_saved = config.exist?
  if config_saved && @options["edit"]
    editor.open(config.source_file)
    return
  elsif @options["edit"]
    output.puts "Sorry, no holdings configuration found."
    output.print "Run \""
    output.print "$ #{add_color('coinpare holdings', :yellow)}\" "
    output.puts "to setup new altfolio."
    return
  end

  config.read if config_saved

  holdings = config.fetch("holdings")
  if holdings.nil? || (holdings && holdings.empty?)
    info = setup_portfolio(input, output)
    config.merge(info)
  elsif @options["add"]
    coin_info = add_coin(input, output)
    config.append(coin_info, to: ["holdings"])
  elsif @options["remove"]
    coin_info = remove_coin(input, output)
    config.remove(*coin_info, from: ["holdings"])
  elsif @options["clear"]
    prompt = create_prompt(input, output)
    answer = prompt.yes?("Do you want to remove all holdings?")
    if answer
      config.delete("holdings")
      output.puts add_color("All holdings removed", :red)
    end
  end

  holdings = config.fetch("holdings")
  no_holdings_left = holdings.nil? || (holdings && holdings.empty?)
  if no_holdings_left
    config.delete("holdings")
  end

  # Persist current configuration
  home_file = ::File.join(Dir.home, "#{config.filename}#{config.extname}")
  file = config.source_file
  config.write(file.nil? ? home_file : file, force: true)
  if no_holdings_left
    output.puts add_color("Please add holdings to your altfolio!", :green)
    exit
  end

  @spinner.auto_spin
  settings = config.fetch("settings")
  # command options take precedence over config settings
  overridden_settings = {}
  overridden_settings["exchange"] = @options.fetch("exchange", settings.fetch("exchange"))
  overridden_settings["base"]     = @options.fetch("base", settings.fetch("base"))
  holdings = config.fetch("holdings") { [] }
  names = holdings.map { |c| c["name"] }

  if @options["watch"]
    output.print cursor.hide
    @timers.now_and_every(@interval) do
      display_coins(output, names, overridden_settings)
    end
    loop { @timers.wait }
  else
    display_coins(output, names, overridden_settings)
  end
ensure
  @spinner.stop
  if @options["watch"]
    @timers.cancel
    output.print cursor.clear_screen_down
    output.print cursor.show
  end
end

#remove_coin(input, output) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/coinpare/commands/holdings.rb', line 180

def remove_coin(input, output)
  prompt = create_prompt(input, output)
  holdings = config.fetch("holdings")
  data = prompt.multi_select("Which hodlings to remove?") do |menu|
    holdings.each do |holding|
      menu.choice "#{holding['name']} (#{holding['amount']})", holding
    end
  end
  output.print cursor.up(1)
  output.print cursor.clear_line
  data
end

#setup_portfolio(input, output) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/coinpare/commands/holdings.rb', line 193

def setup_portfolio(input, output)
  output.print "\nCurrently you have no investments setup...\n" \
               "Let's change that and create your altfolio!\n\n"

  prompt = create_prompt(input, output)
  context = self
  data = prompt.collect do
    key("settings") do
      key("base").ask("What base currency to convert holdings to?") do |q|
        q.default "USD"
        q.convert ->(b) { b.upcase }
        q.validate(/\w{3}/, "Currency code needs to be 3 chars long")
      end
      key("exchange").ask("What exchange would you like to use?") do |q|
        q.default "CCCAGG"
        q.required true
      end
    end

    while prompt.yes?("Do you want to add coin to your altfolio?")
      key("holdings").values(&context.ask_coin)
    end
  end

  lines = 4 + # intro
          2 + # base + exchange
          data["holdings"].size * 4 + 1
  output.print cursor.up(lines)
  output.print cursor.clear_screen_down

  data
end

#setup_table(raw_data, display_data) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/coinpare/commands/holdings.rb', line 285

def setup_table(raw_data, display_data)
  base = @options.fetch("base", config.fetch("settings", "base")).upcase
  total_buy = 0
  total = 0
  to_symbol = nil

  table = TTY::Table.new(header: [
    { value: "Coin", alignment: :left },
    "Amount",
    "Price",
    "Total Price",
    "Cur. Price",
    "Total Cur. Price",
    "Change",
    "Change%"
  ])

  config.fetch("holdings").each do |coin|
    coin_data = raw_data[coin["name"]][base]
    coin_display_data = display_data[coin["name"]][base]
    past_price = coin["amount"] * coin["price"]
    curr_price = coin["amount"] * coin_data["PRICE"]
    to_symbol = coin_display_data["TOSYMBOL"]
    change = curr_price - past_price
    arrow = pick_arrow(change)
    total_buy +=  past_price
    total += curr_price

    coin_details = [
      { value: add_color(coin["name"], :yellow), alignment: :left },
      coin["amount"],
      "#{to_symbol} #{number_to_currency(round_to(coin['price']))}",
      "#{to_symbol} #{number_to_currency(round_to(past_price))}",
      add_color("#{to_symbol} #{number_to_currency(round_to(coin_data['PRICE']))}", pick_color(change)),
      add_color("#{to_symbol} #{number_to_currency(round_to(curr_price))}", pick_color(change)),
      add_color("#{arrow} #{to_symbol} #{number_to_currency(round_to(change))}", pick_color(change)),
      add_color("#{arrow} #{round_to(percent_change(past_price, curr_price))}%", pick_color(change))
    ]
    table << coin_details
  end

  total_change = total - total_buy
  arrow = pick_arrow(total_change)

  table << [
    { value: add_color("ALL", :cyan), alignment: :left }, "-", "-",
    "#{to_symbol} #{number_to_currency(round_to(total_buy))}", "-",
    add_color("#{to_symbol} #{number_to_currency(round_to(total))}", pick_color(total_change)),
    add_color("#{arrow} #{to_symbol} #{number_to_currency(round_to(total_change))}", pick_color(total_change)),
    add_color("#{arrow} #{round_to(percent_change(total_buy, total))}%", pick_color(total_change))
  ]

  table
end

#setup_table_with_pies(raw_data, display_data) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/coinpare/commands/holdings.rb', line 261

def setup_table_with_pies(raw_data, display_data)
  past_pie, curr_pie, to_symbol = *create_pie_charts(raw_data, display_data)

  total_change = curr_pie.total - past_pie.total
  arrow = pick_arrow(total_change)

  header_past = "Total Price (#{to_symbol} #{number_to_currency(round_to(past_pie.total))})"
  header_curr = [
    "Total Current Price (#{to_symbol} #{number_to_currency(round_to(curr_pie.total))}) ",
    add_color("#{arrow} #{to_symbol} #{number_to_currency(round_to(total_change))}", pick_color(total_change)),
  ].join

  table = TTY::Table.new(
    header: [
      { value: header_past, alignment: :center },
      { value: header_curr, alignment: :center }
    ]
  )
  past_pie.to_s.split("\n").zip(curr_pie.to_s.split("\n")).each do |past_part, curr_part|
    table << [past_part, curr_part]
  end
  table
end