Class: NprCliNewsReader::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



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

def initialize
  @done = false
  @categories = ["National", "World", "Politics", "Business", "Health", "Science", "Technology", "Race & Culture"]
end

Instance Attribute Details

#doneObject

Returns the value of attribute done.



3
4
5
# File 'lib/npr_cli_news_reader/cli.rb', line 3

def done
  @done
end

#selected_categoryObject

Returns the value of attribute selected_category.



3
4
5
# File 'lib/npr_cli_news_reader/cli.rb', line 3

def selected_category
  @selected_category
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/npr_cli_news_reader/cli.rb', line 10

def call 
  greet_user
  present_categories
  handle_category_input
  handle_article_selection
  display_app_end_user_options
  while @done == false
    user_input = gets.strip
    if user_input.downcase == 'category'
      go_back_to_category_selection
    elsif user_input.downcase == 'article'
      go_back_to_articles
    else
      puts Rainbow('Thank you for using Npr CLI News Reader!').bright.fg(:white)
      puts Rainbow('Goodbye!').bright.fg(:green)
      @done = true
    end
  end
end

#display_app_end_user_optionsObject



44
45
46
47
48
49
# File 'lib/npr_cli_news_reader/cli.rb', line 44

def display_app_end_user_options
  puts Rainbow("What would you like to do?").bright.fg(:white)
  puts "#{Rainbow('Exit Program:').bright.fg(:white)} #{Rainbow('exit').bright.fg(:green)}"
  puts "#{Rainbow('Go back to Category Selection:').bright.fg(:white)} #{Rainbow('category').bright.fg(:green)}"
  puts "#{Rainbow('Go back to article selection:').bright.fg(:white)} #{Rainbow('article').bright.fg(:green)}"
end

#display_category_articlesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/npr_cli_news_reader/cli.rb', line 99

def display_category_articles
  NprCliNewsReader::Article.sort_articles_by_category(@selected_category).each_with_index do |article, i|
    formatted_teaser = format_teaser(article.teaser)
    if i % 2 == 0
      puts Rainbow("#{i + 1}").bright + ")." + Rainbow(" #{article.title}").bright
      puts
      puts Rainbow("#{formatted_teaser}")
      puts
      puts "--------------------"
      puts
    else
      puts Rainbow("#{i + 1}). #{article.title}").bright.fg(:green)
      puts
      puts Rainbow("#{formatted_teaser}").fg(:green)
      puts
      puts "--------------------"
      puts
    end
  end
end

#display_full_article(article) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/npr_cli_news_reader/cli.rb', line 135

def display_full_article(article)
  puts Rainbow("#{article.title}").bright
  puts "--------------------"
  article.full_article.each do |paragraph_node| 
    puts "\t#{paragraph_node.text}"
    puts "--------------------"
  end
  open_default_browser(article)
end

#format_teaser(teaser_string) ⇒ Object



78
79
80
81
# File 'lib/npr_cli_news_reader/cli.rb', line 78

def format_teaser(teaser_string)
  formatted_teaser = teaser_string.split(/\A.+[\u0095]\s/)[1]
  formatted_teaser
end

#go_back_to_articlesObject



30
31
32
33
34
# File 'lib/npr_cli_news_reader/cli.rb', line 30

def go_back_to_articles
  display_category_articles
  handle_article_selection
  display_app_end_user_options
end

#go_back_to_category_selectionObject



37
38
39
40
41
42
# File 'lib/npr_cli_news_reader/cli.rb', line 37

def go_back_to_category_selection
  present_categories
  handle_category_input
  handle_article_selection
  display_app_end_user_options
end

#greet_userObject



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

def greet_user
  puts Rainbow("Hello fellow fact finder...").bright
  puts Rainbow("Welcome to the Npr Cli News Reader.").bright.fg(:green)
  puts "--------------------"
  sleep(1.5)
end

#handle_article_selectionObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/npr_cli_news_reader/cli.rb', line 120

def handle_article_selection
  sorted_articles = NprCliNewsReader::Article.sort_articles_by_category(@selected_category)
  puts Rainbow("Which article would you like to see the full article for?").bright
  puts "Use the numbers associated with the articles."
  article_input = gets.strip
  while !valid_input?(article_input, sorted_articles)
    puts "Sorry, the input received is not valid to select an article"
    puts "Please enter a number 1-#{sorted_articles.size}:"
    article_input = gets.strip
  end
  article = sorted_articles[article_input.to_i - 1]
  NprCliNewsReader::Scraper.scrape_full_article(article)
  display_full_article(article)
end

#handle_category_inputObject



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

def handle_category_input
  puts Rainbow("Which Category would you like to see articles for?").bright
  puts "Use the numbers associated with the categories."
  category_input = gets.strip
  while !valid_input?(category_input, @categories)
    puts "Sorry, the input received is not valid to select a category"
    puts "Please enter a number 1-#{@categories.size}:"
    category_input = gets.strip  
  end
  @selected_category = @categories[category_input.to_i - 1].downcase
  puts Rainbow("#{@selected_category.capitalize} Category").bright
  puts "--------------------"
  NprCliNewsReader::Scraper.scrape_articles_for_category(@selected_category)
  display_category_articles
end

#open_default_browser(article) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/npr_cli_news_reader/cli.rb', line 145

def open_default_browser(article)
  puts Rainbow("Would you like to view this article on the npr website? (Y/n): ").bright.fg(:white)
  user_input = gets.strip.downcase 
  if user_input == 'y'
    system("open", "#{article.article_url}")
  end
end

#present_categoriesObject



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

def present_categories
  puts Rainbow("Categories").bright
  puts "--------------------"
  @categories.each_with_index do |category, i|
    if i.even?
      puts Rainbow("#{i + 1}). #{category}").bright
    else
      puts Rainbow("#{i + 1}). #{category}").bright.fg(:green)
    end
  end
end

#valid_input?(user_input, collection) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/npr_cli_news_reader/cli.rb', line 70

def valid_input?(user_input, collection)
  if user_input.to_i > 0 && user_input.to_i <= collection.size
    return true
  else
    return false
  end
end