Class: DeveloperNewsCliApp::CLI

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

Constant Summary collapse

@@websites =
["FreeCodeCamp", "HackerNoon", "CodeBurst", "A List Apart"]
@@current_article_count =
0
@@current_website =
""
@@current_articles =
[]

Instance Method Summary collapse

Instance Method Details

#article_selection_instructionsObject



56
57
58
59
60
61
# File 'lib/developer_news_cli_app/cli.rb', line 56

def article_selection_instructions
	puts "\nType the number of an article to see more information about it."
	puts "Or type list or l to see a list of websites."
	puts "Or type exit to end the program."
	get_article_selection
end

#callObject



7
8
9
10
11
12
# File 'lib/developer_news_cli_app/cli.rb', line 7

def call
	welcome
	get_articles
	list_websites
	get_article_selection
end

#current_article_countObject



207
208
209
# File 'lib/developer_news_cli_app/cli.rb', line 207

def current_article_count
	@@current_article_count
end

#display_summariesObject



189
190
191
192
193
194
195
196
197
# File 'lib/developer_news_cli_app/cli.rb', line 189

def display_summaries
	DeveloperNewsCliApp::Article.clear_current_articles
	puts "Free Code Camp (FCC)"
	DeveloperNewsCliApp::Article.add_to_current_artciles(DeveloperNewsCliApp::Article.display_top_three("FreeCodeCamp"))
	puts "HackerNoon (HN)"
	DeveloperNewsCliApp::Article.add_to_current_artciles(DeveloperNewsCliApp::Article.display_top_three("HackerNoon"))
	puts "CodeBurst (CB)"
	DeveloperNewsCliApp::Article.add_to_current_artciles(DeveloperNewsCliApp::Article.display_top_three("CodeBurst"))
end

#get_article_selectionObject



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

def get_article_selection
	input = nil
	max_number = @@current_articles.count
	input = gets.strip
	if input.to_i <= max_number && input.to_i != 0
		print_summary(@@current_articles[input.to_i - 1])
	elsif input == "list" || input == "l"
		list_websites
		initial_instructions
	elsif	input.to_i > max_number
		puts "I'm not sure what you meant."
		article_selection_instructions
	elsif input =="exit"
		goodbye
	else
		get_article_selection
	end
end

#get_article_summary_input(article) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/developer_news_cli_app/cli.rb', line 101

def get_article_summary_input(article)
	puts "\nType read or r to open the article in your browser."
	puts "Type back or b to go back to the list of #{@@current_website} articles."
	puts "Type list or l to see a list of websites."
	puts "Type exit to exit the program."

	input = gets.strip
	case input
	when "r", "read"
		open_url(article)
		print_summary(article)
	when "b", "back"
		if @@current_website == "FreeCodeCamp"
			show_FreeCodeCampArticles
			article_selection_instructions
		elsif @@current_website == "HackerNoon"
			show_HackerNoonArticles
			article_selection_instructions
		elsif @@current_website == "CodeBurst"
			show_CodeBurstArticles
			article_selection_instructions
		else
			show_AListApartArticles
			article_selection_instructions
		end
	when "list", "l"
		list_websites
		# initial_instructions
		get_website_input
	when "exit"
		goodbye
	else
		puts "I'm not sure what you meant."
		get_article_summary_input(article)
	end
end

#get_articlesObject



19
20
21
22
23
24
# File 'lib/developer_news_cli_app/cli.rb', line 19

def get_articles
	DeveloperNewsCliApp::FreeCodeCampScrapper.new.make_article
	DeveloperNewsCliApp::HackerNoonScrapper.new.make_article
	DeveloperNewsCliApp::CodeBurstScrapper.new.make_article
	DeveloperNewsCliApp::AListApartScrapper.new.make_article
end

#get_website_inputObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/developer_news_cli_app/cli.rb', line 35

def get_website_input
	input = nil
	input = gets.strip
	if input == "1"
		show_FreeCodeCampArticles
	elsif	input == "2"
		show_HackerNoonArticles
	elsif input == "3"
		show_CodeBurstArticles
	elsif input == "4"
		show_AListApartArticles
	elsif input == "exit"
		goodbye
	elsif input == "list"
		list_websites
	else
		puts "I'm not sure what you meant."
		list_websites
	end
end

#goodbyeObject



224
225
226
227
# File 'lib/developer_news_cli_app/cli.rb', line 224

def goodbye
	puts "Thanks for using Developer News!"
	exit
end

#list_articlesObject



182
183
184
185
186
187
# File 'lib/developer_news_cli_app/cli.rb', line 182

def list_articles
	puts "Today's top developer articles:"
	puts ""
	display_summaries
	puts ""
end

#list_websitesObject



26
27
28
29
30
31
32
33
# File 'lib/developer_news_cli_app/cli.rb', line 26

def list_websites
	puts ""
	self.websites.each.with_index(1){|website, index| puts"#{index}. #{website}"}
	puts ""
	puts "Please type the number of the website to see more of its articles"
	puts "Or type exit to end the program"
	get_website_input
end

#open_url(article) ⇒ Object



199
200
201
# File 'lib/developer_news_cli_app/cli.rb', line 199

def open_url(article)
	`open #{article.url}`
end


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

def print_summary(article)
	puts "---------- #{@@current_website} ----------"
	puts "Title: #{article.title}"
	if	article.subtitle != nil
		puts "Subtitle: #{article.subtitle}"
	end
	if article.trailing != nil
		puts "Trailing: #{article.trailing}"
	end
	puts "Author: #{article.author}"
	if article.date != nil
		puts "Date: #{article.date}"
	end
	if article.read_time != nil
		puts "Read Time: #{article.read_time}"
	end
	get_article_summary_input(article)
end

#show_AListApartArticlesObject



171
172
173
174
175
176
177
178
179
180
# File 'lib/developer_news_cli_app/cli.rb', line 171

def show_AListApartArticles
	@@current_website = "AListApart"
	update_current_article_count("AListApart")
	@@current_articles = DeveloperNewsCliApp::AListApartScrapper.articles
	puts "\nA List Apart:\n"
	DeveloperNewsCliApp::AListApartScrapper.articles.each.with_index(1) do |article, index|
		puts "#{index}. #{article.title}"
	end
	article_selection_instructions
end

#show_CodeBurstArticlesObject



160
161
162
163
164
165
166
167
168
169
# File 'lib/developer_news_cli_app/cli.rb', line 160

def show_CodeBurstArticles
	@@current_website = "CodeBurst"
	update_current_article_count("CodeBurst")
	@@current_articles = DeveloperNewsCliApp::CodeBurstScrapper.articles
	puts "\nCodeBurst:\n"
	DeveloperNewsCliApp::CodeBurstScrapper.articles.each.with_index(1) do |article, index|
		puts "#{index}. #{article.title}"
	end
	article_selection_instructions
end

#show_FreeCodeCampArticlesObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/developer_news_cli_app/cli.rb', line 138

def show_FreeCodeCampArticles
	@@current_website = "FreeCodeCamp"
	update_current_article_count("FreeCodeCamp")
	@@current_articles = DeveloperNewsCliApp::FreeCodeCampScrapper.articles
	puts "\nFreeCodeCamp:\n"
	DeveloperNewsCliApp::FreeCodeCampScrapper.articles.each.with_index(1) do |article, index|
		puts "#{index}. #{article.title}"
	end
	article_selection_instructions
end

#show_HackerNoonArticlesObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/developer_news_cli_app/cli.rb', line 149

def show_HackerNoonArticles
	@@current_website = "HackerNoon"
	update_current_article_count("HackerNoon")
	@@current_articles = DeveloperNewsCliApp::HackerNoonScrapper.articles
	puts "\nHackerNoon:\n"
	DeveloperNewsCliApp::HackerNoonScrapper.articles.each.with_index(1) do |article, index|
		puts "#{index}. #{article.title}"
	end
	article_selection_instructions
end

#update_current_article_count(website) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/developer_news_cli_app/cli.rb', line 211

def update_current_article_count(website)
	case website
	when "FreeCodeCamp"
		@@current_article_count = DeveloperNewsCliApp::FreeCodeCampScrapper.article_count
	when "HackerNoon"
		@@current_article_count = DeveloperNewsCliApp::HackerNoonScrapper.article_count
	when "CodeBurst"
		@@current_article_count = DeveloperNewsCliApp::CodeBurstScrapper.article_count
	when "AListApart"
		@@current_article_count = DeveloperNewsCliApp::AListApartScrapper.article_count
	end
end

#websitesObject



203
204
205
# File 'lib/developer_news_cli_app/cli.rb', line 203

def websites
	@@websites
end

#welcomeObject



14
15
16
17
# File 'lib/developer_news_cli_app/cli.rb', line 14

def welcome
	puts "Welcome to the Developer News CLI App."
	puts "Scrapping the websties now..."
end