class Articlecli::Cli

Public Class Methods

new() click to toggle source
# File lib/cli.rb, line 5
def initialize
    @sc = Scraper.new
end

Public Instance Methods

call() click to toggle source
# File lib/cli.rb, line 9
def call
    @sc.create_articles
    @newspaper = @sc.newspaper
    @articles = @newspaper.articles
    start
end
list_articles() click to toggle source
# 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_category() click to toggle source
# File lib/cli.rb, line 58
def list_category
    @articles.each do |x|
        puts '---------'
        puts x.category
        puts '---------'
    end

end
print_commands() click to toggle source
show_article() click to toggle source
# 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_article() click to toggle source
# File lib/cli.rb, line 67
def show_random_article
    article = @articles.sample
    puts '---------'
    puts article.title
    puts '---------'
    puts article.content
    puts '---------'
end
start() click to toggle source
# 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