Class: BestSellingBooks::CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



4
5
6
7
8
# File 'lib/best_selling_books/cli.rb', line 4

def call
  choose_best_sellers_list
  choose_listing
  choose_info
end

#choose_best_sellers_listObject



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

def choose_best_sellers_list
  puts ">> Would you like to see Barnes & Noble's or Amazon's best sellers of #{Time.now.year}?"
  site = gets.strip
  case site.downcase
  when "amazon", "amazon's"
    @site = BestSellingBooks::Amazon
    create_and_display_book_list
  when "barnes & noble", "barnes & noble's", "barnes and noble", "barnes and noble's", "b&n"
    @site = BestSellingBooks::BarnesAndNoble
    create_and_display_book_list
  when "exit"
    exit
  else
    puts "Invalid input. Please type the name of the best sellers list you would like to view."
    choose_best_sellers_list
  end
end

#choose_infoObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/best_selling_books/cli.rb', line 66

def choose_info
  display_book_info_options
  loop do
    input = gets.strip
    case input.downcase
    when "a"
      puts "About " + @book.author
      puts @book.author_bio
    when "b"
      puts @book.price
    when "c"
      puts @book.format
    when "d"
      puts @book.rating
    when "e"
      puts @book.link
    when "list"
      @site.list_best_sellers
      choose_listing
      choose_info
    when "best sellers"
      call
    when "exit"
      exit
    else
      puts "Invalid input."
    end
  end
end

#choose_listingObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/best_selling_books/cli.rb', line 37

def choose_listing
  puts ">> Please type a listing number for more information."
  display_other_options
  input = gets.strip
  if input.downcase == "exit"
    exit
  elsif input.to_i != 0
    @book = @site.all_books.detect {|instance| instance.rank == input }
  elsif input.downcase == "list"
    create_and_display_book_list
    choose_listing
  elsif input.downcase == "best sellers"
    call
  else
    puts "Invalid input."
    choose_listing
  end
end

#create_and_display_book_listObject



28
29
30
31
# File 'lib/best_selling_books/cli.rb', line 28

def create_and_display_book_list
  @site.create_and_collect_books if @site.all_books == []
  @site.list_best_sellers
end

#display_book_info_optionsObject



56
57
58
59
60
61
62
63
64
# File 'lib/best_selling_books/cli.rb', line 56

def display_book_info_options
  puts @book.title
  puts "A. Author Bio"
  puts "B. Price"
  puts "C. Available Format"
  puts "D. Site Rating"
  puts "E. #{@site.name} Link"
  puts ">> What would you like to know about this listing? Please select a letter."
end

#display_other_optionsObject



33
34
35
# File 'lib/best_selling_books/cli.rb', line 33

def display_other_options
  puts "> Or type 'List' to return to the list, 'Best Sellers' to pick a different top 20 list, or 'Exit' to leave."
end