Skip to content

Commit

Permalink
Add terminal-table gem
Browse files Browse the repository at this point in the history
Got first version of Terminal display to visualize
  • Loading branch information
taywils committed Feb 14, 2018
1 parent b1501fe commit 74395b2
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 56 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_crypto_etf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%w(bigdecimal coinbase/wallet faraday binance-ruby monetize).each do |library|
%w(bigdecimal coinbase/wallet faraday binance-ruby monetize terminal-table).each do |library|
require "#{library}"
end

Expand Down
49 changes: 49 additions & 0 deletions lib/ruby_crypto_etf/displays/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module RubyCryptoETF
module BaseDisplay
def display_bigdecimal(big_decimal)
if big_decimal.class != BigDecimal
big_decimal = BigDecimal(big_decimal)
end
big_decimal.to_s("F")
end

def display_usd_price(price_string)
Money.use_i18n = false

price_usd = BigDecimal(price_string)
price_split = price_usd.split
price_significant_string = price_split[1]
price_exponent = price_split[3]

dollars_display = ->(dollars_string) do
Monetize.parse(dollars_string).format.split('.').first
end

zero_dollars = dollars_display.call('0')

if price_exponent > 0
dollars = price_significant_string.slice(0...price_exponent)
slice_end = (price_significant_string.length - price_exponent - 2) * -1
if slice_end.zero?
cents = price_significant_string.slice(price_exponent..-1)
else
cents = price_significant_string.slice(price_exponent...slice_end)
cents = '00' if cents.empty?
end
"#{dollars_display.call(dollars)}.#{cents}"
elsif price_exponent.zero?
cents = price_significant_string.slice(0...2)
if cents.to_i.zero?
"#{zero_dollars}.00"
else
"#{zero_dollars}.#{cents}"
end
elsif price_exponent == -1
cent = price_significant_string.slice(0...1)
"#{zero_dollars}.0#{cent}"
else
"#{zero_dollars}.00"
end
end
end
end
27 changes: 17 additions & 10 deletions lib/ruby_crypto_etf/displays/terminal.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
module RubyCryptoETF
attr_reader :table

class Terminal
class TerminalDisplay
include BaseDisplay

def initialize(args = {})
table = Terminal::Table.new
table.headings = ['Coin', 'Amount', 'Value Est.', 'Exchanges']
table.title = 'Current MarketCap'
@table = table
@table = Terminal::Table.new.tap do |tbl|
tbl.headings = ['Coin', 'Amount', 'Value Est.', 'Exchange']
tbl.title = 'Current MarketCap'
end
end

def visualize(display_data)
market_cap = display_data[:market_cap] || ''
table.title += " #{market_cap}"
market_cap = display_usd_price(display_data[:market_cap]) || ''
table_rows = []
display_data[:coins].each do |coin|
table_rows << [coin.symbol, coin.amount, coin.value, coin.exchange]
table_rows << [coin.symbol,
display_bigdecimal(coin.amount),
display_usd_price(coin.value),
coin.exchange]
end

@table.tap do |tbl|
tbl.title += " #{market_cap}"
tbl.rows = table_rows
end
@table.rows = table_rows
@table
end
end
end
44 changes: 1 addition & 43 deletions lib/ruby_crypto_etf/markets/coin_market_cap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def self.symbol_mappings
def fetch_total_market_cap
response = @conn.get CoinMarketCap.endpoints[:market_cap]
market_cap_response = JSON.parse(response.body)
total_market_cap_usd_string = market_cap_response['total_market_cap_usd'].to_s
@capitalization = display_usd_price(total_market_cap_usd_string)
@capitalization = market_cap_response['total_market_cap_usd'].to_s
end

def fetch_tickers
Expand Down Expand Up @@ -59,47 +58,6 @@ def get_usd_for_symbol(coin_symbol)
def get_coin_ticker_for_symbol(symbol)
@coin_tickers.find { |ticker| ticker['symbol'] == symbol.upcase }
end

private

def display_usd_price(total_market_cap_usd_string)
Money.use_i18n = false

price_usd = BigDecimal(total_market_cap_usd_string)
price_split = price_usd.split
price_significant_string = price_split[1]
price_exponent = price_split[3]

dollars_display = ->(dollars_string) do
Monetize.parse(dollars_string).format.split('.').first
end

zero_dollars = dollars_display.call('0')

if price_exponent > 0
dollars = price_significant_string.slice(0...price_exponent)
slice_end = (price_significant_string.length - price_exponent - 2) * -1
if slice_end.zero?
cents = price_significant_string.slice(price_exponent..-1)
else
cents = price_significant_string.slice(price_exponent...slice_end)
cents = '00' if cents.empty?
end
"#{dollars_display.call(dollars)}.#{cents}"
elsif price_exponent.zero?
cents = price_significant_string.slice(0...2)
if cents.to_i.zero?
"#{zero_dollars}.00"
else
"#{zero_dollars}.#{cents}"
end
elsif price_exponent == -1
cent = price_significant_string.slice(0...1)
"#{zero_dollars}.0#{cent}"
else
"#{zero_dollars}.00"
end
end
end
end

4 changes: 2 additions & 2 deletions lib/ruby_crypto_etf/models/portfolio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def prepare_for_visualization
coin.value = coin.amount * coin_price
end

byebug
display_data
end

def load_from_settings(settings_file_path)
def initialize_exchanges_from_settings(settings_file_path)
settings = YAML.load_file(settings_file_path)

accounts = settings['accounts'] if settings.keys.include?('accounts')
Expand Down
1 change: 1 addition & 0 deletions ruby_crypto_etf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "binance-ruby", "~> 0.1", ">= 0.1.8"
spec.add_runtime_dependency "coinbase", "~> 4.1", ">= 4.1.0"
spec.add_runtime_dependency "monetize", "~> 1.7", ">= 1.7.0"
spec.add_runtime_dependency "terminal-table", "~> 1.8", ">= 1.8.0"
end

0 comments on commit 74395b2

Please sign in to comment.