Class: ChildrensBooks::CLI

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

Instance Method Summary collapse

Instance Method Details

#callObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/childrens_books/cli.rb', line 4

def call 
    ChildrensBooks::Scraper.scrape
    puts ""
    puts "Welcome to the Children's Books Database!".magenta
    puts ""
    @input = nil
    menu
    while @input != "exit" 
        @input = gets.chomp
        if @input == "1"
            puts "Here are some books 2-4 year olds will love:".green
            books_array = ChildrensBooks::Book.preschoolbooks
            print_books(books_array)
            puts "Enter 'menu' for more options or 'exit' to leave the program.".green
        elsif @input == "2"
            puts "Here are some books 5-7 year olds will love:".green
            books_array = ChildrensBooks::Book.littlekidbooks
            print_books(books_array)
            puts "Enter 'menu' for more options or 'exit' to leave the program.".green
        elsif @input == "3"
            puts "Here are some books 8-9 year olds will love:".green
            books_array = ChildrensBooks::Book.bigkidbooks
            print_books(books_array)
            puts "Enter 'menu' for more options or 'exit' to leave the program.".green
        elsif @input == "4"
            puts "Here are some books 10-12 year olds will love:".green
            books_array = ChildrensBooks::Book.tweenbooks
            print_books(books_array)
            puts "Enter 'menu' for more options or 'exit' to leave the program.".green
        elsif @input == "5"
            puts "Here's a randomly selected book for you to try:".green
            book_obj = ChildrensBooks::Book.all.sample
            print_book(book_obj)
            puts "Enter 'menu' for more options or 'exit' to leave the program.".green
        elsif @input == "menu"
            menu
        elsif @input == "exit"
            break
        else 
            puts "Sorry, you did not select a valid number. Please try again.".red
            puts ""
            menu
        end
    end
    puts "Thanks for visiting. I hope you're headed out to the bookstore!".magenta
end


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

def menu
    puts "Select an age range to see the best children's book suggestions!".cyan
    puts ""
    puts <<-MENU
    1. Ages 2-4
    2. Ages 5-7
    3. Ages 8-9
    4. Ages 10-12
    5. Surprise me!
    Type "exit" to leave the program.
    Type "menu" at any time to return to menu.
    MENU
    puts ""
end


78
79
80
81
82
83
84
85
86
# File 'lib/childrens_books/cli.rb', line 78

def print_book(book)
        puts ""
        puts "Title: #{book.title}"
        puts "Author: #{book.author}"
        puts "Description: #{book.description}"
        puts "Release Year: #{book.year}"
        puts "Age: #{book.age}"
        puts ""
end


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/childrens_books/cli.rb', line 66

def print_books(array)
    array.each do |book|
        puts ""
        puts "Title: #{book.title}"
        puts "Author: #{book.author}"
        puts "Description: #{book.description}"
        puts "Release Year: #{book.year}"
        puts "Age: #{book.age}"
        puts ""
    end
end