Class: Coingecko::CLI
- Inherits:
-
Object
- Object
- Coingecko::CLI
- Defined in:
- lib/coingecko/cli.rb
Constant Summary collapse
- @@commands =
["ls", "list", "b", "back", "menu", "m", "q", "quit", "exit", "exit!", "find", "f"]
Instance Method Summary collapse
- #another_selection? ⇒ Boolean
- #check_selection(input) ⇒ Object
-
#decimal_separator(number) ⇒ Object
Helper Method.
- #find ⇒ Object
- #list_top_coins ⇒ Object
- #main_menu ⇒ Object
- #print_coin(id, currency = "usd") ⇒ Object
- #print_global_info(currency = "usd") ⇒ Object
- #print_top(list) ⇒ Object
- #quit ⇒ Object
- #round_if_num(num) ⇒ Object
- #run ⇒ Object
- #selection ⇒ Object
- #table_printer(rows) ⇒ Object
- #welcome ⇒ Object
Instance Method Details
#another_selection? ⇒ Boolean
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/coingecko/cli.rb', line 22 def another_selection? puts "Would you like make another selection?" input = gets.strip.downcase if input == "y" || input == "yes" elsif input == "n" || input == "no" quit return else check_selection(input) end end |
#check_selection(input) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/coingecko/cli.rb', line 50 def check_selection(input) case input when "ls", "list" list_top_coins when "menu", "m", "back", "b" when "q", "quit", "exit", "exit!" quit when "global", "g", "gen", "general" print_global_info when "find", "f" find else puts "Sorry! Did not Understand that." sleep 1 puts "Going back.." sleep 1 selection end end |
#decimal_separator(number) ⇒ Object
Helper Method. Separates numbers with decimals or returns ∞ when NaN.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/coingecko/cli.rb', line 93 def decimal_separator(number) #Helper Method. Separates numbers with decimals or returns ∞ when NaN. if number.is_a? Numeric whole, decimal = number.to_s.split(".") whole_with_commas = whole.chars.to_a.reverse.each_slice(3).map(&:join).join(",").reverse [whole_with_commas, decimal].compact.join(".") else number = "∞" number end end |
#find ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/coingecko/cli.rb', line 140 def find puts "Which coin would you like to find?" input = gets.chomp.downcase coin = find_by_name(input) || guess_by_name(input) case when coin.class == Coingecko::Global #found with pry to see what class it is sleep 1 puts "\nCoin match found!\n\n" sleep 2 print_coin(coin.id) when coin.class == Array && coin.length > 0 sleep 1 puts "\nMore than one possible match found. Please see below..\n\n" sleep 2 coin.each_with_index { |o, i| puts "#{i + 1} - #{o.name}" } puts "\nIf one of the coins provided is what you're looking for, please type 1 - #{coin.length}. Else type menu to go back.\n\n" answer = gets.chomp if answer.to_i > 0 #if string converted to_i will have value of 0 id = coin[answer.to_i - 1].id print_coin(id) else check_selection(answer) end else sleep 1 puts "\nSorry. There was no coin match for '#{input}'.\n\n" sleep 2 another_selection? end end |
#list_top_coins ⇒ Object
45 46 47 48 |
# File 'lib/coingecko/cli.rb', line 45 def list_top_coins list = Coingecko::Coin.new_from_top_100 print_top(list) end |
#main_menu ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/coingecko/cli.rb', line 36 def puts "\n-To list the top 100 coins type ls." puts "-To find a coin by name, type find." puts "-To get general info, type gen." puts "-To QUIT: please type q." sleep 1 selection end |
#print_coin(id, currency = "usd") ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/coingecko/cli.rb', line 172 def print_coin(id, currency="usd") puts "Retrieving your coin." sleep 0.5 puts ".." sleep 0.5 puts "....\n\n" sleep 0.5 coin = Coingecko::Coin.get_coin(id) rows = [] rows << [ "----------- #{coin.name}(#{coin.symbol}) - Rank##{coin.market_cap_rank} (Real-Time) ------------"] sleep 2 rows << [ "\nCurrent Price: $#{decimal_separator(coin.market_data["current_price"][currency])} | Market Cap: $#{decimal_separator(coin.market_data["market_cap"][currency])}"] rows << [ "24hr Trading Vol: $#{decimal_separator(coin.market_data["total_volume"][currency])}"] rows << [ "Available Supply: #{decimal_separator(coin.market_data["total_supply"])} / #{decimal_separator(coin.market_data["circulating_supply"])}\n\n" ] table_printer(rows) sleep 2 puts "\nDESCRIPTION:\n" sleep 2 puts coin.description["en"].gsub(/<\/?[^>]*>/, "") #.gsub strips HTML tags sleep 2 puts "\n----------------QUICK FACTS---------------\n" sleep 1 rows_two = [] rows_two << ["Percentage Change: \n(7 Days) =>(30 Days) =>(1 Year)"] rows_two << [ "#{round_if_num(coin.market_data["price_change_percentage_7d_in_currency"][currency])}% #{round_if_num(coin.market_data["price_change_percentage_30d_in_currency"][currency])}% #{round_if_num(coin.market_data["price_change_percentage_1y_in_currency"][currency])}% "] rows_two << [ "\n\nAll-Time High | ATH Date | Since ATH "] rows_two << ["#{coin.market_data["ath"][currency]} #{coin.market_data["ath_date"][currency][0..9]} #{round_if_num(coin.market_data["ath_change_percentage"][currency])}%"] table_printer(rows_two) rows_three = [] rows_three << ["Website: #{coin.links["homepage"][0]}"] rows_three << ["Reddit: #{coin.links["subreddit_url"]}"] rows_three << ["Github: #{coin.links["repos_url"]["github"][0]} "] rows_three << ["Twitter Handle: @#{coin.links["twitter_screen_name"]}"] rows_three << ["Genesis Date: #{coin.genesis_date}"] if coin.genesis_date rows_three << ["Last Updated: #{coin.last_updated[0..9]}"] sleep 2 table_printer(rows_three) sleep 2 another_selection? end |
#print_global_info(currency = "usd") ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/coingecko/cli.rb', line 117 def print_global_info(currency="usd") global_info = Coingecko::Global.new_from_global rows = [] puts "Returning Real-Time Global Info from Coingecko..." sleep 2 puts "\nGlobal Info:\n" sleep 0.5 rows << ["Total Cryptocurrencies: #{global_info.data["active_cryptocurrencies"]}"] rows << ["Total Market Cap: #{decimal_separator(global_info.data["total_market_cap"][currency])}"] rows << ["Total Volume: #{decimal_separator(global_info.data["total_volume"][currency])}"] table_printer(rows) sleep 2 rows_two = [] puts "Market Cap Share (Top 10):\n" sleep 1.5 global_info.data["market_cap_percentage"].each do |k, v| rows_two << ["#{k.upcase}: #{round_if_num(v)}%"] end table_printer(rows_two) sleep 2 another_selection? end |
#print_top(list) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/coingecko/cli.rb', line 71 def print_top(list) puts "Here are the Top 100 Coins!" sleep 1.0 puts ".." sleep 1.0 puts "....\n\n" sleep 0.5 Coingecko::Coin.top_coins.each_with_index do |coin, index| puts "#{index + 1}. #{coin.name}" end sleep 0.5 puts "\n\nWhich coin would you like to check out? Please type a number 1-100." answer = gets.chomp.to_i if answer > 0 #if string converted to_i will have value of 0 id = Coingecko::Coin.top_coins[answer - 1].id print_coin(id) else check_selection(answer) end end |
#quit ⇒ Object
213 214 215 216 217 218 |
# File 'lib/coingecko/cli.rb', line 213 def quit sleep 0.5 puts "\nGoodbye! See you next time." sleep 1 system('clear') end |
#round_if_num(num) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/coingecko/cli.rb', line 104 def round_if_num(num) if num.is_a? Numeric num.round(1) else "Coingecko Returned N/A" end end |
#run ⇒ Object
6 7 8 9 10 |
# File 'lib/coingecko/cli.rb', line 6 def run welcome sleep 0.5 selection end |
#selection ⇒ Object
16 17 18 19 20 |
# File 'lib/coingecko/cli.rb', line 16 def selection puts "\nWhat would you like to do? For the main menu please type menu." input = gets.strip.downcase self.check_selection(input) #from now on self will be implicit as method receiver. end |
#table_printer(rows) ⇒ Object
112 113 114 115 |
# File 'lib/coingecko/cli.rb', line 112 def table_printer(rows) table = Terminal::Table.new :rows => rows puts table end |
#welcome ⇒ Object
12 13 14 |
# File 'lib/coingecko/cli.rb', line 12 def welcome puts "\nWelcome to Coingecko! Powered by CoinGecko API.\n" end |