Class: GoodNews::Cli

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

Class Method Summary collapse

Class Method Details

.call_userObject

Class method to begin interaction with user. Gives a choice of topics or exit. Calls on #display_topics.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/good_news/cli.rb', line 6

def self.call_user
    user_input = nil
    while user_input != "exit"
        puts "Welcome, it looks like you are looking for some Good News!"
        puts "To see Good News topics, type 'topics'."
        puts "To quit, type 'exit'."
        puts "Please enter your choice now."

        user_input = gets.downcase.chomp

        case user_input
        when 'topics'
            self.display_topics
        when 'exit'
            self.good_bye
        end
    end
end

.display_articles(topic_input) ⇒ Object

Class method that loops through a topic objects articles attribute, an array of article objects. Offers user a choice of article to pick or to exit. Launches article in browser.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/good_news/cli.rb', line 52

def self.display_articles(topic_input)
    user_input = nil
    topic_articles = GoodNews::Topic.all[topic_input.to_i - 1].articles
    counter = 1
    topic_articles.each do |article|
        puts "#{counter}. #{article.title}"
        counter += 1
    end
    while user_input != "exit"
        puts "Please enter the number of the article to be taken to its page."
        puts "Or type 'exit' to quit."
        
        user_input = gets.chomp

        if (1..topic_articles.length).include?(user_input.to_i)
            puts "Hold onto your seat, we are sending you to some Good News!"
            Launchy.open(topic_articles[user_input.to_i - 1].web_addr)
        elsif user_input == "exit"
            self.good_bye 
        end
    end
end

.display_topicsObject

Class method to loop through topic objects and display names. Offers user choice of picking a topic to display articles in that topic or exit. Calls on #display_articles.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/good_news/cli.rb', line 28

def self.display_topics
    user_input = nil
    counter = 1
    GoodNews::Topic.all.each do |topic|
        puts "#{counter}. #{topic.name}"
        counter += 1
    end
    while user_input != "exit"
        puts "Please enter the number of the topic to see a list of articles."
        puts "Or type 'exit' to quit."
        
        user_input = gets.chomp

        if (1..GoodNews::Topic.all.length).include?(user_input.to_i)
            self.display_articles(user_input)
        elsif user_input == "exit"
            self.good_bye
        end
    end
end

.good_byeObject

Class method to abstract away the exit redundancy.



76
77
78
79
# File 'lib/good_news/cli.rb', line 76

def self.good_bye
    puts "See you next time!"
    exit
end