Class: NotableBooks2018::CLI

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

Instance Method Summary collapse

Instance Method Details

#book_info_contents(book) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/notable_books_2018/cli.rb', line 261

def book_info_contents(book)
  puts "\n--------------------------------------------"
  puts Paint["#{book.title}", :bright]
  puts "by #{book.author}"
  puts "--------------------------------------------"
  puts "#{Paint["\nPublisher:", :bright]} #{book.publisher}"
  puts "#{Paint["Price:", :bright]} #{book.price}"
  if book.genre.length == 1
    puts Paint["\nGenre:", :bright]
  else
    puts Paint["\nGenres:", :bright]
  end
  book.genre.collect do |genre|
    puts "#{genre.name}"
  end
  puts Paint["\nDescription:",:bright]
  puts "#{wrap_text(book.description)}"
  puts "\n--------------------------------------------"
end

#choose_books_by_numObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/notable_books_2018/cli.rb', line 187

def choose_books_by_num
  @number_input = nil

  puts "\nEnter a #{Paint["number", :magenta]} to see a list of books."
  puts "Enter #{Paint["'main'", :magenta]} to return to the main menu or "\
  "#{Paint["'exit'", :magenta]} to quit."

  @number_input = gets.chomp

  if (1..100).include?(@number_input.to_i)
    print_book_list(@number_input.to_i)
    select_book_by_number
    see_more_books
  elsif @number_input.downcase == "main"
    welcome
  elsif @number_input.downcase == "exit"
    goodbye
  else
    puts "\nThat is not a valid selection."
    choose_books_by_num
  end
  puts ""
end

#choose_displayObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/notable_books_2018/cli.rb', line 26

def choose_display
  puts "\nEnter #{Paint["1", :magenta]} to view books by genre."
  puts "Enter #{Paint["2", :magenta]} to view the numbered book list."
  puts "Or, enter #{Paint["'exit'", :magenta]} to quit."

  input = gets.chomp
  if input.to_i == 1
    view_books_by_genre
  elsif input.to_i == 2
    view_books_by_num
  elsif input == "exit"
    goodbye
  else
    puts "\nThat is not a valid selection."
    choose_display
  end
end

#choose_genreObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/notable_books_2018/cli.rb', line 64

def choose_genre
  puts "\nEnter a #{Paint["genre name", :magenta]} to browse books by genre."
  puts "Enter #{Paint["'main'", :magenta]} to return to the main menu or "\
    "#{Paint["'exit'", :magenta]} to quit."

  @genre_name = gets.chomp.downcase

  if @all_genre_names.include?(@genre_name)
    print_books_by_genre(@genre_name)
  elsif @genre_name == "main"
    welcome
  elsif @genre_name == "exit"
    goodbye
  else
    puts "\nThat is not a valid entry."
    choose_genre
  end
end

#display_number_groupsObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/notable_books_2018/cli.rb', line 168

def display_number_groups
  puts <<~DOC

    --------------------------------------------
    #{Paint["View Books Numbered:", :bright]}
    --------------------------------------------
    1-10
    11-20
    21-30
    31-40
    41-50
    51-60
    61-70
    71-80
    81-90
    91-100
  DOC
end

#goodbyeObject



313
314
315
316
# File 'lib/notable_books_2018/cli.rb', line 313

def goodbye
  puts "\nThank you for browsing Notable Books 2018. Happy reading!"
  exit
end

#list_genresObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/notable_books_2018/cli.rb', line 51

def list_genres
  puts "\n--------------------------------------------"
  puts Paint["Displaying All Genres", :bright]
  puts "--------------------------------------------"
  puts "\n"

  @all_genre_names = []
  NotableBooks2018::Genre.all.collect do |genre|
    puts genre.name
    @all_genre_names << genre.name.downcase
  end
end


253
254
255
256
257
258
259
# File 'lib/notable_books_2018/cli.rb', line 253

def print_book_info(book_index)
  NotableBooks2018::Book.all.each.with_index(1) do |book, index|
    if index == book_index
      book_info_contents(book)
    end
  end
end


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/notable_books_2018/cli.rb', line 122

def print_book_info_from_genre(book_index)
  NotableBooks2018::Genre.all.each do |genre_instance|
    if @genre_name == genre_instance.name.downcase
      genre_instance.books.each.with_index(1) do |book, index|
        if book_index == index
          book_info_contents(book)
        end
      end
    end
  end
end


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/notable_books_2018/cli.rb', line 211

def print_book_list(by_number)
  if by_number == 100
    puts "\n--------------------------------------------"
    puts "#{Paint["Displaying The 100th Notable Book", :bright]}"
    puts "--------------------------------------------"
  elsif by_number >= 92 && by_number != 100
    puts "\n--------------------------------------------"
    puts "#{Paint["Displaying Notable Books #{by_number} - 100", :bright]}"
    "--------------------------------------------"
  else
    puts "\n--------------------------------------------"
    puts "#{Paint["Displaying Notable Books #{by_number} - #{by_number+9}",
      :bright]}"
    puts "--------------------------------------------"
    puts ""
  end

  NotableBooks2018::Book.all[by_number-1, 10].each.with_index(by_number) do |book, index|
      puts "#{index}. #{book.title} by #{book.author}"
  end
end


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/notable_books_2018/cli.rb', line 83

def print_books_by_genre(genre_name)
  puts "\n--------------------------------------------"
  puts Paint["Viewing: #{genre_name.split.map(&:capitalize!).join(" ")}",
    :bright]
  puts "--------------------------------------------"
  puts ""

  @indices_from_genre = []
  NotableBooks2018::Genre.all.each do |genre|
    if genre_name == genre.name.downcase
      genre.books.each.with_index(1) do |book, index|
        puts "#{index}. #{book.title} by #{book.author}"
        @indices_from_genre << index.to_i
      end
    end
  end
end

#runObject



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

def run
  NotableBooks2018::Scraper.scrape_book_info
  welcome
  goodbye
end

#see_more_booksObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/notable_books_2018/cli.rb', line 281

def see_more_books
  puts <<~DOC
    Would you like to see another book?
    Enter #{Paint["'back'", :magenta]} to return to your selected list.
    Enter #{Paint["'list'", :magenta]} to return to the main list of books by number.
    Enter #{Paint["'genre'", :magenta]} to switch to browsing books by genre.
    Enter #{Paint["'exit'", :magenta]} to quit.
  DOC

  input = gets.chomp.downcase

  case input
  when "back"
      print_book_list(@number_input.to_i)
      select_book_by_number
      see_more_books
    when "list"
      view_books_by_num
    when "genre"
      view_books_by_genre
    when "exit"
      goodbye
    else
      puts "\nThat is not a valid selection."
      see_more_books
  end
end

#see_more_books_by_genreObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/notable_books_2018/cli.rb', line 134

def see_more_books_by_genre
  puts <<~DOC
    Would you like to see another book?
    Enter #{Paint["'back'", :magenta]} to return to your chosen genre's books.
    Enter #{Paint["'list'", :magenta]} to return to view all genres.
    Enter #{Paint["'number'", :magenta]} to switch to browsing books by number.
    Enter #{Paint["'exit'", :magenta]} to quit.
  DOC

  input = gets.chomp.downcase

  case input
    when "back"
      print_books_by_genre(@genre_name)
      select_book_by_number_through_genre
      print_book_info_from_genre(@book_index_from_genre)
      see_more_books_by_genre
    when "list"
      view_books_by_genre
    when "number"
      view_books_by_num
    when "exit"
      goodbye
    else
      puts "\nThat is not a valid selction."
      see_more_books_by_genre
  end
end

#select_book_by_numberObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/notable_books_2018/cli.rb', line 234

def select_book_by_number
  puts "\nEnter the #{Paint["number", :magenta]} of a book you would like "\
    "more information about."
  puts "Or, enter #{Paint["'back'", :magenta]} to return to the numbered "\
    "book list or #{Paint["'exit'", :magenta]} to quit."
  second_input = gets.chomp

  if ((@number_input.to_i)..((@number_input.to_i)+9)).include?(second_input.to_i)
    print_book_info(second_input.to_i)
  elsif second_input == "back"
    view_books_by_num
  elsif second_input.downcase == "exit"
    goodbye
  else
    puts "\nPlease enter a number #{@number_input.to_i}-#{(@number_input.to_i)+9}."
    select_book_by_number
  end
end

#select_book_by_number_through_genreObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/notable_books_2018/cli.rb', line 101

def select_book_by_number_through_genre
  puts "\nEnter the #{Paint["number", :magenta]} of a book you would like "\
    "to read more about."
  puts "Or, enter #{Paint["'back'", :magenta]} to return the genre list or "\
    "#{Paint["'exit'", :magenta]} to quit."

  @book_index_from_genre = gets.chomp

  if @indices_from_genre.include?(@book_index_from_genre.to_i)
    print_book_info_from_genre(@book_index_from_genre.to_i)
  elsif @book_index_from_genre == "back"
    view_books_by_genre
  elsif @book_index_from_genre == "exit"
    goodbye
  else
    puts "\nPlease enter a number between #{@indices_from_genre.first}-"\
    "#{@indices_from_genre.last}."
    select_book_by_number_through_genre
  end
end

#view_books_by_genreObject



44
45
46
47
48
49
# File 'lib/notable_books_2018/cli.rb', line 44

def view_books_by_genre
  list_genres
  choose_genre
  select_book_by_number_through_genre
  see_more_books_by_genre
end

#view_books_by_numObject



163
164
165
166
# File 'lib/notable_books_2018/cli.rb', line 163

def view_books_by_num
  display_number_groups
  choose_books_by_num
end

#welcomeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/notable_books_2018/cli.rb', line 9

def welcome
  puts "\n--------------------------------------------"
  puts Paint["Welcome to Notable Books of 2018!", :bright]
  puts "--------------------------------------------"
  puts <<~DOC

    At the end of 2018, The New York Times Book Review published a list of the
    notable new book releases from that year. You can use this gem to browse
    2018's book selections and find more information about each book that
    interests you.

    Would you like to browse books by genre or view books by list number?
  DOC

  choose_display
end

#wrap_text(txt, col = 80) ⇒ Object



309
310
311
# File 'lib/notable_books_2018/cli.rb', line 309

def wrap_text(txt, col = 80)
  txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/, "\\1\\3\n")
end