Class: PodcastBookClub::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



3
4
5
6
7
8
9
10
11
12
# File 'lib/podcast_book_club/cli.rb', line 3

def initialize
    welcome_message
    @today = Date.today
    
    Whirly.start(spinner: "pencil", color: false, remove_after_stop: true, status: "Loading Episodes") do
        @scraper = Scraper.new
    end

    call
end

Instance Method Details

#callObject



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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/podcast_book_club/cli.rb', line 14

def call

    until @input == "exit"

        menu_options

        puts "\n\nEnter your selection:"
        @input = gets.chomp.downcase

        case @input
        when "1", "this week"
            first_date = @today - @today.wday
            episodes = Episode.find_by_date(first_date, @today)

            select_episodes(episodes)

        when "2", "last week"
            first_date = @today - @today.wday - 7
            last_date = @today - @today.wday
            episodes = Episode.find_by_date(first_date, last_date)

            select_episodes(episodes)

        when "3", "this month"
            first_date = @today - @today.mday + 1
            episodes = Episode.find_by_date(first_date, @today)

            select_episodes(episodes)

        when "4", "last month"
            last_date = @today - @today.mday
            first_date = last_date - last_date.mday + 1
            episodes = Episode.find_by_date(first_date, last_date)

            select_episodes(episodes)

        when "5", "this year"
            first_date = @today - @today.yday + 1
            episodes = Episode.find_by_date(first_date, @today)

            select_episodes(episodes)

        when "6", "keyword"
            puts "\n\nEnter a keyword or phrase:"
            keyword = gets.chomp.downcase
            episodes = Episode.find_by_keyword(keyword)

            select_episodes(episodes)

        when "7", "author"

            if Book.count > 0
                Author.sort_by_name.each { |author| output_author(author) }
            else
                no_books
            end

        when "8", "genre"

            if Book.count > 0
                Genre.sort_by_name.each { |genre| output_genre(genre) }
            else
                no_books
            end

        when "9", "search"

            if Book.count > 0
                puts "\n\nEnter a keyword or phrase:"
                keyword = gets.chomp.downcase
                books = Book.find_by_keyword(keyword)

                books.each_with_index do |book, i|
                    episodes = book.episode.map { |ep| ep.title}

                    output_book(book, i+1)
                    puts "From the episode(s): #{episodes.join(", ")}\n\n"
                end

            else
                no_books
            end

        when "animal style"
            create_library(Episode.all)

        when "help"
            menu_options

        when "exit"
            break

        else
            unexpected_input

        end

    end

end

#create_library(episodes) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/podcast_book_club/cli.rb', line 116

def create_library(episodes)
    episodes.each do |episode|

        @scraper.build_books(episode) unless episode.books != []

        puts "\n\nHere are the recommendations from \"#{episode.title}\":\n\n"

        episode.books.each_with_index do |book, i|
            output_book(book, i+1)
        end
    end
end

#select_episodes(episodes) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/podcast_book_club/cli.rb', line 129

def select_episodes(episodes)

    select_menu(episodes)
    @selection = gets.chomp.downcase

    case @selection
    when /\d/
        unless @selection.to_i > episodes.count
            selected_episodes = [episodes[@selection.to_i - 1]]
            create_library(selected_episodes)

        else
            unexpected_input
            select_menu(episodes)
            @selection = gets.chomp.downcase

        end

    when "all"
        create_library(episodes)
    when "back"
        call
    when "exit"
        @input = "exit"
    else
        unexpected_input
    end

end