Class: Articlecli::Cli
- Inherits:
-
Object
- Object
- Articlecli::Cli
- Defined in:
- lib/cli.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #list_articles ⇒ Object
- #list_category ⇒ Object
- #print_commands ⇒ Object
- #show_article ⇒ Object
- #show_random_article ⇒ Object
- #start ⇒ Object
Constructor Details
Instance Method Details
#call ⇒ Object
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_articles ⇒ Object
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_category ⇒ Object
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 |
#print_commands ⇒ Object
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_article ⇒ Object
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_article ⇒ Object
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 |
#start ⇒ Object
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 |