Class: Libri::CLI

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

Constant Summary collapse

LINE =
"----------------------------------------------------------"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#awardObject

Returns the value of attribute award.



3
4
5
# File 'lib/libri/cli.rb', line 3

def award
  @award
end

#awards_arrayObject

Returns the value of attribute awards_array.



3
4
5
# File 'lib/libri/cli.rb', line 3

def awards_array
  @awards_array
end

#book_info_hashObject

Returns the value of attribute book_info_hash.



3
4
5
# File 'lib/libri/cli.rb', line 3

def book_info_hash
  @book_info_hash
end

#books_arrayObject

Returns the value of attribute books_array.



3
4
5
# File 'lib/libri/cli.rb', line 3

def books_array
  @books_array
end

#quotes_arrayObject

Returns the value of attribute quotes_array.



3
4
5
# File 'lib/libri/cli.rb', line 3

def quotes_array
  @quotes_array
end

#title_by_authorObject

Returns the value of attribute title_by_author.



3
4
5
# File 'lib/libri/cli.rb', line 3

def title_by_author
  @title_by_author
end

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/libri/cli.rb', line 7

def call
    puts "Welcome to Libri.".blue
    puts "I'm the Raven, your guide in this chamber full of literary wonders.".blue
    puts "Below are some of the most prized literary awards of our time.".blue
    puts "Come freely. This will take a few moments...".blue
    puts LINE
    make_awards
    list_awards
    puts LINE
    leave
end

#leaveObject



149
150
151
# File 'lib/libri/cli.rb', line 149

def leave
    puts "Farewell!".blue
end

#list_awardsObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/libri/cli.rb', line 23

def list_awards
    Libri::Awards.all.each.with_index(1) { |award, i|
        puts "#{i}. #{award.name}"
    }
    puts LINE
    puts "Which award would you like to explore?".blue
    puts LINE

    menu_awards
end

#list_books(award) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/libri/cli.rb', line 38

def list_books(award)
    Libri::Books.all.each.with_index(1) { |book, i|
        puts "#{i}. #{book.title} #{book.author}"
    }
    puts LINE
    puts "Which book would you like to know more about?".blue
    puts LINE

    menu_books(award)
    
end

#list_details(book) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/libri/cli.rb', line 50

def list_details(book)
    info = Libri::Scraper.new.scrape_book(book)

    puts 
    puts "Title by Author".upcase.red
    puts LINE
    puts "#{info.title_by_author}"
    puts
    puts "Blurbs and Plot".upcase.red
    puts LINE
    puts "#{info.blurbs_and_plot}"
    puts
    puts "About the Author".upcase.red
    puts LINE
    puts "#{info.about_author}"
    puts 
    puts "Availability".upcase.red
    puts LINE
    puts "#{info.availability}"
    puts        
    puts "URL".upcase.red
    puts LINE
    puts "#{info.url}"
    puts 

    if !info.excerpt.nil?
        puts "An excerpt of this book is available. Would you like to read it? (Yn)".blue
        input = STDIN.gets.strip.downcase
        if input == "y"
            puts
            puts "Excerpt".upcase.red
            puts LINE
            puts "#{info.excerpt.slice(1..1000)}..."
            puts
        else
            nil
        end
    end
end

#make_awardsObject



19
20
21
# File 'lib/libri/cli.rb', line 19

def make_awards
    Libri::Scraper.new.scrape_barnes_noble
end

#make_books(award) ⇒ Object



34
35
36
# File 'lib/libri/cli.rb', line 34

def make_books(award)
    Libri::Scraper.new.scrape_award(award)
end


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

def menu_awards
    input = STDIN.gets.strip.downcase

    if input.to_i.between?(1,Libri::Awards.all.size)
        award = Libri::Awards.all[input.to_i - 1]
        Libri::Books.all.clear
        make_books(award)
        list_books(award)
    elsif input == "awards"
        list_awards
    elsif input == "exit"
        nil
    elsif input == "nevermore"
        puts LINE
        random_quote
    else
        puts "The raven croaked, 'Please try again.'".red
        puts LINE
        list_awards
    end
end


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/libri/cli.rb', line 122

def menu_books(award)
    input = STDIN.gets.strip.downcase

    if input.to_i.between?(1,Libri::Books.all.size)
        book = Libri::Books.all[input.to_i - 1]
        list_details(book)
        puts LINE
        puts "To list the books of the same award again, type books".blue
        puts "To list all the awards again, type awards".blue
        puts LINE
        menu_books(award)               
    elsif input == "nevermore"
        puts LINE
        random_quote
    elsif input == "books"
        list_books(award)
    elsif input == "awards"
        list_awards
    elsif input == "exit"
        nil
    else
        puts "The raven croaked, 'Please try again.'".red
        puts LINE
        list_books(award)
    end
end

#random_quoteObject



90
91
92
93
94
95
96
97
98
# File 'lib/libri/cli.rb', line 90

def random_quote
    quote = Libri::Scraper.new.scrape_quote.sample

    puts
    puts "#{quote.quote}"
    puts 
    puts "#{quote.author}"
    puts
end