class Coingecko::CLI

Public Instance Methods

another_selection?() click to toggle source
# 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"
    main_menu
  elsif input == "n" || input == "no"
    quit
    return
  else
   check_selection(input)
  end
end
check_selection(input) click to toggle source
# File lib/coingecko/cli.rb, line 50
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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/coingecko/cli.rb, line 45
def list_top_coins
    list = Coingecko::Coin.new_from_top_100
    print_top(list)
end
main_menu() click to toggle source
print_coin(id, currency="usd") click to toggle source
print_global_info(currency="usd") click to toggle source
print_top(list) click to toggle source
quit() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/coingecko/cli.rb, line 6
def run
  welcome
  sleep 0.5
  selection
end
selection() click to toggle source
# 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) click to toggle source
# File lib/coingecko/cli.rb, line 112
def table_printer(rows)
  table = Terminal::Table.new :rows => rows
  puts table
end
welcome() click to toggle source
# File lib/coingecko/cli.rb, line 12
def welcome
  puts "\nWelcome to Coingecko! Powered by CoinGecko API.\n"
end