Class: BookwormBestSellers::CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



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

def call
  puts "\nLOADING BEST SELLERS..."
  list_books
  prompt
end

#current_weekObject



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

def current_week
  time = Time.new
  if time.wday == 0
    time.strftime("%B %d, %Y")
  else
    sunday = time - (time.wday * 86400)
    sunday.strftime("%B %d, %Y")
  end
end

#exit_messageObject



44
45
46
# File 'lib/bookworm_best_sellers/cli.rb', line 44

def exit_message
  puts "\nThanks for using Bookworm! Have a great day!\n\n"
end

#list_booksObject



9
10
11
12
13
14
15
# File 'lib/bookworm_best_sellers/cli.rb', line 9

def list_books
  @books = BookwormBestSellers::Scraper.this_week
  puts "\nBest Sellers - Week of #{current_week}\n\n"
  @books.each.with_index(1) do |book, i|
    puts "#{i}. #{book.title} - #{book.author}"
  end
end

#promptObject



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

def prompt
  puts "\nType a number to learn more about a book, LIST to see the list again, or type EXIT."
  input = gets.strip.downcase
  if input.to_i > 0 && input.to_i < 11
    puts "\n#{@books[input.to_i-1].description}"
    prompt
  elsif input == "exit"
    exit_message
  elsif input == "list"
    list_books
    prompt
  else
    puts "\nINVALID CHOICE"
    prompt
  end
end