Class: CLI

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

Overview

> Handles all interfacing with our user

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#articlesObject

> Articles array accessor



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

def articles
  @articles
end

Instance Method Details

#display_article(article) ⇒ Object

> Displays all of the article information for the user to read



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/crowder_news/cli.rb', line 83

def display_article(article)
  input = nil
  puts "\n#{article.title}"
  puts "\nAuthor: #{article.author} on #{article.date}: "
  puts "\n\t#{article.body}"
  if article.youtube_links
    puts "\nYoutube Links:"
    article.youtube_links.each{ |link|
      puts link
    }
  end
  puts 'Press any key to find another article or "exit" to exit.'
  input = gets.strip.downcase
  if input == "exit"
    goodbye
  else
    list_articles
  end
end

#display_list(articles) ⇒ Object

> Displays a list of articles for the user



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

def display_list(articles)
  articles.each.with_index(1) do |article, index|
    puts "#{index}. #{article.title}"
    if(article.excerpt != "")
      puts "\t#{article.excerpt[0...98]}..."
    end
  end
  puts ""
end

#goodbyeObject

> Tells the user goodbye and exits program



106
107
108
# File 'lib/crowder_news/cli.rb', line 106

def goodbye
  puts "\nSee you next time!"
end

#initiateObject

> Intiates the program, scrapes the website and welcomes the user



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

def initiate
  Scraper.initiate_scrape
  puts "Welcome to Today on Crowder!"
  list_articles
end

#list_articlesObject

> Lists the articles according to the user’s preferences



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
# File 'lib/crowder_news/cli.rb', line 18

def list_articles
  input = nil
  choices = 'Choose one: recent, featured, or both. To exit type "exit".'
  puts choices
  input = gets.strip.downcase
  puts ""
  if input == "recent"
    puts "Recent Articles: "
    @articles = Article.recents
    display_list(@articles)
    menu
  elsif input == "featured"
    puts "Featured Articles: "
    @articles = Article.featured
    display_list(@articles)
    menu
  elsif input == "both"
    puts "All articles: "
    @articles = Article.all
    display_list(@articles)
    menu
  elsif input == "exit"
    goodbye
  else
    list_articles
  end

end

> Gives the user the options to view articles, switch lists, or exit the program



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/crowder_news/cli.rb', line 63

def menu
  input = nil
  puts "Enter the number of the article you want to see, type list to list the articles again or type exit: "
  input = gets.strip.downcase
  if input.to_i > 0 && input.to_i < @articles.length + 1
    article = @articles[input.to_i - 1]
    display_article(article)
  elsif input == "list"
    list_articles
  elsif input == "exit"
    goodbye
  else
    puts "Please make a valid selection."
    menu
  end
end