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



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
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/coding_resources/cli.rb', line 97

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

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

  if input > 0 && input <= CodingResources::Books.pages[@@page].length
    book = CodingResources::Books.pages[@@page][input - 1]
    book.details
  else
    puts "Please choose a different number."
    details
  end

  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



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

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. Go back to the main menu"
      puts "7. 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
        menu
        break
      when 7
        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


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

def menu
  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
      CodingResources::Books.create_pages("all_books")
      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
8
9
10
# File 'lib/coding_resources/cli.rb', line 5

def run
  puts "Welcome to Coding Resources"
  puts "Retrieving books..."
  CodingResources::Books.create_all_books
  menu
end

#searchObject



88
89
90
91
92
93
94
95
# File 'lib/coding_resources/cli.rb', line 88

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)
  CodingResources::Books.create_pages("search")
  @@page = 0
  list
end