Class: CryptoMarket::CLI

Inherits:
Object
  • Object
show all
Includes:
CryptoMarket
Defined in:
lib/crypto_market/cli.rb

Constant Summary

Constants included from CryptoMarket

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CryptoMarket

#terminal_table

Constructor Details

#initializeCLI

Initialize the CLI with an instance of all currencies



6
7
8
# File 'lib/crypto_market/cli.rb', line 6

def initialize
  @currencies = CryptoMarket::Currencies.new
end

Instance Attribute Details

#currenciesObject

Returns the value of attribute currencies.



3
4
5
# File 'lib/crypto_market/cli.rb', line 3

def currencies
  @currencies
end

Instance Method Details

#enter_input_msgObject



30
31
32
# File 'lib/crypto_market/cli.rb', line 30

def enter_input_msg
  print 'Enter a number: '
end

#enter_valid_input_msgObject



26
27
28
# File 'lib/crypto_market/cli.rb', line 26

def enter_valid_input_msg
  puts 'Enter a correct number from the list'
end

#greetingObject

Greeting message



18
19
20
21
22
23
24
# File 'lib/crypto_market/cli.rb', line 18

def greeting
  <<-DOC
Welcome to the Crypto Market
All the prices are updated every 5 minutes
Enter a number to navigate
  DOC
end

User navigation



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/crypto_market/cli.rb', line 35

def navigation
  table = terminal_table do |t|
    t.title = greeting
    t.add_row [1, 'Get more information about a specific coin']
    t.add_row [2, 'Sort by price']
    t.add_row [3, 'Sort by price change the last 24h']
    t.add_row [4, 'To exit the program']
    t.style = { all_separators: true, width: 60 }
  end
  puts table
  enter_input_msg
  input = gets.strip.to_i
  loop do
    case input
    when 1
      coin_search
      navigation
    when 2
      currencies.print_sorted_prices
      navigation
    when 3
      table = terminal_table do |t|
        t.title = 'Select a number'
        t.add_row [1, '(+) Changed coins the last 24h']
        t.add_row [2, '(-) Changed coins the last 24h']
        t.add_row [3, 'All Changed coins the last 24h']
        t.style = { all_separators: true, width: 60 }
      end
      input = nil
      until input == 1 || input == 2 || input == 3
        puts table
        enter_input_msg
        input = gets.strip.to_i
        if input == 1
          currencies.print_sorted_changes('positive')
          navigation
        elsif input == 2
          currencies.print_sorted_changes('negative')
          navigation
        elsif input == 3
          currencies.print_sorted_changes
          navigation
        else
          enter_valid_input_msg
        end
      end
    when 4
      puts "Goodbye you're now closing Crypto Market!"
      exit
    else
      enter_valid_input_msg
      navigation
    end
  end
end

#runObject

Run command to start the program



11
12
13
14
15
# File 'lib/crypto_market/cli.rb', line 11

def run
  currencies.run
  puts ascii_welcome
  navigation
end