Class: Articlecli::Cli

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

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



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

def initialize
    @sc = Scraper.new
end

Instance Method Details

#callObject



9
10
11
12
13
14
# File 'lib/cli.rb', line 9

def call
    @sc.create_articles
    @newspaper = @sc.newspaper
    @articles = @newspaper.articles
    start
end

#list_articlesObject



50
51
52
53
54
55
56
# File 'lib/cli.rb', line 50

def list_articles
    @articles.each_with_index do |x,i|
        puts '---------'
        puts "#{i+1}. #{x.title}"
        puts '---------'
    end
end

#list_categoryObject



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

def list_category
    @articles.each do |x|
        puts '---------'
        puts x.category
        puts '---------'
    end

end


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

def print_commands
    puts "To see all article titles type 'all'"
    puts "To see the category type 'cat'"
    puts "To see a random article type 'random'"
    puts "To see a specific article type 'article'"
    puts "To exit type 'quit'"
    puts '************************************'
end

#show_articleObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cli.rb', line 76

def show_article
    list_articles
    puts 'Pick an article number!'
    input = gets.strip
    input = input.to_i - 1
    if input >= 0 && input < @articles.size
        puts '---------'
        puts @articles[input].title
        puts '---------'
        puts @articles[input].content
        puts '---------'
    end

end

#show_random_articleObject



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

def show_random_article
    article = @articles.sample
    puts '---------'
    puts article.title
    puts '---------'
    puts article.content
    puts '---------'
end

#startObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cli.rb', line 16

def start
    puts 'Welcome!'
    input = ''

    while input != 'quit'
        print_commands
        input = gets.strip
        
        case input
        when "all"
            list_articles
        when "cat"
            list_category
        when "random"
            show_random_article
        when "article"
            show_article
        end
    
    end

    
end