Class: Coingecko::CLI

Inherits:
Object
  • Object
show all
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

Instance Method Details

#another_selection?Boolean



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/coingecko/cli.rb', line 20

def another_selection?
  puts "Would you like make another selection?"
  input = gets.strip.downcase
  if input == "y" || input == "yes"
    main_menu
  elsif input == "n" || input == "no"
    quit
    return
  else
   check_selection(input)
  end
end

#check_selection(input) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/coingecko/cli.rb', line 48

def check_selection(input)
    case input
    when "ls", "list"
      list_top_coins
    when "menu", "m", "back", "b"
      main_menu
    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.



91
92
93
94
95
96
97
98
99
100
# File 'lib/coingecko/cli.rb', line 91

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

#findObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/coingecko/cli.rb', line 138

def find
  puts "Which coin would you like to find?"
  input = gets.strip.downcase

  Coingecko::Global.get_all_coins_list
  all_coins_basic = Coingecko::Global.all_coins_list
  returned_id = ""
  all_coins_basic.each do |coin|
    if coin.name.strip.downcase == input
      returned_id = coin.id
    end
   end

 if !returned_id.empty?
      sleep 1
    puts "\nCoin match found!\n\n"
      sleep 2
    print_coin(returned_id)
 else
      sleep 1
   puts "\nSorry. There was no coin match for '#{input}'.\n\n"
      sleep 2
   another_selection?
 end

end

#list_top_coinsObject



43
44
45
46
# File 'lib/coingecko/cli.rb', line 43

def list_top_coins
    list = Coingecko::Coin.new_from_top_100
    print_top(list)
end


34
35
36
37
38
39
40
41
# File 'lib/coingecko/cli.rb', line 34

def main_menu
  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


165
166
167
168
169
170
171
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
# File 'lib/coingecko/cli.rb', line 165

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


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/coingecko/cli.rb', line 115

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


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/coingecko/cli.rb', line 69

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

#quitObject



206
207
208
209
210
211
# File 'lib/coingecko/cli.rb', line 206

def quit
  sleep 0.5
  puts "\nGoodbye! See you next time."
  sleep 1
  system('clear')
end

#round_if_num(num) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/coingecko/cli.rb', line 102

def round_if_num(num)
  if num.is_a? Numeric
      num.round(1)
  else
      "Coingecko Returned N/A"
  end
end

#runObject



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

def run
  welcome
  sleep 0.5
  selection
end

#selectionObject



14
15
16
17
18
# File 'lib/coingecko/cli.rb', line 14

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



110
111
112
113
# File 'lib/coingecko/cli.rb', line 110

def table_printer(rows)
  table = Terminal::Table.new :rows => rows
  puts table
end

#welcomeObject



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

def welcome
  puts "\nWelcome to Coingecko! Powered by CoinGecko API.\n"
end