Class: CryptoQuote::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_quote/cli.rb

Constant Summary collapse

@@ticker =
CryptoQuote::Ticker.new.data

Instance Method Summary collapse

Instance Method Details

#callObject



4
5
6
7
8
# File 'lib/crypto_quote/cli.rb', line 4

def call
	welcome
	list_crypto_currencies
	get_selection_input
end

#crypto_detail(input) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/crypto_quote/cli.rb', line 77

def crypto_detail(input)
	c = ticker[input.to_i - 1]
	
	price = currency_format(c['price_usd'])
	market_cap = currency_format(c['market_cap_usd']).gsub(".0", "")
	daily_vol = currency_format(c['24h_volume_usd']).gsub(".0", "")
	available_supply = currency_format(c['available_supply']).gsub(".0", "").gsub("$", "")
	max_supply = c['max_supply']!=nil ? currency_format(c['max_supply']).gsub(".0", "").gsub("$", "") : "N/A"
	available_percent = max_supply=="N/A" ? "N/A" : ((c['available_supply'].to_f / c['max_supply'].to_f)*100).round(2)
	one_hour = (c['percent_change_1h'][0] == '-' ? c['percent_change_1h'].red : c['percent_change_1h'].green)
	twenty_four_hour = (c['percent_change_24h'][0] == '-' ? c['percent_change_24h'].red : c['percent_change_24h'].green)
	seven_days = (c['percent_change_7d'][0] == '-' ? c['percent_change_7d'].red : c['percent_change_7d'].green)

	row1 = []
	row1 << [ c['symbol'], price, one_hour+'%', twenty_four_hour+'%', seven_days+'%' ]
	table1 = Terminal::Table.new :title => "#{c['name']} Details", :headings => ['Symbol', 'Price', '1 Hr %', '24 Hr %', '7 Day %'], :rows => row1		
	
	row2 = []
	row2 << [ market_cap, daily_vol, available_supply, max_supply, "#{available_percent}%"]
	table2 = Terminal::Table.new :title => "#{c['name']} Market Information", :headings => ['Market Cap', '24 Hr Volume', 'Avaiable Supply', 'Total Supply', 'Available %'], :rows => row2

	puts table1, table2
end

#currency_format(number) ⇒ Object



101
102
103
# File 'lib/crypto_quote/cli.rb', line 101

def currency_format(number)
	number != nil ? number.gsub('.00','').reverse.scan(/(\d*\.\d{1,3}|\d{1,3})/).join(',').reverse.insert(0, "$") : 0
end

#detailed_instructionsObject



49
50
51
52
53
# File 'lib/crypto_quote/cli.rb', line 49

def detailed_instructions
	puts "Type l or list to go back to the main list."
	puts "Type exit to exit."
	get_detailed_input
end

#get_detailed_inputObject



55
56
57
58
59
60
61
62
63
# File 'lib/crypto_quote/cli.rb', line 55

def get_detailed_input
	input = gets.strip
	if input == "exit"
		goodbye
		exit
	elsif input == "l" || input == "list"
		list_crypto_currencies
	end
end

#get_selection_inputObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crypto_quote/cli.rb', line 35

def get_selection_input
	input = gets.strip
		if input == "exit"
			goodbye
			exit
		elsif input == "l" || input == "list"
			list_crypto_currencies
			get_selection_input
		elsif valid_input?(input)
			crypto_detail(input)
		end
		detailed_instructions
end

#goodbyeObject



105
106
107
# File 'lib/crypto_quote/cli.rb', line 105

def goodbye
	puts "Thank you for using Crypto Quote!"
end

#list_crypto_currenciesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/crypto_quote/cli.rb', line 15

def list_crypto_currencies
	rows = []
	puts ""
	self.ticker.each do |c| 
		one_hour = (c['percent_change_1h'][0] == '-' ? c['percent_change_1h'].red : c['percent_change_1h'].green)
		twenty_four_hour = (c['percent_change_24h'][0] == '-' ? c['percent_change_24h'].red : c['percent_change_24h'].green)
		rows << [c['rank'], c["symbol"], c["name"], currency_format(c['price_usd']), one_hour+"%", twenty_four_hour+"%"]
	end
	table = Terminal::Table.new :title => "Crypto Quotes", :headings => ['Rank', 'Ticker', 'Name', 'Price', '1 Hr %', '24 Hr %'], :rows => rows
	puts table
	puts ""
	selection_instructions
end

#selection_instructionsObject



29
30
31
32
33
# File 'lib/crypto_quote/cli.rb', line 29

def selection_instructions
	puts "Type the number of a crypto currency to see its details."
	puts "Type exit to exit."
	get_selection_input
end

#tickerObject



65
66
67
# File 'lib/crypto_quote/cli.rb', line 65

def ticker
	@@ticker
end

#ticker_countObject



69
70
71
# File 'lib/crypto_quote/cli.rb', line 69

def ticker_count
	@@ticker.count
end

#valid_input?(input) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/crypto_quote/cli.rb', line 73

def valid_input?(input)
	input == "exit" || input == "l" || input == "list" || (input.to_i > 0 && input.to_i <= ticker_count) ? true : false
end

#welcomeObject



10
11
12
13
# File 'lib/crypto_quote/cli.rb', line 10

def welcome
	puts "Welcome to the Cryto Quote App."
	puts "The quotes are from http://coinmarketcap.com and are delayed by 5 minutes."
end