class Abcdistill::CLI

note that back when we put this class CLI inside the Distill module we just define it as class cli but here it's in a seprate file. so things change. i'm not exactly sure the way things work. i'm just parroting and pryaing it works apparently putting in like this creates the same effect as if you phsyically put it inside the module even though it's not, its in a seprate file hiiiii

Public Class Methods

new() click to toggle source
# File lib/abcdistill/cli.rb, line 10
def initialize
  Abcdistill::Genre.addthegenres()
  #if u add too much at initalize, it slows down the app. but i think here it take same amount either way
end

Public Instance Methods

call() click to toggle source
# File lib/abcdistill/cli.rb, line 15
def call
  #this is the "HomePage"
  puts "Hello!"
  puts "Type quit anytime to quit the app"
  list_genres
  process_genrechoice
  say_goodbye
end
display_book_detail(book) click to toggle source
# File lib/abcdistill/cli.rb, line 47
def display_book_detail(book)
  puts "Title: #{book.title}"
  puts "Genre: #{book.genre}"
  puts "Author: #{book.authorname}"
  puts "Pages: #{book.pages}"
  puts "Publish Date: #{book.publishdate}"
  puts "Amazon Link: #{book.amazonlink}"
  puts "Description: #{book.description}"
end
display_books_in_genre(genre) click to toggle source
# File lib/abcdistill/cli.rb, line 34
def display_books_in_genre(genre)
  #aka display books in genre
  #takes the argument of the genre instance and then display books in that genres
  #u only need to fetch it once. make sure this happen properly
  puts genre.name
  puts genre.genrelink
  #scrape the books with scraper tool
  Abcdistill::Scraper.new.books_in_genre(genre)
  #display the books:
  Abcdistill::Genre.display_books_of_genre(genre)

end
list_genres() click to toggle source
# File lib/abcdistill/cli.rb, line 24
def list_genres
  Abcdistill::Genre.list_genre_names()
  end
process_bookchoice(genre) click to toggle source
# File lib/abcdistill/cli.rb, line 57
  def process_bookchoice(genre)
    #context: user choose a number from a list of 15 books in that genres (from display_genre method)
    #and the task here is to display the details
    #besides the number, we need to figure out the context of the list of choices that was displayed
    #that is needed so that we can figure out what book the user wants
    #to figure out the context, we'll just ask the genre, we won't ask the for the list, we'll get the source of the list ourselves
    #cuz we don't want to have code read the list and understand what it means and search it up it's a mess
    input = nil
    puts "Type the number of the book you want"
    while input != "quit"
      input = gets.strip

      if input.to_i > 0 && input.to_i < 16 #we're strict so we don't run into nil error
        #figure out what book the user chose:
        book = Abcdistill::Genre.books_of_genre(genre)[input.to_i - 1]
        puts "the book we think the user chose is: #{book.title}"
        # Distill::Genre.display_books_of_genre called in display_books_in_genre actually uses this method above to get its list before it display, so the order should be the same
        #that is, unless there was a change in the moment jsut before. which shouldn't happen. becasue this app doesn't update anything automatically on itself
        #fetch more details on that book:
          Abcdistill::Scraper.new.book_detail(book)
          #this should update the book with more details
        #display the book pseudo code:
          display_book_detail(book)

        process_bookchoice(genre)

        return
      elsif input == "quit"
        return
      else
        puts "Invalid input. Please re-enter"
      end #end if

    end #end loop
end
process_genrechoice() click to toggle source
# File lib/abcdistill/cli.rb, line 95
  def process_genrechoice
    input = nil
    puts "Type the number of the GENRE you want"
    while input != "quit"
      input = gets.strip

      if input.to_i > 0 && input.to_i < Abcdistill::Genre.all.size
        #fetch the books in that genre
        genre = Abcdistill::Genre.all[input.to_i - 1] #returns genre instance
        display_books_in_genre(genre)
        process_bookchoice(genre) #this will ask for input and process it


        return




      # if input == "1"
      #   puts "1. abc"
      #   process_choice()
      #   return #gotta put return otherwise u'll be doing nothing
      #   #cuz u have many recursive loop, but the outerloop still doesn't have the input = exit, only input of the inner loops are exit
      # elsif input == "2"
      #   puts "2. def"
      #   process_choice()
      #   return #gotta put return otherwise u'll be doing nothing
    elsif input == "quit"
        return
      else
        puts "Invalid input. Please re-enter"
      end #end if

    end #end loop
end
say_goodbye() click to toggle source
# File lib/abcdistill/cli.rb, line 30
def say_goodbye
  puts "Bye. Thanks for using our app."
end