Class: CoinMarketCap::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



3
4
5
6
7
# File 'lib/coin_market_cap/cli.rb', line 3

def initialize
  puts "Welcome to the CoinMarketCap list gem!"
  list_top10
  user_input
end

Instance Method Details

#color_change(number) ⇒ Object



55
56
57
58
# File 'lib/coin_market_cap/cli.rb', line 55

def color_change(number)
  number = number.gsub("%", "").to_f
  (number < 0) ? "#{number.to_s}%".colorize(:red) : "#{number.to_s}%".colorize(:green)
end

#display_detail(coin) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/coin_market_cap/cli.rb', line 43

def display_detail(coin)
  puts "\n"
  puts "#{coin.name.upcase.colorize(:light_blue)}"
  puts "Price: #{coin.price}"
  puts "Volume: #{coin.volume}"
  puts "Market Cap: #{coin.mcap}"
  puts "24H Change: #{color_change(coin.change)}"
  puts "Circulating Supply: #{coin.cir_supply}"
  puts "Total Supply: #{coin.max_supply}"
  puts "Website: #{coin.website}"
end

#list_top10Object



9
10
11
12
13
14
15
# File 'lib/coin_market_cap/cli.rb', line 9

def list_top10
  puts "\nList of the TOP10 cryptocurrencies:"
  @top10 = CoinMarketCap::Scraper.list
  @top10.each.with_index(1) { |coin, i|
    puts "#{i}. #{coin.name.upcase.colorize(:light_blue)} / #{coin.price}$ / #{coin.mcap}$ / #{color_change(coin.change)}"
  }
end

#user_inputObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/coin_market_cap/cli.rb', line 17

def user_input
  input = nil

  while input != "exit"
    puts "\nPlease choose a coin, display the list or exit:"
    input = gets.strip.downcase

    if input == "list"
      list_top10
    elsif input == "exit"
      puts "\nBye bye, see you next time!"
      break
    elsif input.to_i.between?(1,10)
      coin = @top10[input.to_i - 1]

      if coin.website.nil?
        coin = CoinMarketCap::Scraper.get_coin(coin)
      end

      display_detail(coin)
    else
      puts "Please type a number, list or exit. Try again!"
    end
  end
end