Class: CodingResources::CLI

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

Constant Summary collapse

@@page =
0

Instance Method Summary collapse

Instance Method Details

#detailsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/coding_resources/cli.rb', line 90

def details
  puts "Please type the number of the book you would like more information on."

  puts ""
  input = gets.strip.to_i
  puts ""

  book = CodingResources::Books.pages[@@page][input - 1]
  book.details

  i = 0
  while i < 1 || i > 3
    puts ""
    puts "Please select an option:"
    puts "1. Go back to the list of books."
    puts "2. Search for a new book."
    puts "3. Exit"
    puts ""
    i = gets.strip.to_i
    if i > 0
      case i
      when 1
        list
      when 2
        search
      when 3
        puts "Have a nice day!"
        exit
      end
    else
      puts "That was an invalid selection."
    end
  end
end

#listObject



34
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
# File 'lib/coding_resources/cli.rb', line 34

def list
  if CodingResources::Books.pages.length > 0
    while @@page >= 0 && @@page <= CodingResources::Books.pages.length
      CodingResources::Books.list(@@page)

      puts ""

      puts "Please select an option:"
      puts "1. Get more details on a book."
      puts "2. Go to the next page of 15 books."
      puts "3. Go back one page."
      puts "4. Go to a specific page."
      puts "5. Search for a book."
      puts "6. Exit"
      puts ""

      input = gets.strip.to_i

      case input
      when 1
        details
        break
      when 2
        @@page += 1
      when 3
        @@page -= 1 unless @@page == 0
      when 4
        puts "Please enter 1-#{CodingResources::Books.pages.length} to go to that specific page."
        i = gets.strip.to_i
        @@page = i - 1
      when 5
        search
        break
      when 6
        exit
      when input > 6 || input < 1
        puts "That was an invalid entry."
      end
    end
  else
    puts ""
    puts "No results found."
    puts "Going back to the main menu"
    puts ""
    menu
  end
end


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/coding_resources/cli.rb', line 9

def menu
  CodingResources::Books.create_all_books
  puts "Welcome to Coding Resources"
  puts "Please select an option"
  puts "1. List all books."
  puts "2. Search for a book."
  puts "3. Exit."
  puts ""
  input = gets.strip.to_i
  if input > 0
    case input
    when 1
      list
    when 2
      search
    when 3
      puts "Have a nice day!"
      exit
    end
  else
    puts "That was an invalid selection."
    menu
  end
end

#runObject



5
6
7
# File 'lib/coding_resources/cli.rb', line 5

def run
  menu
end

#searchObject



82
83
84
85
86
87
88
# File 'lib/coding_resources/cli.rb', line 82

def search
  puts "Please enter the name or part of the name of the book you are searching for."
  input = gets.strip.downcase
  CodingResources::Books.search(input)
  @@page = 0
  list
end